package com.iailab.framework.mybatis.core.mapper; import cn.hutool.core.collection.CollUtil; import com.iailab.framework.common.pojo.PageParam; import com.iailab.framework.common.pojo.PageResult; import com.iailab.framework.common.pojo.SortablePageParam; import com.iailab.framework.common.pojo.SortingField; import com.iailab.framework.mybatis.core.enums.SqlConstants; import com.iailab.framework.mybatis.core.util.MyBatisUtils; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.baomidou.mybatisplus.extension.toolkit.Db; import com.github.yulichang.base.MPJBaseMapper; import com.github.yulichang.interfaces.MPJBaseJoin; import com.github.yulichang.wrapper.MPJLambdaWrapper; import org.apache.ibatis.annotations.Param; import java.util.Collection; import java.util.List; import java.util.Objects; /** * 在 MyBatis Plus 的 BaseMapper 的基础上拓展,提供更多的能力 * * 1. {@link BaseMapper} 为 MyBatis Plus 的基础接口,提供基础的 CRUD 能力 * 2. {@link MPJBaseMapper} 为 MyBatis Plus Join 的基础接口,提供连表 Join 能力 */ public interface BaseMapperX extends MPJBaseMapper { default PageResult selectPage(SortablePageParam pageParam, @Param("ew") Wrapper queryWrapper) { return selectPage(pageParam, pageParam.getSortingFields(), queryWrapper); } default PageResult selectPage(PageParam pageParam, @Param("ew") Wrapper queryWrapper) { return selectPage(pageParam, null, queryWrapper); } default PageResult selectPage(PageParam pageParam, Collection sortingFields, @Param("ew") Wrapper queryWrapper) { // 特殊:不分页,直接查询全部 if (PageParam.PAGE_SIZE_NONE.equals(pageParam.getPageSize())) { List list = selectList(queryWrapper); return new PageResult<>(list, (long) list.size()); } // MyBatis Plus 查询 IPage mpPage = MyBatisUtils.buildPage(pageParam, sortingFields); selectPage(mpPage, queryWrapper); // 转换返回 return new PageResult<>(mpPage.getRecords(), mpPage.getTotal()); } default PageResult selectJoinPage(PageParam pageParam, Class clazz, MPJLambdaWrapper lambdaWrapper) { // 特殊:不分页,直接查询全部 if (PageParam.PAGE_SIZE_NONE.equals(pageParam.getPageSize())) { List list = selectJoinList(clazz, lambdaWrapper); return new PageResult<>(list, (long) list.size()); } // MyBatis Plus Join 查询 IPage mpPage = MyBatisUtils.buildPage(pageParam); mpPage = selectJoinPage(mpPage, clazz, lambdaWrapper); // 转换返回 return new PageResult<>(mpPage.getRecords(), mpPage.getTotal()); } default PageResult selectJoinPage(PageParam pageParam, Class resultTypeClass, MPJBaseJoin joinQueryWrapper) { IPage mpPage = MyBatisUtils.buildPage(pageParam); selectJoinPage(mpPage, resultTypeClass, joinQueryWrapper); // 转换返回 return new PageResult<>(mpPage.getRecords(), mpPage.getTotal()); } default T selectOne(String field, Object value) { return selectOne(new QueryWrapper().eq(field, value)); } default T selectOne(SFunction field, Object value) { return selectOne(new LambdaQueryWrapper().eq(field, value)); } default T selectOne(String field1, Object value1, String field2, Object value2) { return selectOne(new QueryWrapper().eq(field1, value1).eq(field2, value2)); } default T selectOne(SFunction field1, Object value1, SFunction field2, Object value2) { return selectOne(new LambdaQueryWrapper().eq(field1, value1).eq(field2, value2)); } default T selectOne(SFunction field1, Object value1, SFunction field2, Object value2, SFunction field3, Object value3) { return selectOne(new LambdaQueryWrapper().eq(field1, value1).eq(field2, value2) .eq(field3, value3)); } default Long selectCount() { return selectCount(new QueryWrapper<>()); } default Long selectCount(String field, Object value) { return selectCount(new QueryWrapper().eq(field, value)); } default Long selectCount(SFunction field, Object value) { return selectCount(new LambdaQueryWrapper().eq(field, value)); } default List selectList() { return selectList(new QueryWrapper<>()); } default List selectList(String field, Object value) { return selectList(new QueryWrapper().eq(field, value)); } default List selectList(SFunction field, Object value) { return selectList(new LambdaQueryWrapper().eq(field, value)); } default List selectList(String field, Collection values) { if (CollUtil.isEmpty(values)) { return CollUtil.newArrayList(); } return selectList(new QueryWrapper().in(field, values)); } default List selectList(SFunction field, Collection values) { if (CollUtil.isEmpty(values)) { return CollUtil.newArrayList(); } return selectList(new LambdaQueryWrapper().in(field, values)); } @Deprecated default List selectList(SFunction leField, SFunction geField, Object value) { return selectList(new LambdaQueryWrapper().le(leField, value).ge(geField, value)); } default List selectList(SFunction field1, Object value1, SFunction field2, Object value2) { return selectList(new LambdaQueryWrapper().eq(field1, value1).eq(field2, value2)); } /** * 批量插入,适合大量数据插入 * * @param entities 实体们 */ default Boolean insertBatch(Collection entities) { // 特殊:SQL Server 批量插入后,获取 id 会报错,因此通过循环处理 if (Objects.equals(SqlConstants.DB_TYPE, DbType.SQL_SERVER)) { entities.forEach(this::insert); return CollUtil.isNotEmpty(entities); } return Db.saveBatch(entities); } /** * 批量插入,适合大量数据插入 * * @param entities 实体们 * @param size 插入数量 Db.saveBatch 默认为 1000 */ default Boolean insertBatch(Collection entities, int size) { // 特殊:SQL Server 批量插入后,获取 id 会报错,因此通过循环处理 if (Objects.equals(SqlConstants.DB_TYPE, DbType.SQL_SERVER)) { entities.forEach(this::insert); return CollUtil.isNotEmpty(entities); } return Db.saveBatch(entities, size); } default int updateBatch(T update) { return update(update, new QueryWrapper<>()); } default Boolean updateBatch(Collection entities) { return Db.updateBatchById(entities); } default Boolean updateBatch(Collection entities, int size) { return Db.updateBatchById(entities, size); } default boolean insertOrUpdate(T entity) { return Db.saveOrUpdate(entity); } default Boolean insertOrUpdateBatch(Collection collection) { return Db.saveOrUpdateBatch(collection); } default int delete(String field, String value) { return delete(new QueryWrapper().eq(field, value)); } default int delete(SFunction field, Object value) { return delete(new LambdaQueryWrapper().eq(field, value)); } }