h5手机端网站开发,高并发网站开发语言,安徽省干部建设教育网站,当前主流的网站开发语言前言
BaseMapperPlus并非MyBatis-Plus#xff08;MP#xff09;官方提供的标准接口#xff0c;而是社区开发者基于MP的BaseMapper接口进行二次封装和增强后创建的一个自定义接口。这个概念可能因不同项目或个人实践而有所差异#xff0c;但其核心思想是为了解决特定场景下…前言
BaseMapperPlus并非MyBatis-PlusMP官方提供的标准接口而是社区开发者基于MP的BaseMapper接口进行二次封装和增强后创建的一个自定义接口。这个概念可能因不同项目或个人实践而有所差异但其核心思想是为了解决特定场景下的需求进一步简化数据库操作并提高开发效率。 代码示例
/*** 自定义 Mapper 接口, 实现 自定义扩展** param M mapper 泛型* param T table 泛型* param V vo 泛型* author xiaoli* since 2024/1/24*/
SuppressWarnings(unchecked)
public interface BaseMapperPlusM, T, V extends BaseMapperT {Log log LogFactory.getLog(BaseMapperPlus.class);int DEFAULT_BATCH_SIZE 1000;default ClassV currentVoClass() {return (ClassV) ReflectionKit.getSuperClassGenericType(this.getClass(), BaseMapperPlus.class, 2);}default ClassT currentModelClass() {return (ClassT) ReflectionKit.getSuperClassGenericType(this.getClass(), BaseMapperPlus.class, 1);}default ClassM currentMapperClass() {return (ClassM) ReflectionKit.getSuperClassGenericType(this.getClass(), BaseMapperPlus.class, 0);}default ListT selectList() {return this.selectList(new QueryWrapper());}/*** 批量插入*/default boolean insertBatch(CollectionT entityList) {return insertBatch(entityList, DEFAULT_BATCH_SIZE);}/*** 批量更新*/default boolean updateBatchById(CollectionT entityList) {return updateBatchById(entityList, DEFAULT_BATCH_SIZE);}/*** 批量插入或更新*/default boolean insertOrUpdateBatch(CollectionT entityList) {return insertOrUpdateBatch(entityList, DEFAULT_BATCH_SIZE);}/*** 批量插入(包含限制条数)*/default boolean insertBatch(CollectionT entityList, int batchSize) {String sqlStatement SqlHelper.getSqlStatement(this.currentMapperClass(), SqlMethod.INSERT_ONE);return SqlHelper.executeBatch(this.currentModelClass(), log, entityList, batchSize,(sqlSession, entity) - sqlSession.insert(sqlStatement, entity));}/*** 批量更新(包含限制条数)*/default boolean updateBatchById(CollectionT entityList, int batchSize) {String sqlStatement SqlHelper.getSqlStatement(this.currentMapperClass(), SqlMethod.UPDATE_BY_ID);return SqlHelper.executeBatch(this.currentModelClass(), log, entityList, batchSize,(sqlSession, entity) - {MapperMethod.ParamMapT param new MapperMethod.ParamMap();param.put(Constants.ENTITY, entity);sqlSession.update(sqlStatement, param);});}/*** 批量插入或更新(包含限制条数)*/default boolean insertOrUpdateBatch(CollectionT entityList, int batchSize) {TableInfo tableInfo TableInfoHelper.getTableInfo(this.currentModelClass());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.currentModelClass(), this.currentMapperClass(), log, entityList, batchSize, (sqlSession, entity) - {Object idVal tableInfo.getPropertyValue(entity, keyProperty);String sqlStatement SqlHelper.getSqlStatement(this.currentMapperClass(), SqlMethod.SELECT_BY_ID);return StringUtils.checkValNull(idVal)|| CollectionUtils.isEmpty(sqlSession.selectList(sqlStatement, entity));}, (sqlSession, entity) - {MapperMethod.ParamMapT param new MapperMethod.ParamMap();param.put(Constants.ENTITY, entity);String sqlStatement SqlHelper.getSqlStatement(this.currentMapperClass(), SqlMethod.UPDATE_BY_ID);sqlSession.update(sqlStatement, param);});}/*** 插入或更新(包含限制条数)*/default boolean insertOrUpdate(T entity) {if (null ! entity) {TableInfo tableInfo TableInfoHelper.getTableInfo(this.currentModelClass());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 tableInfo.getPropertyValue(entity, tableInfo.getKeyProperty());return StringUtils.checkValNull(idVal) || Objects.isNull(selectById((Serializable) idVal)) ? insert(entity) 0 : updateById(entity) 0;}return false;}default V selectVoById(Serializable id) {return selectVoById(id, this.currentVoClass());}/*** 根据 ID 查询*/default C C selectVoById(Serializable id, ClassC voClass) {T obj this.selectById(id);if (ObjectUtil.isNull(obj)) {return null;}return BeanCopyUtils.copy(obj, voClass);}default ListV selectVoById(Collection? extends Serializable idList) {return selectVoBatchIds(idList, this.currentVoClass());}/*** 查询根据ID 批量查询*/default C ListC selectVoBatchIds(Collection? extends Serializable idList, ClassC voClass) {ListT list this.selectBatchIds(idList);if (CollUtil.isEmpty(list)) {return CollUtil.newArrayList();}return BeanCopyUtils.copyList(list, voClass);}default ListV selectVoByMap(MapString, Object map) {return selectVoByMap(map, this.currentVoClass());}/*** 查询根据 columnMap 条件*/default C ListC selectVoByMap(MapString, Object map, ClassC voClass) {ListT list this.selectByMap(map);if (CollUtil.isEmpty(list)) {return CollUtil.newArrayList();}return BeanCopyUtils.copyList(list, voClass);}default V selectVoOne(WrapperT wrapper) {return selectVoOne(wrapper, this.currentVoClass());}/*** 根据 entity 条件查询一条记录*/default C C selectVoOne(WrapperT wrapper, ClassC voClass) {T obj this.selectOne(wrapper);if (ObjectUtil.isNull(obj)) {return null;}return BeanCopyUtils.copy(obj, voClass);}default ListV selectVoList(WrapperT wrapper) {return selectVoList(wrapper, this.currentVoClass());}/*** 根据 entity 条件查询全部记录*/default C ListC selectVoList(WrapperT wrapper, ClassC voClass) {ListT list this.selectList(wrapper);if (CollUtil.isEmpty(list)) {return CollUtil.newArrayList();}return BeanCopyUtils.copyList(list, voClass);}default P extends IPageV P selectVoPage(IPageT page, WrapperT wrapper) {return selectVoPage(page, wrapper, this.currentVoClass());}/*** 分页查询VO*/default C, P extends IPageC P selectVoPage(IPageT page, WrapperT wrapper, ClassC voClass) {IPageT pageData this.selectPage(page, wrapper);IPageC voPage new Page(pageData.getCurrent(), pageData.getSize(), pageData.getTotal());if (CollUtil.isEmpty(pageData.getRecords())) {return (P) voPage;}voPage.setRecords(BeanCopyUtils.copyList(pageData.getRecords(), voClass));return (P) voPage;}}
使用方法示例
// 定义实体类和Vo类
public class User {private Long id;private String username;// 其他属性、getter/setter省略...
}public class UserVo {private Long id;private String username;// 其他属性、getter/setter省略...
}// 自定义扩展的Mapper接口
public interface UserMapper extends BaseMapperPlusUserMapper, User, UserVo {// 可以添加更多针对User的自定义查询方法
}// Mapper接口的实现由MyBatis框架自动生成无需手动编写// 服务层接口
public interface IUserService {UserVo queryById(Long id);// 其他业务方法...
}// 服务层实现类
Service
RequiredArgsConstructor
public class UserServiceImpl implements IUserService {/** 用户Mapper接口 */private final UserMapper userMapper;/*** Description: 根据ID查询用户信息并转换为VO对象* author: luojianmin (此处应替换为实际作者)* date: 2022/6/23 9:32此处应替换为实际日期* param id 用户主键* return 用户信息VO对象*/Overridepublic UserVo queryById(Long id) {return userMapper.selectVoById(id);}// 其他业务逻辑的实现...
}一、BaseMapperPlus的独特作用与诞生背景 简化CRUD操作 BaseMapper已经提供了基础的增删改查功能然而在实际业务开发中我们可能会遇到更复杂的数据处理场景比如批量插入更新、组合查询、分组统计等。BaseMapperPlus正是为了满足这些高级需求而设计通过提供一系列便捷的方法简化了数据操作层面上的代码编写。 定制化方法 开发者可以根据项目特点在BaseMapperPlus中添加一些通用的、适用于大部分实体类的方法如根据多个字段联合查询、特殊条件下的删除操作等。这样每个具体业务对象对应的mapper接口只需继承BaseMapperPlus就能获得丰富的预定义功能。 抽象复用 通过创建BaseMapperPlus接口可以将常见的数据库操作逻辑抽象出来实现代码复用降低重复开发成本同时保持代码的整洁性和一致性。
二、继承BaseMapperPlus的好处 减少重复代码 继承BaseMapperPlus后可以直接调用其中预先定义好的方法避免了在每个具体的mapper接口中手动编写相似的操作逻辑。 提升开发效率 开发者无需关心底层SQL细节仅需关注业务逻辑本身能够更快地完成数据访问层的开发工作。 易于维护 所有通用的数据库操作逻辑都被集中到了BaseMapperPlus中一旦需要修改或优化只需要在一个地方改动即可有利于后期的维护和升级。
三、BaseMapperPlus示例方法及优势
虽然BaseMapperPlus的具体内容取决于项目的实际需求但它通常会包含以下类型的方法
批量操作增强如批量插入、批量更新、批量删除。条件查询增强支持更复杂的组合查询、排序方式自定义、结果集过滤等功能。统计分析增强提供分组统计、聚合函数使用、分页统计等功能。事务控制如果框架本身不提供则可以在BaseMapperPlus中加入对事务管理的支持。
相比原生的MyBatis-PlusBaseMapperPlus的优势在于它更加贴近项目实际具有更强的针对性和灵活性能够解决更多个性化的需求从而让开发者能专注于业务逻辑的实现而不是底层数据操作的繁琐编码。
总结
尽管BaseMapperPlus不是MyBatis-Plus的标准组件但在实际开发过程中这样的扩展接口体现了极高的实用价值。它以一种可复用、易维护的方式增强了MyBatis-Plus的功能使得开发者在面对各种复杂数据库操作时能享受到更高程度的便利性。在构建项目时建议结合具体业务需求来设计并实现一个符合自己团队习惯的BaseMapperPlus接口从而最大化地发挥出它的价值。