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