提交 | 用户 | age
|
5c5945
|
1 |
package com.hx.mybatisTool; |
E |
2 |
|
|
3 |
import com.hx.exception.TipsException; |
9338de
|
4 |
import com.hx.util.SimpleTool; |
5c5945
|
5 |
import com.hx.util.StringUtils; |
E |
6 |
|
|
7 |
import java.util.Map; |
|
8 |
|
|
9 |
/** |
|
10 |
* mybatis 自定义处理sql语句 |
|
11 |
* @author chenjiahe |
|
12 |
* @Data: 2020-06-08 |
|
13 |
*/ |
|
14 |
public class SqlSentence { |
|
15 |
|
|
16 |
private String sqlSentence; |
|
17 |
|
|
18 |
private Map<String,Object> m; |
|
19 |
/**排序*/ |
|
20 |
private String orderBy; |
|
21 |
/**开始页数*/ |
|
22 |
private Integer startPage = 0; |
|
23 |
/**每页数量*/ |
|
24 |
private Integer pageNum = 0; |
|
25 |
|
|
26 |
////////////////////////////////////////////////////////////// |
|
27 |
|
|
28 |
/********************mother****************************/ |
|
29 |
|
|
30 |
public SqlSentence() |
|
31 |
{ |
|
32 |
|
|
33 |
} |
|
34 |
|
|
35 |
public SqlSentence(Class c, String where, Map<String, Object> values) |
|
36 |
{ |
|
37 |
if(StringUtils.isEmpty(where)) |
|
38 |
{ |
|
39 |
throw new TipsException("sql is null"); |
|
40 |
} |
|
41 |
|
|
42 |
if(values == null) |
|
43 |
{ |
|
44 |
throw new TipsException("values is null"); |
|
45 |
} |
|
46 |
|
|
47 |
sqlSentence = "select * from " + c.getSimpleName()+ " where " + where; |
|
48 |
m = values; |
|
49 |
} |
|
50 |
|
|
51 |
public SqlSentence(String where, Map<String, Object> values) |
|
52 |
{ |
|
53 |
if(StringUtils.isEmpty(where)) |
|
54 |
{ |
|
55 |
throw new TipsException("sql is null"); |
|
56 |
} |
|
57 |
|
|
58 |
if(values == null) |
|
59 |
{ |
|
60 |
throw new TipsException("values is null"); |
|
61 |
} |
|
62 |
|
|
63 |
sqlSentence = where; |
|
64 |
m = values; |
|
65 |
} |
|
66 |
|
|
67 |
|
|
68 |
/** |
|
69 |
* sql整条语句 |
|
70 |
* @param sql 如:select * from user Where name = #{m.userName} order by age desc |
|
71 |
* @param values 存放的值如:values.put("userName","ChenJiaHe") |
|
72 |
*/ |
|
73 |
public void sqlSentence(String sql,Map<String,Object> values) { |
|
74 |
if(StringUtils.isEmpty(sql)) |
|
75 |
{ |
|
76 |
throw new TipsException("sql is null"); |
|
77 |
} |
|
78 |
|
|
79 |
if(values == null) |
|
80 |
{ |
|
81 |
throw new TipsException("values is null"); |
|
82 |
} |
|
83 |
|
|
84 |
sqlSentence = sql; |
|
85 |
m = values; |
|
86 |
} |
|
87 |
|
9338de
|
88 |
/** |
C |
89 |
* 查询的语句 |
|
90 |
* @param sql 如:id = #{m.userId} order by age DESC |
|
91 |
* @param values 存放的值如:values.put("userId","123456") |
|
92 |
*/ |
|
93 |
public void sqlWhere(String sql,Map<String,Object> values) { |
|
94 |
if(!SimpleTool.checkNotNull(values)){ |
|
95 |
throw new TipsException("values is null"); |
|
96 |
} |
|
97 |
if(!SimpleTool.checkNotNull(sql)) { |
|
98 |
sql = "1=1"; |
|
99 |
} |
|
100 |
sqlSentence = sql; |
|
101 |
m = values; |
|
102 |
} |
|
103 |
|
|
104 |
/** |
|
105 |
* 更新语句的语句 |
|
106 |
* @param sql 如:name = #{m.name},age = ? WHERE id = #{m.id} |
|
107 |
* @param values 存放的值 |
|
108 |
*/ |
|
109 |
public void sqlUpdate(String sql,Map<String,Object> values) { |
|
110 |
if(!SimpleTool.checkNotNull(values)){ |
|
111 |
throw new TipsException("values is null"); |
|
112 |
} |
|
113 |
m = values; |
|
114 |
sqlSentence = sql; |
|
115 |
} |
|
116 |
|
5c5945
|
117 |
|
E |
118 |
/************************************************************************/ |
|
119 |
|
|
120 |
public Map<String, Object> getM() { |
|
121 |
return m; |
|
122 |
} |
|
123 |
|
|
124 |
public void setM(Map<String, Object> m) { |
|
125 |
this.m = m; |
|
126 |
} |
|
127 |
|
|
128 |
public String getSqlSentence() { |
|
129 |
return sqlSentence; |
|
130 |
} |
|
131 |
|
|
132 |
public void setSqlSentence(String sqlSentence) { |
|
133 |
this.sqlSentence = sqlSentence; |
|
134 |
} |
|
135 |
|
|
136 |
public String getOrderBy() { |
|
137 |
return orderBy; |
|
138 |
} |
|
139 |
|
|
140 |
public void setOrderBy(String orderBy) { |
|
141 |
this.orderBy = orderBy; |
|
142 |
} |
|
143 |
|
|
144 |
public Integer getStartPage() { |
|
145 |
return startPage; |
|
146 |
} |
|
147 |
|
|
148 |
public void setStartPage(Integer startPage) { |
|
149 |
this.startPage = startPage; |
|
150 |
} |
|
151 |
|
|
152 |
public Integer getPageNum() { |
|
153 |
return pageNum; |
|
154 |
} |
|
155 |
|
|
156 |
public void setPageNum(Integer pageNum) { |
|
157 |
this.pageNum = pageNum; |
|
158 |
} |
|
159 |
|
|
160 |
public Integer getStartIndex() |
|
161 |
{ |
|
162 |
return (startPage - 1) * pageNum; |
|
163 |
} |
|
164 |
} |
|
165 |
|
|
166 |
|