建议开发中以此为数据基本操作类,而不是以Mapper为基础操作类,如果需要使用Mapper中的方法,可以直接通过getMapper()取得Entity对应的Mapper类,此类与Mapper类相比做了很多的增强功能,尤其是其lambda语法,非常高效便捷。
// 集成了MP的ServiceImpl,实现了IBaseRepository接口(内部拓展了lambda查询操作)public abstract class BaseRepository<M extends BaseMapper<E>, E> extends ServiceImpl<M, E> implements IBaseRepository<E> {@Overridepublic boolean updateById(E entity) {boolean result = super.updateById(entity);if(result) {// 数据自动更新@DataSource注解的配合逻辑SpringContextUtil.getApplicationContext().publishEvent(EntityUpdateEvent.create(entity));}return result;}@Overridepublic boolean updateBatchById(Collection<E> entityList, int batchSize) {boolean result = super.updateBatchById(entityList, batchSize);if(result) {// 数据自动更新@DataSource注解的配合逻辑for (E entity : entityList) {SpringContextUtil.getApplicationContext().publishEvent(EntityUpdateEvent.create(entity));}}return result;}@Overrideprotected Class<M> currentMapperClass() {return (Class<M>) ReflectionKit.getSuperClassGenericType(this.getClass(), BaseRepository.class, 0);}@Overrideprotected Class<E> currentModelClass() {return (Class<E>) ReflectionKit.getSuperClassGenericType(this.getClass(), BaseRepository.class, 1);}}
