ServiceImpl类是我们进行SQL操作中非常重要的一个类,通过MybatisPlus生成的各个实体类的XXXImpl都会继承ServiceImpl类那里继承全部的方法,那么ServiceImpl类中有哪些方法呢?如下介绍:
复制代码
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/** * IService 实现类( 泛型:M 是 mapper 对象,T 是实体 ) * * @author hubin * @since 2018-06-23 */ @SuppressWarnings("unchecked") public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> { protected Log log = LogFactory.getLog(getClass()); @Autowired protected M baseMapper; @Override public M getBaseMapper() { return baseMapper; } protected Class<T> entityClass = currentModelClass(); @Override public Class<T> getEntityClass() { return entityClass; } protected Class<T> mapperClass = currentMapperClass(); /** * 判断数据库操作是否成功 * * @param result 数据库操作返回影响条数 * @return boolean * @deprecated 3.3.1 */ @Deprecated protected boolean retBool(Integer result) { return SqlHelper.retBool(result); } protected Class<T> currentMapperClass() { return (Class<T>) ReflectionKit.getSuperClassGenericType(getClass(), 0); } protected Class<T> currentModelClass() { return (Class<T>) ReflectionKit.getSuperClassGenericType(getClass(), 1); } /** * 批量操作 SqlSession * * @deprecated 3.3.0 */ @Deprecated protected SqlSession sqlSessionBatch() { return SqlHelper.sqlSessionBatch(entityClass); } /** * 释放sqlSession * * @param sqlSession session * @deprecated 3.3.0 */ @Deprecated protected void closeSqlSession(SqlSession sqlSession) { SqlSessionUtils.closeSqlSession(sqlSession, GlobalConfigUtils.currentSessionFactory(entityClass)); } /** * 获取 SqlStatement * * @param sqlMethod ignore * @return ignore * @see #getSqlStatement(SqlMethod) * @deprecated 3.4.0 */ @Deprecated protected String sqlStatement(SqlMethod sqlMethod) { return SqlHelper.table(entityClass).getSqlStatement(sqlMethod.getMethod()); } /** * 批量插入 * * @param entityList ignore * @param batchSize ignore * @return ignore */ @Transactional(rollbackFor = Exception.class) @Override public boolean saveBatch(Collection<T> entityList, int batchSize) { String sqlStatement = getSqlStatement(SqlMethod.INSERT_ONE); return executeBatch(entityList, batchSize, (sqlSession, entity) -> sqlSession.insert(sqlStatement, entity)); } /** * 获取mapperStatementId * * @param sqlMethod 方法名 * @return 命名id * @since 3.4.0 */ protected String getSqlStatement(SqlMethod sqlMethod) { return SqlHelper.getSqlStatement(mapperClass, sqlMethod); } /** * TableId 注解存在更新记录,否插入一条记录 * * @param entity 实体对象 * @return boolean */ @Transactional(rollbackFor = Exception.class) @Override public boolean saveOrUpdate(T entity) { if (null != entity) { TableInfo tableInfo = TableInfoHelper.getTableInfo(this.entityClass); Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!"); String keyProperty = tableInfo.getKeyProperty(); Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!"); Object idVal = ReflectionKit.getFieldValue(entity, tableInfo.getKeyProperty()); return StringUtils.checkValNull(idVal) || Objects.isNull(getById((Serializable) idVal)) ? save(entity) : updateById(entity); } return false; } @Transactional(rollbackFor = Exception.class) @Override public boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize) { TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass); Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!"); String keyProperty = tableInfo.getKeyProperty(); Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!"); return SqlHelper.saveOrUpdateBatch(this.entityClass, this.mapperClass, this.log, entityList, batchSize, (sqlSession, entity) -> { Object idVal = ReflectionKit.getFieldValue(entity, keyProperty); return StringUtils.checkValNull(idVal) || CollectionUtils.isEmpty(sqlSession.selectList(getSqlStatement(SqlMethod.SELECT_BY_ID), entity)); }, (sqlSession, entity) -> { MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>(); param.put(Constants.ENTITY, entity); sqlSession.update(getSqlStatement(SqlMethod.UPDATE_BY_ID), param); }); } @Transactional(rollbackFor = Exception.class) @Override public boolean updateBatchById(Collection<T> entityList, int batchSize) { String sqlStatement = getSqlStatement(SqlMethod.UPDATE_BY_ID); return executeBatch(entityList, batchSize, (sqlSession, entity) -> { MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>(); param.put(Constants.ENTITY, entity); sqlSession.update(sqlStatement, param); }); } @Override public T getOne(Wrapper<T> queryWrapper, boolean throwEx) { if (throwEx) { return baseMapper.selectOne(queryWrapper); } return SqlHelper.getObject(log, baseMapper.selectList(queryWrapper)); } @Override public Map<String, Object> getMap(Wrapper<T> queryWrapper) { return SqlHelper.getObject(log, baseMapper.selectMaps(queryWrapper)); } @Override public <V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper) { return SqlHelper.getObject(log, listObjs(queryWrapper, mapper)); } /** * 执行批量操作 * * @param consumer consumer * @since 3.3.0 * @deprecated 3.3.1 后面我打算移除掉 {@link #executeBatch(Collection, int, BiConsumer)} }. */ @Deprecated protected boolean executeBatch(Consumer<SqlSession> consumer) { return SqlHelper.executeBatch(this.entityClass, this.log, consumer); } /** * 执行批量操作 * * @param list 数据集合 * @param batchSize 批量大小 * @param consumer 执行方法 * @param <E> 泛型 * @return 操作结果 * @since 3.3.1 */ protected <E> boolean executeBatch(Collection<E> list, int batchSize, BiConsumer<SqlSession, E> consumer) { return SqlHelper.executeBatch(this.entityClass, this.log, list, batchSize, consumer); } /** * 执行批量操作(默认批次提交数量{@link IService#DEFAULT_BATCH_SIZE}) * * @param list 数据集合 * @param consumer 执行方法 * @param <E> 泛型 * @return 操作结果 * @since 3.3.1 */ protected <E> boolean executeBatch(Collection<E> list, BiConsumer<SqlSession, E> consumer) { return executeBatch(list, DEFAULT_BATCH_SIZE, consumer); } }
ServiceImpl类各方法(未过期)的作用
- getBaseMapper()
- getEntityClass()
- saveBatch()
- saveOrUpdate()
- saveOrUpdateBatch()
- updateBatchById()
- getOne()
- getMap()
- getObj()
ServiceImpl类各属性的作用
- log:打印日志
- baseMapper:实现了许多的SQL操作
- entityClass:实体类
- mapperClass:映射类
BaseMapper类中各方法
ServiceImpl类中有这个类的成员变量,因此通过ServiceImpl这个类便能够操作如下方法:
- int insert(T entity);:插入记录
- int deleteById(Serializable id);:通过id删除指定记录
- int deleteByMap(Map<String, Object> columnMap):通过Map集合添加删除指定记录
- int delete(@Param(Constants.WRAPPER) Wrapper queryWrapper):通过添加构造器删除指定记录
- int deleteBatchIds(Collection<? extends Serializable> idList):通过List集合批量删除记录
- int updateById(T entity):根据id修改指定记录
- int update(T entity, Wrapper updateWrapper):根据条件构造器
- T selectById(Serializable id):根据id查询指定记录
- List selectBatchIds(Collection<? extends Serializable> idList):根据List集合批量查询记录
- List selectByMap(Map<String, Object> columnMap):根据Map集合查询记录
- T selectOne(Wrapper queryWrapper):根据条件构造器查询一条记录
- Integer selectCount(Wrapper queryWrapper):根据条件构造器查询记录总数
- List selectList(Wrapper queryWrapper):根据条件构造器查询全部记录
- List<Map<String, Object>> selectMaps(Wrapper queryWrapper):根据条件构造器查询全部记录
- ist selectObjs(Wrapper queryWrapper):根据条件构造器查询全部记录
- <E extends IPage> E selectPage(E page, Wrapper queryWrapper):根据条件构造器查询全部记录(并翻页)
- <E extends IPage<Map<String, Object>>> E selectMapsPage(E page, Wrapper queryWrapper):根据条件构造器查询全部记录(并翻页)
Wrapper类各方法
- getEntity():实体对象(子类实现)
- getSqlSelectgetSqlSet():
- getSqlComment():
- getSqlFirst():
- getExpression():获取 MergeSegments
- getCustomSqlSegment():获取自定义SQL 简化自定义XML复杂情况
- isEmptyOfWhere():查询条件为空(包含entity)
- nonEmptyOfWhere():查询条件不为空(包含entity)
- isEmptyOfNormal():查询条件为空(不包含entity)
- nonEmptyOfNormal():查询条件为空(不包含entity)
- nonEmptyOfEntity():深层实体判断属性
- fieldStrategyMatch():根据实体FieldStrategy属性来决定判断逻辑
- isEmptyOfEntity():深层实体判断属性
- getTargetSql():获取格式化后的执行sql
- clear():条件清空
实例说明
复制代码
1
2
3
4public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {} public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {}
在ServiceImpl中已经注入了Mapper对象: protected M baseMapper;因此XXXServiceImpl只要继承了这个原生的ServiceImpl,这个M实体Dao就已经注入了进来,不需要重新注入。
最后
以上就是追寻小馒头最近收集整理的关于MybatisPlus生成器ServiceImpl类详解的全部内容,更多相关MybatisPlus生成器ServiceImpl类详解内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复