Mybatis plus无介绍快使用,MybatisPlus3.5版本设置批量插入附源码(十一)

问题背景

项目中,尽量使用批量插入,减少数据库磁盘IO的操作,提高效率
注意事项:

Mybatis-plus无介绍快使用,CRUD增删改查基本使用附源码(一)

Mybatis-plus无介绍快使用,自定义sql语句CRUD增删改查附源码(二)

Mybatis-plus无介绍快使用,自带封装service层的使用附源码(三)

Mybatis-plus无介绍快使用,注解的使用(四)

Mybatis-plus无介绍快使用,Wrapper条件构造器的使用附源码(五)

Mybatis-plus无介绍快使用,分页插件和乐观锁插件的使用附源码(六)

Mybatis-plus无介绍快使用,枚举变量的使用附源码(七)

Mybatis-plus无介绍快使用,多数据源的使用(八)

Mybatis-plus无介绍快使用,MybatisX自动生成代码插件的使用(九)

Mybatis-plus无介绍快使用,可继承通用的基础实体类(十)

MybatisPlus批量插入

1 MybatisPlus3.5版本和3.3版本有些许不同,3.3版本会少一个tableInfo参数,创建EasySqlInjector类

package com.lanran.transactional.config;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn;

import java.util.List;

/**
 * @Author suolong
 * @Date 2022/7/21 9:59
 * @Version 2.0
 */


public class EasySqlInjector extends DefaultSqlInjector {

    @Override
    public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
        // TODO Auto-generated method stub
        List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
        methodList.add(new InsertBatchSomeColumn()); // 添加InsertBatchSomeColumn方法
        return methodList;
    }

}

2 添加mybatisplus配置类

package com.lanran.transactional.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @Author suolong
 * @Date 2022/7/21 9:57
 * @Version 2.0
 */
@Configuration
public class MybatisPlusConfig {

        @Bean
        public EasySqlInjector easySqlInjector () {
            return new EasySqlInjector();
        }

}

3 启动类,一定要添加MapperScan

package com.lanran.transactional;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan("com.lanran.transactional.dao")
@SpringBootApplication
public class TransactionalApplication {

    public static void main(String[] args) {
        SpringApplication.run(TransactionalApplication.class, args);
    }

}

4 mapper、xml、entity都是使用mybatisplus生成的,测试类如下

package com.lanran.transactional;

import com.lanran.transactional.dao.JpaTestMapper;
import com.lanran.transactional.dao.PaymentMapper;
import com.lanran.transactional.entity.Payment;
import com.lanran.transactional.service.PaymentService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;


@SpringBootTest
class TransactionalApplicationTests {

    @Test
    void contextLoads() {
    }

    @Autowired
    PaymentMapper paymentMapper;


    @Test
    public void batchInsertTest(){
        List<Payment> payments = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Payment p = new Payment();
            p.setSerial(String.valueOf(i));
            payments.add(p);
        }
        Integer integer = paymentMapper.insertBatchSomeColumn(payments);
        System.out.println(integer);
    }

}

5 application配置文件

spring:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mysqltest?characterEncoding=utf-8&useSSL=false
    username: root
    password: 123456
  sql:
    init:
      encoding: utf-8

6 测试结果,一共批量插入了5条数据

7 整体项目目录

总结

自己也可以在xml中写sql语句,只是表太多有点麻烦




作为程序员第 213 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha …

Lyric: 放晴 等雨说放弃

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值