两个类分别是Sql和Where,支持hql语句和支持sql语句拼凑,当你传入的参数为空时,拼凑的这个条件就会自动的换成“1=1”,使用方法:Sql sql = Sql.start("select * from tbl_name").add(Where.newIntance().like("property", value), "where")(这里拼接很灵活大家可以看代码)获取sql语句直接可以sql.toString(),如果获取所有参数可以通过sql.getParams()。
Sql.java
复制代码
Where.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.fbtt.axst.utils; import java.util.ArrayList; import java.util.List; public class Sql { private Sql(String hql) { this.hql = hql; } public static Sql start(String hql){ return new Sql(hql); } //所有的参数 private List params = new ArrayList(); //最终的hql语句 private String hql = null; /** * 向当前hql语句追加语句 * Example: * hql = Hql.start("from Employee a "); * hql.add("order by a.name") * String str = hql.toString(); * 此处str为:from Employee a order by a.name; */ public Sql add(String str){ hql = hql + " " + str; return this; } /** * 向当前hql语句追加语句,其它的参数 * Example: * hql = Hql.start("from Employee a "); * hql.add("where a.name like ?", new Serializable[]{"张三"}); * String str = hql.toString(); * str为:from Employee a where a.name like ? * Serializable[] params = hql.getParams(); * params为:["张三"] * @param hql * @param params */ @SuppressWarnings("unchecked") public Sql add(String str, Object[] params){ this.add(str); if(params != null){ for(Object obj : params){ this.params.add(obj); } } return this; } public Sql add(String str, Object param){ this.add(str, new Object[]{param}); return this; } //获取所有参数 public Object[] getParams(){ return params.toArray(); } /** * 添加where条件 * Example: * WhereHql wh = WhereHql.newInstance(); * wh.eq("a.name", "张三"); * Hql hql = Hql.start("from Employee a "); * hql.add(wh, "where"); * hql.toString(); * 此时得到:from Employee a where a.name = '张三' * @param wh * @param prefix 当wh.toString不为空时在其前添加的前缀 * @return */ public Sql add(Where wh, String prefix){ String wstr = wh.toString(); if(prefix == null)prefix = ""; if(wstr != null){ this.add(prefix + " " + wstr, wh.getParams()); } return this; } /** * 获取生成的hql语句 */ @Override public String toString() { return hql; } }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.fbtt.axst.utils; import java.util.ArrayList; import java.util.List; public class Where { private Where(){ } public static Where newInstance(){ return new Where(); } //所有的参数 private List params = new ArrayList(); //最终的hql语句 private String hql = null; /** * 向当前hql语句追加语句,及它的参数 * @param params */ @SuppressWarnings("unused") private Where addNotNull(String str, Object param, String prefix){ if(param == null || param.equals(""))return this; this.add(str, param, prefix); return this; } private Where addAndNotNull(String str, Object param){ if(param == null || param.equals(""))return this; this.add(str, param, "and"); return this; } @SuppressWarnings("unchecked") public Where add(String str, Object param, String prefix){ if(prefix == null)prefix = "and"; if(hql == null) hql = str; else hql = hql + " " + prefix + " " + str; if(param != null){ this.params.add(param); } return this; } @Override public String toString() { return hql; } //获取所有参数 public Object[] getParams(){ return this.params.toArray(); } //= public Where eq(String prop, Object param){ if(param == null || param.equals(""))return this; this.addAndNotNull(prop + "=?", param); return this; } //< public Where lt(String prop, Object param){ if(param == null || param.equals(""))return this; this.addAndNotNull(prop + " < ?", param); return this; } //<= public Where le(String prop, Object param){ if(param == null || param.equals(""))return this; this.addAndNotNull(prop + " <= ?", param); return this; } //like public Where like(String prop, Object param){ if(param == null || param.equals(""))return this; this.addAndNotNull(prop + " like ?", "%" + param + "%"); return this; } //> public Where gt(String prop, Object param){ if(param == null || param.equals(""))return this; this.addAndNotNull(prop + " > ?", param); return this; } //>= public Where ge(String prop, Object param){ if(param == null || param.equals(""))return this; this.addAndNotNull(prop + " >= ?", param); return this; } }
转载于:https://my.oschina.net/huangsm/blog/29121
最后
以上就是眼睛大冰淇淋最近收集整理的关于Java数据库语句拼凑的全部内容,更多相关Java数据库语句拼凑内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复