代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
通用数据查询
作者:
mickelfeng
/ 发布于
2012/10/22
/
466
通用数据查询
<div>ackage org.zch.infra.helper;</div> <div></div> import java.util.ArrayList;</div> import java.util.Arrays;</div> import java.util.Date;</div> import java.util.HashMap;</div> import java.util.List;</div> import java.util.Map;</div> import java.util.Map.Entry;</div> <div></div> <div>public class Criteria {</div> <div></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>private final List<String> ands = new ArrayList<String>();</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>private final List<String> ors = new ArrayList<String>();</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>private final Map<String,Object> paramSources = new HashMap<String,Object>();</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>private final static String WHERE = " WHERE ";</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>private final static String AND = " AND ";</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>private final static String OR = " OR ";</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>private final static String LIKE = " LIKE ";</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>private final static String EQ = "=";</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>private final static String GT = ">";</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>private final static String LT = "<";</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>private final static String GE = ">=";</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>private final static String LE = "<=";</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>private final static String COLON = ":";</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>private static String join(List<String> list, String connector) {</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>StringBuilder joins = new StringBuilder();</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>for(int i=0; i<list.size(); i++) {</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>String s = list.get(i);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>if("".equals(s) || s == null) continue;</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>if(joins.length() > 0) joins.append(connector);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>joins.append(s);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span>return joins.toString();</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>private static Map<String,Object> indentMapKeys(Map<String,Object> ps, int i){</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>Map<String,Object> paramSources = new HashMap<String,Object>();</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>for(Entry<String,Object> e : ps.entrySet() ){</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>paramSources.put(e.getKey() + "_" + i, e.getValue());</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span>return paramSources;</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>private static String indentParams(String sql, int i) {</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>return sql.replaceAll("(:(\\S)*)", "$1_" + i);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>private static String toCamel(String s) {</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>String[] split = s.split("_");</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>StringBuilder camel = new StringBuilder();</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>for(String str : split){</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>camel.append(Character.toUpperCase(str.charAt(0)) + str.substring(1).toLowerCase());</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span>return Character.toLowerCase(camel.charAt(0)) + camel.substring(1);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>public static String formatAsDBColumn(String s) {</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>if(s.contains("_")) return s;</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>return s.replaceAll("([A-Z])", "_$1").toUpperCase();</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>private String genParamName(String key){</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>return toCamel(key) + this.paramSources.size();</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>private Criteria addCretria(List<String> criterias, String key, String symbol, Object value){</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>String colName = formatAsDBColumn(key);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>String paramName = genParamName(key);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>criterias.add(colName + symbol + COLON + paramName);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>this.paramSources.put(paramName, value);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>return this;</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>private Criteria addIsCretria(List<String> criterias, String key, String value){</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>String colName = formatAsDBColumn(key);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>criterias.add(colName + " IS " + value);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>return this;</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>private Criteria addBetweenCretria(List<String> criterias, String key, Object from, Object to){</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>String paramFrom = genParamName(key);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>this.paramSources.put(paramFrom, from);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>String paramTo = genParamName(key);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>this.paramSources.put(paramFrom, to);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>String colName = formatAsDBColumn(key);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>criterias.add(colName + " BETWEEN " + COLON + paramFrom + " AND " + COLON + paramTo );</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>return this;</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>private Criteria addINCriteria(List<String> criterias, String key, Object[] values){</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>StringBuilder inCriteria = new StringBuilder();</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>for(Object value : values){</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>String paramName = genParamName(key);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>inCriteria.append(COLON + paramName + ",");</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>this.paramSources.put(paramName, value);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span>inCriteria.deleteCharAt(inCriteria.length()-1);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>String colName = formatAsDBColumn(key);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>this.ands.add(colName + " IN ( " + inCriteria.toString() + ")");</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>return this;</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>public Criteria andEQ(String key, Object value){</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>return addCretria(ands, key, EQ, value);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>public Criteria andGT(String key, Object value){</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>return addCretria(ands, key, GT, value);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>public Criteria andLT(String key, Object value){</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>return addCretria(ands, key, LT, value);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>public Criteria andGE(String key, Object value){</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>return addCretria(ands, key, GE, value);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>public Criteria andLE(String key, Object value){</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>return addCretria(ands, key, LE, value);</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>} <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>public Criteria andIN(String key, Object... values){</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>return addINCriteria(ands, key, valu</div>
试试其它关键字
通用数据
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
mickelfeng
贡献的其它代码
(
22
)
.
安卓图片异步加载工具类
.
加密解密php代码
.
将PHP数组有格式的写入文件中
.
获取访问页面的http状态码
.
WinInet远程下载文件的
.
用NetAPI获取网卡的物理MAC地址
.
极方便的动态 SQL 生成工具
.
PHP图片缩放函数:实现等比例不失真缩放
.
判断手机移动设备访问
.
扁平化的bootstrap
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3