提交 | 用户 | 时间
|
a6de49
|
1 |
/** |
H |
2 |
* Copyright (c) 2018 人人开源 All rights reserved. |
|
3 |
* |
|
4 |
* https://www.renren.io |
|
5 |
* |
|
6 |
* 版权所有,侵权必究! |
|
7 |
*/ |
|
8 |
|
|
9 |
package com.iailab.framework.common.service.impl; |
|
10 |
|
|
11 |
import com.baomidou.mybatisplus.core.conditions.Wrapper; |
|
12 |
import com.baomidou.mybatisplus.core.enums.SqlMethod; |
|
13 |
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
|
14 |
import com.baomidou.mybatisplus.core.metadata.IPage; |
|
15 |
import com.baomidou.mybatisplus.core.metadata.OrderItem; |
|
16 |
import com.baomidou.mybatisplus.core.toolkit.Constants; |
|
17 |
import com.baomidou.mybatisplus.core.toolkit.ReflectionKit; |
|
18 |
import com.baomidou.mybatisplus.core.toolkit.StringUtils; |
|
19 |
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
|
20 |
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper; |
|
21 |
import com.iailab.framework.common.constant.Constant; |
|
22 |
import com.iailab.framework.common.page.PageData; |
|
23 |
import com.iailab.framework.common.service.BaseService; |
|
24 |
import com.iailab.framework.common.util.object.BeanUtils; |
|
25 |
import org.apache.ibatis.binding.MapperMethod; |
|
26 |
import org.apache.ibatis.logging.Log; |
|
27 |
import org.apache.ibatis.logging.LogFactory; |
|
28 |
import org.apache.ibatis.session.SqlSession; |
|
29 |
import org.springframework.beans.factory.annotation.Autowired; |
|
30 |
import org.springframework.transaction.annotation.Transactional; |
|
31 |
|
|
32 |
import java.io.Serializable; |
|
33 |
import java.util.Collection; |
|
34 |
import java.util.List; |
|
35 |
import java.util.Map; |
|
36 |
import java.util.function.BiConsumer; |
|
37 |
|
|
38 |
/** |
|
39 |
* 基础服务类,所有Service都要继承 |
|
40 |
* |
|
41 |
* @author Mark sunlightcs@gmail.com |
|
42 |
*/ |
|
43 |
public abstract class BaseServiceImpl<M extends BaseMapper<T>, T> implements BaseService<T> { |
|
44 |
@Autowired |
|
45 |
protected M baseDao; |
|
46 |
protected Log log = LogFactory.getLog(getClass()); |
|
47 |
|
|
48 |
/** |
|
49 |
* 获取分页对象 |
|
50 |
* @param params 分页查询参数 |
|
51 |
* @param defaultOrderField 默认排序字段 |
|
52 |
* @param isAsc 排序方式 |
|
53 |
*/ |
|
54 |
protected IPage<T> getPage(Map<String, Object> params, String defaultOrderField, boolean isAsc) { |
|
55 |
//分页参数 |
|
56 |
long curPage = 1; |
|
57 |
long limit = 10; |
|
58 |
|
|
59 |
if(params.get(Constant.PAGE) != null){ |
|
60 |
curPage = Long.parseLong((String)params.get(Constant.PAGE)); |
|
61 |
} |
|
62 |
if(params.get(Constant.LIMIT) != null){ |
|
63 |
limit = Long.parseLong((String)params.get(Constant.LIMIT)); |
|
64 |
} |
|
65 |
|
|
66 |
//分页对象 |
|
67 |
Page<T> page = new Page<>(curPage, limit); |
|
68 |
|
|
69 |
//分页参数 |
|
70 |
params.put(Constant.PAGE, page); |
|
71 |
|
|
72 |
//排序字段 |
|
73 |
String orderField = (String)params.get(Constant.ORDER_FIELD); |
|
74 |
String order = (String)params.get(Constant.ORDER); |
|
75 |
|
|
76 |
//前端字段排序 |
|
77 |
if(StringUtils.isNotBlank(orderField) && StringUtils.isNotBlank(order)){ |
|
78 |
if(Constant.ASC.equalsIgnoreCase(order)) { |
|
79 |
return page.addOrder(OrderItem.asc(orderField)); |
|
80 |
}else { |
|
81 |
return page.addOrder(OrderItem.desc(orderField)); |
|
82 |
} |
|
83 |
} |
|
84 |
|
|
85 |
//没有排序字段,则不排序 |
|
86 |
if(StringUtils.isBlank(defaultOrderField)){ |
|
87 |
return page; |
|
88 |
} |
|
89 |
|
|
90 |
//默认排序 |
|
91 |
if(isAsc) { |
|
92 |
page.addOrder(OrderItem.asc(defaultOrderField)); |
|
93 |
}else { |
|
94 |
page.addOrder(OrderItem.desc(defaultOrderField)); |
|
95 |
} |
|
96 |
|
|
97 |
return page; |
|
98 |
} |
|
99 |
|
|
100 |
protected <T> PageData<T> getPageData(List<?> list, long total, Class<T> target){ |
|
101 |
List<T> targetList = BeanUtils.toBean(list, target); |
|
102 |
|
|
103 |
return new PageData<>(targetList, total); |
|
104 |
} |
|
105 |
|
|
106 |
protected <T> PageData<T> getPageData(IPage page, Class<T> target){ |
|
107 |
return getPageData(page.getRecords(), page.getTotal(), target); |
|
108 |
} |
|
109 |
|
|
110 |
protected void paramsToLike(Map<String, Object> params, String... likes){ |
|
111 |
for (String like : likes){ |
|
112 |
String val = (String)params.get(like); |
|
113 |
if (StringUtils.isNotBlank(val)){ |
|
114 |
params.put(like, "%" + val + "%"); |
|
115 |
}else { |
|
116 |
params.put(like, null); |
|
117 |
} |
|
118 |
} |
|
119 |
} |
|
120 |
|
|
121 |
/** |
|
122 |
* <p> |
|
123 |
* 判断数据库操作是否成功 |
|
124 |
* </p> |
|
125 |
* <p> |
|
126 |
* 注意!! 该方法为 Integer 判断,不可传入 int 基本类型 |
|
127 |
* </p> |
|
128 |
* |
|
129 |
* @param result 数据库操作返回影响条数 |
|
130 |
* @return boolean |
|
131 |
*/ |
|
132 |
protected static boolean retBool(Integer result) { |
|
133 |
return SqlHelper.retBool(result); |
|
134 |
} |
|
135 |
|
|
136 |
protected Class<M> currentMapperClass() { |
|
137 |
return (Class<M>) ReflectionKit.getSuperClassGenericType(this.getClass(), BaseServiceImpl.class, 0); |
|
138 |
} |
|
139 |
|
|
140 |
@Override |
|
141 |
public Class<T> currentModelClass() { |
|
142 |
return (Class<T>)ReflectionKit.getSuperClassGenericType(this.getClass(), BaseServiceImpl.class, 1); |
|
143 |
} |
|
144 |
|
|
145 |
protected String getSqlStatement(SqlMethod sqlMethod) { |
|
146 |
return SqlHelper.getSqlStatement(this.currentMapperClass(), sqlMethod); |
|
147 |
} |
|
148 |
|
|
149 |
@Override |
|
150 |
public boolean insert(T entity) { |
|
151 |
return BaseServiceImpl.retBool(baseDao.insert(entity)); |
|
152 |
} |
|
153 |
|
|
154 |
@Override |
|
155 |
@Transactional(rollbackFor = Exception.class) |
|
156 |
public boolean insertBatch(Collection<T> entityList) { |
|
157 |
return insertBatch(entityList, 100); |
|
158 |
} |
|
159 |
|
|
160 |
/** |
|
161 |
* 批量插入 |
|
162 |
*/ |
|
163 |
@Override |
|
164 |
@Transactional(rollbackFor = Exception.class) |
|
165 |
public boolean insertBatch(Collection<T> entityList, int batchSize) { |
|
166 |
String sqlStatement = getSqlStatement(SqlMethod.INSERT_ONE); |
|
167 |
return executeBatch(entityList, batchSize, (sqlSession, entity) -> sqlSession.insert(sqlStatement, entity)); |
|
168 |
} |
|
169 |
|
|
170 |
/** |
|
171 |
* 执行批量操作 |
|
172 |
*/ |
|
173 |
protected <E> boolean executeBatch(Collection<E> list, int batchSize, BiConsumer<SqlSession, E> consumer) { |
|
174 |
return SqlHelper.executeBatch(this.currentModelClass(), this.log, list, batchSize, consumer); |
|
175 |
} |
|
176 |
|
|
177 |
|
|
178 |
@Override |
|
179 |
public boolean updateById(T entity) { |
|
180 |
return BaseServiceImpl.retBool(baseDao.updateById(entity)); |
|
181 |
} |
|
182 |
|
|
183 |
@Override |
|
184 |
public boolean update(T entity, Wrapper<T> updateWrapper) { |
|
185 |
return BaseServiceImpl.retBool(baseDao.update(entity, updateWrapper)); |
|
186 |
} |
|
187 |
|
|
188 |
@Override |
|
189 |
@Transactional(rollbackFor = Exception.class) |
|
190 |
public boolean updateBatchById(Collection<T> entityList) { |
|
191 |
return updateBatchById(entityList, 30); |
|
192 |
} |
|
193 |
|
|
194 |
@Override |
|
195 |
@Transactional(rollbackFor = Exception.class) |
|
196 |
public boolean updateBatchById(Collection<T> entityList, int batchSize) { |
|
197 |
String sqlStatement = getSqlStatement(SqlMethod.UPDATE_BY_ID); |
|
198 |
return executeBatch(entityList, batchSize, (sqlSession, entity) -> { |
|
199 |
MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>(); |
|
200 |
param.put(Constants.ENTITY, entity); |
|
201 |
sqlSession.update(sqlStatement, param); |
|
202 |
}); |
|
203 |
} |
|
204 |
|
|
205 |
@Override |
|
206 |
public T selectById(Serializable id) { |
|
207 |
return baseDao.selectById(id); |
|
208 |
} |
|
209 |
|
|
210 |
@Override |
|
211 |
public boolean deleteById(Serializable id) { |
|
212 |
return SqlHelper.retBool(baseDao.deleteById(id)); |
|
213 |
} |
|
214 |
|
|
215 |
@Override |
|
216 |
public boolean deleteBatchIds(Collection<? extends Serializable> idList) { |
|
217 |
return SqlHelper.retBool(baseDao.deleteBatchIds(idList)); |
|
218 |
} |
|
219 |
} |