springboot中整合pagehelper、mybatisplus(扩展批量插入)

springboot中整合pagehelper、mybatisplus(批量插入) ,需要扩展mybatisplus的BaseMapper。

1、pom继承spring-boot-starter-parent:2.1.7.RELEASE

2、项目pom.xml中pagehelper及mybatisplus依赖

<!--pageHelper 分页插件-->
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper-spring-boot-starter</artifactId>
        <version>1.2.10</version>
        <exclusions>
            <exclusion>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

       <!--mybatis-plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.2.0</version>
        </dependency>

3、application.yml配置

#mybatsplus 配置
mybatis-plus:
  config-location: classpath:SqlMapConfig.xml

#分页插件
pagehelper:
  helperDialect: mysql 
  reasonable: false  #翻页直到最后,不重复翻页。
  supportMethodsArguments: false # 需要手动调用PageHelper.startPage() 开启分页
  params: count=countSql

SqlMapConfig.xml配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
  <settings>
    <!-- 启动延迟加载  积极加载false -->
    <setting name="lazyLoadingEnabled" value="true"/>
    <setting name="aggressiveLazyLoading" value="false"/>
    <!-- mapper中带下划线字段可映射为bean中对应驼峰格式的属性-->
    <setting name="mapUnderscoreToCamelCase" value="true"/>
  </settings>
  <mappers>
    <package name="com.cbyzs.ssdemo"/>
  </mappers>
  
</configuration>

4、扩展mybatisplus 

ExpandBaseMapper 扩展BaseMapper

package com.cbyzs.ssdemo.common.mplus;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import java.util.Collection;
public interface  ExpandBaseMapper<T> extends BaseMapper<T> {
 
    /**
     * 批量插入 仅适用于mysql
     * @param entityList 实体列表
     * @return 影响行数
     */
    Integer insertBatchSomeColumn(Collection<T> entityList);
 
}

自定义ExpandSqlInjector 扩展DefaultSqlInjector

package com.cbyzs.ssdemo.common.mplus;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import com.baomidou.mybatisplus.extension.injector.methods.additional.InsertBatchSomeColumn;
import java.util.List;
  
public class ExpandSqlInjector extends DefaultSqlInjector {
 
    @Override
    public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
        List<AbstractMethod> methodList = super.getMethodList(mapperClass);
        methodList.add(new InsertBatchSomeColumn());
        return methodList;
    }
}

MybatisPlusConfig 配置类

package com.cbyzs.ssdemo.common.mplus;
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisPlusConfig  {
 
    @Bean
    public ExpandSqlInjector expandSqlInjector() {
        return new ExpandSqlInjector();
    }
 
}

自己的业务Mapper接口需改为继承ExpandBaseMapper ,业务service中调用mapper的insertBatchSomeColumn()方法即可实现增强的批量插入。

例如: 

Mapper层:

public interface CarRegMapper extends ExpandBaseMapper<CarReg> {

}

Service层方法:

 
    public void importData(List<CarReg> dataList) {
       //xxx
        carRegMapper.insertBatchSomeColumn(dataList);
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值