这里我就不想多说应该能看懂代码的都应该知道什么意思了。
这个类暂时应该就是这样子了。
package net.zz.zjf.plugin; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Created by ZaoSheng on 2015/7/15. */ public class QueryParams { private Integer pageIndex = 1; private Integer pageSize = 10; private List<String> groups = new ArrayList<String>(); private Map<String, Object> likes = new HashMap<String, Object>(); private Map<String, Object> orders = new HashMap<String, Object>(); private Map<String, List<String>> ins = new HashMap<String, List<String>>(); protected String sql = " where 1=1 "; private Map<String, Object> value = new HashMap<String, Object>(); private List<Object> paras = new ArrayList<Object>(); // private Map<String, List<Object>> mutValues = new HashMap<String, List<Object>>(); public void addOrder(String key, OrderAD ad) { orders.put(key, ad.name()); } public void addOrder(String key) { orders.put(key, OrderAD.DESC.name()); } public enum OrderAD { DESC, ASC } public Map<String, Object> getSqlLikes() { return likes; } public QueryParams addGroup(String group) { this.groups.add(group); return this; } public QueryParams addIn(String propertyName, List<String> values) { if (null != values && values.size() > 0) { ins.put(propertyName, values); } return this; } public Map<String, Object> getLikes() { return likes; } public Map<String, List<String>> getIns() { return ins; } public List<Object> getParas() { return paras; } public QueryParams like(String propertyName, String value, MatchMode matchMode) { this.likes.put(propertyName, matchMode.toMatchString(value)); return this; } public QueryParams like(String propertyName, String value) { like(propertyName, value, MatchMode.ANYWHERE); return this; } public List<String> getGroups() { return groups; } private String prefix(String prefix) { if (prefix != null && !"".equals(prefix.trim())) { return prefix += "."; } return "m."; } public String toGroupSQL(String prefix) { prefix = prefix(prefix); if (groups != null && groups.size() >= 1) { StringBuilder g = new StringBuilder(); for (String group : groups) { g.append(prefix).append(group).append(", "); } g.deleteCharAt(g.length() - 2); return String.format(" GROUP BY %s ", g.toString()); } return ""; } public String toLikeSQL(String prefix) { prefix = prefix(prefix); if (likes != null && !likes.isEmpty()) { StringBuilder g = new StringBuilder(); for (String like : likes.keySet()) { String _sql = " and %s%s like :%s "; g.append(String.format(_sql, prefix, like, like)); } return g.toString(); } return ""; } public String toInSQL(String prefix) { prefix = prefix(prefix); if (ins != null && !ins.isEmpty()) { StringBuilder g = new StringBuilder(); for (String in : ins.keySet()) { String _sql = " and %s%s in( :%s )"; g.append(String.format(_sql, prefix, in, in)); } return g.toString(); } return ""; } public Integer getPageIndex() { return pageIndex; } public void setPageIndex(Integer pageIndex) { this.pageIndex = pageIndex; } public Integer getPageSize() { return pageSize; } public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } public QueryParams add(String key, Object _value) { if (_value == null) { value.remove(key); } else { value.put(key, _value); } return this; } public Map<String, Object> getSqlValue() { return value; } public String toOrderSQL() { return toOrderSQL(""); } public String toOrderSQL(String prefix) { prefix = prefix(prefix); if (!orders.isEmpty()) { StringBuffer sb = new StringBuffer(); sb.append("Order BY "); for (String order : orders.keySet()) { System.out.println( orders.get(order)); sb.append(String.format(" %s%s %s, ", prefix, order, orders.get(order))); } sb.deleteCharAt(sb.length() - 2); return sb.toString(); } return ""; } public String toWhereSQL() { return toWhereSQL(""); } public String toWhereSQL(String prefix) { prefix = prefix(prefix); String _sql = " and %s%s = :%s "; Set<String> keys = value.keySet(); Map<String, Object> _value = new HashMap<String, Object>(); StringBuffer sb = new StringBuffer(); sb.append(sql); for (String key : keys) { String filterDotKey = key.replaceAll("\\.", "_"); // String filterDotKey = key; _value.put(filterDotKey, value.get(key)); sb.append(String.format(_sql, prefix, key, filterDotKey)); } this.value = _value; return sb.toString(); } public String toFormatSQL(String hsql) { Matcher matcher = Pattern.compile(":(\\w+)").matcher(hsql); while ( matcher.find()){ String rexp = null; String group = matcher.group(1); Object ov = value.get(group); if (ov instanceof List) { StringBuilder sb = new StringBuilder(); List vs = (List) ov; for (Object v : vs) { sb.append("?,"); paras.add(v); } sb.deleteCharAt(sb.length() - 1); rexp = sb.toString(); }else { paras.add(ov); rexp = "?"; } hsql = hsql.replace(String.format(":%s", group), rexp); } return hsql; } public String toSqlExceptSelect(String tableName, String prefix ) { String hsql = " from " + tableName + " " + prefix + toWhereSQL(prefix) + toInSQL(prefix) + toLikeSQL(prefix) + toGroupSQL(prefix) + toOrderSQL(prefix); getSqlValue().putAll(getSqlLikes()); getSqlValue().putAll(getIns()); return toFormatSQL(hsql); } public static void main(String[] args) { QueryParams params = new QueryParams(); params.add("id", 1); params.addGroup("cc"); List<String> names = new ArrayList<String>(); names.add("张三"); names.add("李四"); params.addIn("name", names); params.like("nick", "张"); params.addOrder("time"); String hsql = " from user" + " t " + params.toWhereSQL("t") + params.toInSQL("t") + params.toLikeSQL("t") + params.toGroupSQL("t") + params.toOrderSQL("t"); System.out.println("hsql:" + hsql); params.getSqlValue().putAll(params.getSqlLikes()); params.getSqlValue().putAll(params.getIns()); String sql = params.toFormatSQL(hsql); // System.out.println("sql:" + sql); params.atts(params.getParas().toArray()); } public void atts(Object ... os) { System.out.println("参数:"); for (Object o : os) { System.out.print(o); System.out.print(","); } } }
上面这个参数类main函数的运行结果为
hsql: from user t where 1=1 and t.id = :id and t.name in( :name ) and t.nick like :nick GROUP BY t.cc Order BY t.time DESC sql: from user t where 1=1 and t.id = ? and t.name in( ?,? ) and t.nick like ? GROUP BY t.cc Order BY t.time DESC 参数: 1,张三,李四,%张%,
其实这个类的很好的用法应该是每个实体对象都匹配一个DaoParams类然后继承这个类。在对应的set方法里面调用add方法,将对应的字段匹配进行。DaoParams类的字段可用枚举
接下来就是所依赖的类
/** * Created by ZaoSheng on 2015/7/15. */ /** * Represents an strategy for matching strings using "like". * * @author Gavin King * @see (MatchMode) */ public enum MatchMode { /** * Match the entire string to the pattern */ EXACT { @Override public String toMatchString(String pattern) { return pattern; } }, /** * Match the start of the string to the pattern */ START { @Override public String toMatchString(String pattern) { return pattern + ‘%‘; } }, /** * Match the end of the string to the pattern */ END { @Override public String toMatchString(String pattern) { return ‘%‘ + pattern; } }, /** * Match the pattern anywhere in the string */ ANYWHERE { @Override public String toMatchString(String pattern) { return ‘%‘ + pattern + ‘%‘; } }; /** * Convert the pattern, by appending/prepending "%" * * @param pattern The pattern for convert according to the mode * * @return The converted pattern */ public abstract String toMatchString(String pattern); }
接下来这个类也是对应依赖的类
package net.zz.zjf.plugin; /** * Created by ZaoSheng on 2015/7/30. */ public enum Restriction { /** * 等于查询(from Object o where o.property = ?) */ EQ { public String toMatchString(String pattern) { return "= :" + pattern; } }, /** * 非等于查询(from Object o where o.property <> ?) */ NE { public String toMatchString(String pattern) { return "<> :" + pattern; } }, /** * 大于等于查询(from Object o where o.property >= ?) */ GE { public String toMatchString(String pattern) { return ">= :" + pattern; } }, /** * 大于查询(from Object o where o.property > ?) */ GT { @Override public String toMatchString(String pattern) { return "> :" + pattern; } }, /** * 小于等于查询(from Object o where o.property <= ?) */ LE { @Override public String toMatchString(String pattern) { return "> :" + pattern; } }, /** * 小于查询(from Object o where o.property < ?) */ LT { @Override public String toMatchString(String pattern) { return "> :" + pattern; } }, /** * 包含查询(from Object o where o.property in(?,?,?)) */ IN { @Override public String toMatchString(String pattern) { return "> :" + pattern; } }, /** * 非包含查询(from Object o where o.property not in(?,?,?)) */ NIN { @Override public String toMatchString(String pattern) { return "> :" + pattern; } }, /* *//** * 左模糊查询(from Object o where o.property like %?) *//* LLIKE { @Override public String toMatchString(String pattern) { return ‘%‘ + pattern; } }, *//** * 右模糊查询(from Object o where o.property like ?%) *//* RLIKE { @Override public String toMatchString(String pattern) { return pattern + ‘%‘; } }, *//** * 模糊查询(from Object o where o.property like %?%) *//* LIKE { @Override public String toMatchString(String pattern) { return ‘%‘ + pattern + ‘%‘; } }*/; public abstract String toMatchString(String pattern); }
差不多就是这样子吧,本人小菜一枚,表达能力也不是很好。请大神们见谅。初学者还有很多要学的,请大神多指教。
继续对JFinal的Model部分包装一下,github地址:
https:
//github.com/cnzzs/zjf
本文出自 “7031393” 博客,请务必保留此出处http://7041393.blog.51cto.com/7031393/1680736
原文地址:http://7041393.blog.51cto.com/7031393/1680736