ServiceImpl類是我們進行SQL操作中非常重要的一個類,通過MyBatisPlus生成的各個實體類的XXXImpl都會繼承ServiceImpl類那里繼承全部的方法,那么ServiceImpl類中有哪些方法呢?如下介紹:
/** * 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類各方法(未過期)的作用
1.getBaseMapper()
2.getEntityClass()
3.saveBatch()
4.saveOrUpdate()
5.saveOrUpdateBatch()
6.updateBatchById()
7.getOne()
8.getMap()
9.getObj()
ServiceImpl類各屬性的作用
1.log:打印日志
2.baseMapper:實現了許多的SQL操作
3.entityClass:實體類
4.mapperClass:映射類
BaseMapper類中各方法
ServiceImpl類中有這個類的成員變量,因此通過ServiceImpl這個類便能夠操作如下方法:
1.int insert(T entity);:插入記錄
2.int deleteById(Serializable id);:通過id刪除指定記錄
3.int deleteByMap(Map<String, Object> columnMap):通過Map集合添加刪除指定記錄
4.int delete(@Param(Constants.WRAPPER) Wrapper queryWrapper):通過添加構造器刪除指定記錄
5.int deleteBatchIds(Collection<? extends Serializable> idList):通過List集合批量刪除記錄
6.int updateById(T entity):根據id修改指定記錄
7.int update(T entity, Wrapper updateWrapper):根據條件構造器
8.T selectById(Serializable id):根據id查詢指定記錄
9.List selectBatchIds(Collection<? extends Serializable> idList):根據List集合批量查詢記錄
10.List selectByMap(Map<String, Object> columnMap):根據Map集合查詢記錄
11.T selectOne(Wrapper queryWrapper):根據條件構造器查詢一條記錄
12.Integer selectCount(Wrapper queryWrapper):根據條件構造器查詢記錄總數
13.List selectList(Wrapper queryWrapper):根據條件構造器查詢全部記錄
14.List<Map<String, Object>> selectMaps(Wrapper queryWrapper):根據條件構造器查詢全部記錄
15.ist selectObjs(Wrapper queryWrapper):根據條件構造器查詢全部記錄
16.<E extends IPage> E selectPage(E page, Wrapper queryWrapper):根據條件構造器查詢全部記錄(并翻頁)
17.<E extends IPage<Map<String, Object>>> E selectMapsPage(E page, Wrapper queryWrapper):根據條件構造器查詢全部記錄(并翻頁)
Wrapper類各方法
1.getEntity():實體對象(子類實現)
2.getSqlSelectgetSqlSet():
3.getSqlComment():
4.getSqlFirst():
5.getExpression():獲取 MergeSegments
6.getCustomSqlSegment():獲取自定義SQL 簡化自定義XML復雜情況
7.isEmptyOfWhere():查詢條件為空(包含entity)
8.nonEmptyOfWhere():查詢條件不為空(包含entity)
9.isEmptyOfNormal():查詢條件為空(不包含entity)
10.nonEmptyOfNormal():查詢條件為空(不包含entity)
11.nonEmptyOfEntity():深層實體判斷屬性
12.fieldStrategyMatch():根據實體FieldStrategy屬性來決定判斷邏輯
13.isEmptyOfEntity():深層實體判斷屬性
14.getTargetSql():獲取格式化后的執行sql
15.clear():條件清空
實例說明
public 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就已經注入了進來,不需要重新注入。