BaseDaoImpl.java
13.3 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package com.bjivt.base.dao.impl;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.jdbc.SqlRunner;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import com.bjivt.base.bean.BeanMapUtil;
import com.bjivt.base.dao.BaseDao;
import com.bjivt.base.reflect.ReflectGeneric;
@SuppressWarnings("unchecked")
public class BaseDaoImpl<T,ID extends Serializable> extends SqlSessionDaoSupport implements BaseDao<T, ID>{
/**
* @Title: BaseDaoImpl.java
* @Package com.enla.base
* @Description: 基础
* @author JASON
* @date 2015-12-1
*/
public static final String SQLNAME_SEPARATOR=".";
public static final String SQL_SAVE="save";
public static final String SQL_INSERTSELECTIVE="insertSelective";
public static final String SQL_UPDATE="update";
public static final String SQL_UPDATEBYIDSELECTIVE="updateByIdSelective";
public static final String SQL_FINDBYID="findById";
public static final String SQL_DELETEBYID="deleteById";
public static final String SQL_DELETEBYIDS="deleteByIds";
public static final String SQL_FINDLISTBY="findListBy";
public static final String SQL_FINDLISTBYMAP="findListByMap";
public static final String SQL_FINDCOUNTBY="findCountBy";
public static final String SQL_SELECTBYID="selectById";
public static final String SQL_GETCOUNTBY="getCountBy";
private static final String SORT_NAME = "SORT";
private static final String DIR_NAME = "DIR";
/** 不能用于SQL中的非法字符(主要用于排序字段名) */
public static final String[] ILLEGAL_CHARS_FOR_SQL = {",", ";", " ", "\"", "%"};
private Class<T> clazz;
/**
* spring-mabatis1.2.2已经将sqlSessionFactory的自动注入移除掉了
*/
@Autowired
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
super.setSqlSessionFactory(sqlSessionFactory);
}
/**
* 功能说明:获取默认SqlMapping命名空间
* <br/>使用泛型参数中业务实体类型的全限定名作为默认的命名空间。
* <br/>如果实际应用中需要特殊的命名空间,可由子类重写该方法实现自己的命名空间规则。
* @author jason.chen
* @updated
* @return 返回命名空间字符串
*/
protected String getDefaultSqlNamespace() {
clazz= ReflectGeneric.getClassGenricType(this.getClass());
return clazz.getName();
}
/**
* 获取 SqlMapping 的命名空间
*/
private String sqlNamespace = getDefaultSqlNamespace();
/**
* 功能说明:将SqlMapping命名空间与给定的SqlMapping名组合在一起。
* @author jason.chen
* @updated
* @param sqlName SqlMapping名
* @return 组合了SqlMapping命名空间后的完整SqlMapping名
*/
protected String getSqlName(String sqlName) {
return sqlNamespace+ SQLNAME_SEPARATOR + sqlName;
}
/**
* 功能说明:获取 SqlMapping 的命名空间
* @author jason.chen
* @updated
* @return SqlMapping命名空间
*/
public String getSqlNamespace() {
return sqlNamespace;
}
/**
* 功能说明:设置SqlMapping命名空间。
* 此方法只用于注入SqlMapping命名空间,以改变默认的SqlMapping命名空间,
* 不能滥用此方法随意改变SqlMapping命名空间。
* @author jason.chen
* @updated
* @param sqlNamespace 要设置的sqlMapping命名空间的名称
*/
public void setSqlNamespace(String sqlNamespace) {
this.sqlNamespace = sqlNamespace;
}
/**
* 功能说明:生成主键值。
* 默认情况下什么也不做;
* 如果需要生成主键,需要由子类重写此方法根据需要的方式生成主键值。
* @author jason.chen
* @updated
* @param item 要持久化的对象
*/
protected void generateId(T item) {
}
public Integer insertSelective(T item) {
generateId(item);
return this.getSqlSession().insert(getSqlName(SQL_INSERTSELECTIVE),item);
}
public Integer save(T item) {
generateId(item);
return this.getSqlSession().insert(getSqlName(SQL_SAVE),item);
}
/**
* 功能说明:将statement包装了命名空间,方便DAO子类调用。
* @author jason.chen
* @updated
* @param keyId 映射的语句ID
* @param parameter 参数
* @return 执行结果——插入成功的记录数
*/
public Integer save(String keyId, Object parameter) {
return this.getSqlSession().insert(getSqlName(keyId), parameter);
}
/**
* 功能说明:将statement包装了命名空间,方便DAO子类调用。
* @author jason.chen
* @updated
* @param keyId 映射的语句ID
* @return 执行结果——插入成功的记录数
*/
protected Integer save(String keyId) {
return this.getSqlSession().insert(getSqlName(keyId));
}
/**
* 功能说明:使用update方法更新
* @author jason.chen
* @updated
* @return 执行结果——插入成功的记录数
*/
public Integer update(T item) {
return this.getSqlSession().update(getSqlName(SQL_UPDATE), item);
}
/**
* 功能说明:使用updateByIdSelective方法更新--
* @author jason.chen
* @updated
* @return 执行结果——插入成功的记录数
*/
public Integer updateByIdSelective(T item) {
return this.getSqlSession().update(getSqlName(SQL_UPDATEBYIDSELECTIVE), item);
}
/**
* 功能说明:将statement包装了命名空间,方便DAO子类调用
* @author jason.chen
* @updated
* @param keyId 映射的语句ID
* @param parameter 参数
* @return 执行结果——更新成功的记录数
*/
public Integer update(String keyId, Object parameter) {
return this.getSqlSession().update(getSqlName(keyId), parameter);
}
/**
* 功能说明:将statement包装了命名空间,方便DAO子类调用。
* @author jason.chen
* @updated
* @param keyId 映射的语句ID
* @return 执行结果——更新成功的记录数
*/
protected int update(String keyId) {
return this.getSqlSession().update(getSqlName(keyId));
}
public T findById(ID id) {
return (T)this.getSqlSession().selectOne(getSqlName(SQL_FINDBYID), id);
}
public T findBy(String keyId,Object parameter) {
return (T)this.getSqlSession().selectOne(getSqlName(keyId), parameter);
}
public Map<String,Object> findMapBy(String namespace,String keyId,Map<String,Object> map){
return this.getSqlSession().selectOne(namespace+"."+keyId, map);
}
public Integer deleteById(ID id) {
return this.getSqlSession().delete(getSqlName(SQL_DELETEBYID), id);
}
/**
* 功能说明:将statement包装了命名空间,方便DAO子类调用。
* @author jason.chen
* @updated
* @param keyId 映射的语句ID
* @param parameter 参数
* @return 执行结果——删除成功的记录数
*/
public int deleteBy(String keyId, Object parameter) {
return this.getSqlSession().delete(getSqlName(keyId), parameter);
}
/**
* 功能说明:将statement包装了命名空间,方便DAO子类调用。
* @author jason.chen
* @updated
* @param keyId 映射的语句ID
* @return
*/
protected int delete(String statement) {
return this.getSqlSession().delete(getSqlName(statement));
}
public Integer deleteByIds(ID[] ids){
return this.getSqlSession().delete(getSqlName(SQL_DELETEBYIDS), ids);
}
/**
* 功能说明:从给定字符串中将指定的非法字符串数组中各字符串过滤掉。
* @author jason.chen
* @updated
* @param str 待过滤的字符串
* @param filterChars 指定的非法字符串数组
* @return 过滤后的字符串
*/
protected String filterIllegalChars(String str, String[] filterChars) {
String rs = str;
if (rs != null && filterChars != null) {
for (String fc : filterChars) {
if (fc != null && fc.length() > 0) {
str = str.replaceAll(fc, "");
}
}
}
return rs;
}
public Integer findCountBy(T item) {
return this.getSqlSession().selectOne(getSqlName(SQL_GETCOUNTBY), item);
}
protected List<T> selectList(String keyId, Object parameter, RowBounds rowBounds) {
return this.getSqlSession().selectList(getSqlName(keyId), parameter, rowBounds);
}
public List<T> findListBy(T item, String sortColumn, String des) {
Map<String,Object> paramMap=new HashMap<String,Object>();
try{
if(item!=null){
paramMap = BeanMapUtil.bean2Map(item);
}
}catch(Exception e){
throw new RuntimeException("获取参数失败", e);
}
if (sortColumn != null) {
// 排序字段不为空,过滤其中可能存在的非法字符
sortColumn = filterIllegalChars(sortColumn, ILLEGAL_CHARS_FOR_SQL);
}
if (StringUtils.isNotEmpty(sortColumn) && StringUtils.isNotEmpty(des)) {
paramMap.put(SORT_NAME, sortColumn);
paramMap.put(DIR_NAME, des);
}
return this.getSqlSession().selectList(getSqlName(SQL_FINDLISTBY), paramMap);
}
public List<T> selectList(String keyId, Object parameter) {
return this.getSqlSession().selectList(getSqlName(keyId), parameter);
}
public List<Map<String,Object>> selectListMap(String keyId, Object parameter) {
return this.getSqlSession().selectList(getSqlName(keyId), parameter);
}
protected List<T> selectList(String keyId) {
return this.getSqlSession().selectList(getSqlName(keyId));
}
protected Object selectOne(String keyId, Object parameter) {
return this.getSqlSession().selectOne(getSqlName(keyId), parameter);
}
protected Object selectOne(String keyId) {
return this.getSqlSession().selectOne(getSqlName(keyId));
}
protected Map<String,Object> selectMap(String keyId, Object parameter, String mapKey,RowBounds rowBounds) {
return this.getSqlSession().selectMap(getSqlName(keyId),parameter, mapKey, rowBounds);
}
/**
* 功能说明:
* @author jason.chen
* @updated
* @param keyId 映射的语句ID
* @param parameter 参数
* @param mapKey 数据mapKey
* @return
*/
protected Map<String,Object> selectMap(String keyId, Object parameter, String mapKey) {
return this.getSqlSession().selectMap(getSqlName(keyId), parameter, mapKey);
}
protected void select(String keyId, Object parameter, RowBounds rowBounds,
ResultHandler handler) {
this.getSqlSession().select(getSqlName(keyId),parameter, rowBounds, handler);
}
protected void select(String keyId, Object parameter, ResultHandler handler) {
this.getSqlSession().select(getSqlName(keyId), parameter, handler);
}
protected void select(String keyId, ResultHandler handler) {
this.getSqlSession().select(getSqlName(keyId), handler);
}
public List<T> findListBy(T item){
return findListBy(item, null, null);
}
public List<T> findListByMap(Map<String,Object> map){
return this.getSqlSession().selectList(getSqlName(SQL_FINDLISTBYMAP), map);
}
public List<T> findListBy(String namespace,String keyId,T item){
return findListBy(namespace,keyId,item,null,null);
}
public List<T> findListBy(String namespace,String keyId,T item,String sortColumn,String des){
Map<String,Object> paramMap=new HashMap<String,Object>();
try{
if(item!=null){
paramMap = BeanMapUtil.bean2Map(item);
}
}catch(Exception e){
throw new RuntimeException("获取参数失败", e);
}
if (sortColumn != null) {
// 排序字段不为空,过滤其中可能存在的非法字符
sortColumn = filterIllegalChars(sortColumn, ILLEGAL_CHARS_FOR_SQL);
}
if (StringUtils.isNotEmpty(sortColumn) && StringUtils.isNotEmpty(des)) {
paramMap.put(SORT_NAME, sortColumn);
paramMap.put(DIR_NAME, des);
}
return this.getSqlSession().selectList(namespace+"."+keyId, paramMap);
}
public List<Map<String,Object>> findListMapBy(String namespace,String keyId,Map<String,Object> map){
return findListMapBy(namespace,keyId,map,null,null);
}
public List<Map<String,Object>> findListMapBy(String namespace,String keyId,Map<String,Object> map,String sortColumn,String des){
Map<String,Object> paramMap=new HashMap<String,Object>();
if(map!=null&&map.size()>0){
paramMap.putAll(map);
}
if (sortColumn != null) {
// 排序字段不为空,过滤其中可能存在的非法字符
sortColumn = filterIllegalChars(sortColumn, ILLEGAL_CHARS_FOR_SQL);
}
if (StringUtils.isNotEmpty(sortColumn) && StringUtils.isNotEmpty(des)) {
paramMap.put(SORT_NAME, sortColumn);
paramMap.put(DIR_NAME, des);
}
return this.getSqlSession().selectList(namespace+"."+keyId, paramMap);
}
public List<T> findList() {
return findListBy(null);
}
public List<Map<String,Object>> findListBySql(String sql,Object... obj) throws SQLException{
SqlRunner sr=new SqlRunner(getSqlSession().getConnection());
return sr.selectAll(sql, obj);
}
public Map<String,Object> findMapBySql(String sql,Object... obj )throws SQLException{
SqlRunner sr=new SqlRunner(getSqlSession().getConnection());
return sr.selectOne(sql, obj);
}
public Integer updateBySql(String sql,Object... obj)throws SQLException{
SqlRunner sr=new SqlRunner(getSqlSession().getConnection());
return sr.update(sql, obj);
}
public Integer deleteBySql(String sql,Object... obj)throws SQLException{
SqlRunner sr=new SqlRunner(getSqlSession().getConnection());
return sr.delete(sql, obj);
}
@Override
public T selectById(ID id) {
return (T)this.getSqlSession().selectOne(getSqlName(SQL_SELECTBYID), id);
}
@Override
public Integer getCountBy(T item) {
return this.getSqlSession().selectOne(getSqlName(SQL_GETCOUNTBY), item);
}
}