自定义方法SQL注入器-DefaultSqlInjector

文章介绍了如何在mybatis-plus框架中创建一个自定义Sql注入器,扩展了默认的insertBatchSomeColumn方法,同时配置了MybatisPlusInterceptor以实现分页功能。作者强调了Lambda表达式的使用和对原有方法的保留。
摘要由CSDN通过智能技术生成
/**
 * 自定义Sql注入
 * @author zy
 */
public class SqlInjector extends DefaultSqlInjector {
    @Override
    public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
        // 注意:此SQL注入器继承了DefaultSqlInjector(默认注入器),调用了DefaultSqlInjector的getMethodList方法,保留了mybatis-plus的自带方法
        List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
        //往父类方法列表中添加了一个新的方法 InsertBatchSomeColumn
        //该方法使用了一个 Lambda 表达式作为参数,逻辑是检查字段填充(fieldFill)是否不等于 FieldFill.UPDATE
        methodList.add(new InsertBatchSomeColumn(i -> i.getFieldFill() != FieldFill.UPDATE));
        return methodList;
    }
}
/**
 * mybatisPlus配置类
 * @author zy
 */
@Configuration
public class MybatisConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }
    @Bean
    public SqlInjector easySqlInjector(){
        return new SqlInjector();
    }
}
public interface SqlInjectorMapper<T> extends BaseMapper<T> {
    /**
     * 批量插入
     */
    Integer insertBatchSomeColumn(Collection<T> entityList);
}
public interface UserMapper extends SqlInjectorMapper<User> {

}

A用insertBatchSomeColumn()方法

B用saveBatch()方法

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MaGgIeOo0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值