博客详情

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

作者: 朝如青丝暮成雪
发布时间:2021-01-16 11:50:25  文章分类:java编程   阅读(1491)  评论(0)

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: true
  supportMethodsArguments: true
  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);
    }



关键字:  mybatisplus  pagehelper
评论信息
暂无评论
发表评论

亲,您还没有登陆,暂不能评论哦! 去 登陆 | 注册

博主信息
   
数据加载中,请稍候...
文章分类
   
数据加载中,请稍候...
阅读排行
 
数据加载中,请稍候...
评论排行
 
数据加载中,请稍候...

Copyright © 叮叮声的奶酪 版权所有
备案号:鄂ICP备17018671号-1

鄂公网安备 42011102000739号