扩展BaseMapper类

扩展mybatis-plus的BaseMapper类

mp的BaseMapper只提供了简单的增删改查方法,一些复杂点的操作没有提供,或是在IService类中提供。如果想要直接通过mapper类调用这些方法,可以通过扩展BaseMapper来使其获取新的功能。

insertBatch

扩展一个批量插入insertBatch的方法。

  1. 定义SqlInjector类,扩展功能
java 复制代码
public class ExpandSqlInjector extends DefaultSqlInjector {
    @Override
    public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
        List<AbstractMethod> methodList = super.getMethodList(mapperClass);
        methodList.add(new InsertBatchMethod());
        methodList.add(new InsertIfNotExistsMethod());
        return methodList;
    }

    /**
     * 批量插入方法
     */
    public static class InsertBatchMethod extends AbstractMethod {
        @Override
        public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
            String sql = "<script>INSERT INTO %s (%s) VALUES %s";

            List<String> columnList = new ArrayList<>();
            List<String> propertyList = new ArrayList<>();
            if (tableInfo.havePK() && !AUTO.equals(tableInfo.getIdType())) {
                columnList.add(tableInfo.getKeyColumn());
                propertyList.add(tableInfo.getKeyProperty());
            }
            // getFiledList等方法获取的内容会自动根据@TableField相关配置而改变
            for (TableFieldInfo fieldInfo : tableInfo.getFieldList()) {
                columnList.add(fieldInfo.getColumn());
                propertyList.add(fieldInfo.getProperty());
            }

            String columns = String.join(",", columnList);
            String values = "<foreach collection='list' item='item' separator=','>" +
                    "(#{item." + String.join("},#{item.", propertyList) + "})" +
                    "</foreach></script>";
            String sqlResult = String.format(sql, tableInfo.getTableName(), columns, values);

            SqlSource sqlSource = languageDriver.createSqlSource(configuration, sqlResult, modelClass);
            return getMappedStatement(this::addInsertMappedStatement, mapperClass, "insertBatch", modelClass, tableInfo, sqlSource, "");
        }
    }

    private static MappedStatement getMappedStatement(MappedStatementMethod method, Class<?> mapperClass, String id, Class<?> modelClass, TableInfo tableInfo, SqlSource sqlSource, String prefix) {

        // 根据@TableId注解自动设置KeyGenerator
        KeyGenerator keyGenerator = NoKeyGenerator.INSTANCE; // 默认值
        String keyColumn = null;
        String keyProperty = null;

        // 如果表有主键字段
        if (tableInfo.havePK()) {
            keyColumn = tableInfo.getKeyColumn();
            keyProperty = prefix + tableInfo.getKeyProperty();

            // 根据主键类型设置,INPUT、NONE:NoKeyGenerator,ASSIGN_ID, ASSIGN_UUID等策略,Java层面生成,不需要数据库生成
            if (AUTO.equals(tableInfo.getIdType())) {
                keyGenerator = Jdbc3KeyGenerator.INSTANCE;
            }
        }
        // 这里的keyGenerator与实际sql语句无关,是否插入id取决你构造的sql语句
        return method.accept(mapperClass, modelClass, id, sqlSource, keyGenerator, keyProperty, keyColumn);
    }

    @FunctionalInterface
    public interface MappedStatementMethod {
        MappedStatement accept(Class<?> mapperClass, Class<?> parameterType, String id, SqlSource sqlSource, KeyGenerator keyGenerator, String keyProperty, String keyColumn);
    }
}
  1. 配置SqlInjector:在mp的配置类中(也可以通过yml文件配置)
java 复制代码
@Bean
public ExpandSqlInjector expandSqlInjector() {
  	return new ExpandSqlInjector();
}
  1. 具体mapper类加上insertBatch方法;或定义一个新的BaseMapper,让其继承BaseMapper(推荐后者,可以统一配置)

实际通过前面二步,生成的BaseMapper代理类已经有了扩展的方法,这一步了是为了让接口也拥有扩展方法。

java 复制代码
public interface AppMapper<T> extends BaseMapper<T> {
    /**
     * 批量插入
     * @param list 实体列表
     * @return 插入条数
     */
    int insertBatch(List<T> list);
}

insertIfNotExists

扩展一个不存在则插入insertIfNotExists的方法。

  1. ExpandSqlInjector类中添加以下内容
java 复制代码
/**
 * 如果满足条件的记录不存在才插入
 */
public static class InsertIfNotExistsMethod extends AbstractMethod {
  @Override
  public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
    // 动态生成 SQL
    String sql = "<script>INSERT INTO %s (%s) " +
      "SELECT %s FROM dual " +
      "WHERE NOT EXISTS (" +
      "   SELECT 1 FROM %s ${ew.customSqlSegment}" +
      ")</script>";

    // 1. 插入的字段列表(会自动加上if标签,根据类型添加相关的非空判断,最后一个字段会多个逗号)
    String insertColumns = tableInfo.getAllInsertSqlColumnMaybeIf("entity.");
    insertColumns = SqlScriptUtils.convertTrim(insertColumns, null, null, null, ",");
    // 2. 插入的值(会自动加上if标签,根据类型添加相关的非空判断,最后一个值会多个逗号)
    String insertValues = tableInfo.getAllInsertSqlPropertyMaybeIf("entity.");
    insertValues = SqlScriptUtils.convertTrim(insertValues, null, null, null, ",");

    String tableName = tableInfo.getTableName();
    String sqlResult = String.format(sql, tableName, insertColumns, insertValues, tableName);

    SqlSource sqlSource = languageDriver.createSqlSource(configuration, sqlResult, modelClass);
    return getMappedStatement(this::addInsertMappedStatement, mapperClass, "insertIfNotExists", modelClass, tableInfo, sqlSource, "entity.");
  }
}
  1. AppMapper类里添加以下内容:
java 复制代码
/**
 * 插入,如果满足指定条件的记录已存在则忽略
 * @param entity 插入的实体类实例
 * @param ew 条件对象
 */
int insertIfNotExists(@Param("entity") T entity, @Param(WRAPPER) Wrapper<T> ew);
相关推荐
wenyq72 分钟前
LeetCode 152. Maximum Product Subarray
java·算法·leetcode
余额瞒着我当琳3 分钟前
C++异常,异常的概念,异常抛出与捕获,RAII 和智能指针的设计思路,C++标准库一些指针的使用
java·开发语言·c++·算法
花间相见20 分钟前
【macOS开发环境:Java配置】—— Mac上的Java版本管理-SDKMAN安装、多版本切换与项目实战
java·架构·个人开发
骑着蜗牛撵大象32725 分钟前
大模型 SDK 编译就崩?用 CMakeLists 把 httplib、JSON 与 OpenSSL 装配成可复用的 ChatServer
android·java·c++·http·json·cmake·大模型sdk
码智社29 分钟前
Redis生产环境高频踩坑总结(Java后端视角)
java·数据库·redis
江苏汉软36 分钟前
制造企业为什么需要MES
java·数据库·制造·mes系统·mes软件
deviant-ART44 分钟前
java注解简写规则
java·后端
邪修king1 小时前
Re:Linux系统篇(三十八):进程间通信ChapterSystem V 信号量深度解析|同步互斥、内核源码、C 指针多态、实战代码
java·linux·c语言
文丑颜不良啊1 小时前
LeetCode1181~1200题目解析
java·leetcode