欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis-plus批量插入的2種方式總結(jié)

 更新時(shí)間:2023年08月08日 08:34:29   作者:廠長(zhǎng)寫代碼  
這篇文章主要給大家總結(jié)介紹了關(guān)于Mybatis-plus批量插入的2種方式,Mybatis-Plus提供了多種方式進(jìn)行批量插入優(yōu)化,文中通過(guò)代碼示例將實(shí)現(xiàn)的方法介紹的非常詳細(xì),需要的朋友可以參考下

前言

Mybatis-plus很強(qiáng),為我們誕生了極簡(jiǎn)CURD操作,但對(duì)于數(shù)據(jù)批量操作,顯然默認(rèn)提供的insert方法是不夠看的了,于是它和它來(lái)了!!! Mybatis-plus提供的兩種插入方式

  • 繼承IService(偽批量)
  • insertBatchSomeColumn 

一、繼承IService(偽批量)

在Mapper繼承BaseMapper<T>

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.UserStudy;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserStudyMapper extends BaseMapper<UserStudy> {
}

在Service中繼承IService<T>

import com.baomidou.mybatisplus.extension.service.IService;
import com.example.demo.entity.UserStudy;

/********************************************************************************
 ** @author : ZYJ
 ** @date :2023/04/20
 ** @description :廠長(zhǎng)老婆催的睡覺(jué)了-批量插入Service
 *********************************************************************************/
public interface UserStudyService extends IService<UserStudy> {

}

在Service實(shí)現(xiàn)類繼承ServiceImpl<M,T>

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.UserStudy;
import com.example.demo.mapper.UserStudyMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/********************************************************************************
 ** @author : ZYJ
 ** @date :2023/04/04
 ** @description :廠長(zhǎng)加班寫代碼-批量插入
 *********************************************************************************/
@Service
public class UserStudyServiceImpl extends ServiceImpl<UserStudyMapper, UserStudy> implements UserStudyService {

    @Resource
    private UserStudyMapper userStudyMapper;

}

測(cè)試代碼,調(diào)用IService的saveBatch方法

    /*
     *批量插入
     */
    @Override
    public void greatMany() {
        List<UserStudy> userStudyList = new ArrayList<>();
        UserStudy userStudy1 = new UserStudy();
        userStudy1.setName("張三");
        UserStudy userStudy2 = new UserStudy();
        userStudy2.setName("李四");
        userStudyList.add(userStudy1);
        userStudyList.add(userStudy2);
        //調(diào)用IService的saveBatch方法
        this.saveBatch(userStudyList);
    }

Mybatis-plus的SQL日志打印在配置文件application.yml配置

mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #開啟sql日志
#    log-impl: org.apache.ibatis.logging.nologging.NoLoggingImpl #關(guān)閉sql日志

測(cè)試結(jié)果,代碼執(zhí)行打印了兩條SQL,所以可以看得出saveBatch底層也是遍歷循環(huán)完成 

saveBatch方法分析

底層也是通過(guò)for來(lái)完成,默認(rèn)是一個(gè)事務(wù)一次提交1000條數(shù)據(jù),點(diǎn)擊進(jìn)入saveBatch可以看到, 也可以自定義每次提交多少條,自定義如下

        //調(diào)用IService的saveBatch方法
        this.saveBatch(userStudyList,2000);

二、insertBatchSomeColumn

自定義SQL注入器

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

import java.util.List;

/********************************************************************************
 ** @author : ZYJ
 ** @date :2023/03/09
 ** @description :廠長(zhǎng)加班寫代碼-批量插入SQL注入器
 *********************************************************************************/
public class InsertBatchSqlInjector extends DefaultSqlInjector {
    @Override
    public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
        List<AbstractMethod> methodList = super.getMethodList(mapperClass);
        methodList.add(new InsertBatchSomeColumn()); //添加InsertBatchSomeColumn方法
        return methodList;
    }
}

把SQL注入器交給Spring

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Component
public class MybatisPlusConfig {
    /********************************************************************************
     ** @author : ZYJ
     ** @date :2023/04/14
     ** @description :注入配置
     *********************************************************************************/
    @Bean
    public InsertBatchSqlInjector easySqlInjector () {
        return new InsertBatchSqlInjector();
    }
}

到此定義完畢,在Mapper中生成insertBatchSomeColumn(必須是這個(gè)方法名)方法,你就可以撒手不管了,直接調(diào)用就行,或者直接在ServiceImpl通過(guò)Mapper調(diào)用insertBatchSomeColumn,然后ALT+回車生成此方法。

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.UserStudy;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface UserStudyMapper extends BaseMapper<UserStudy> {
    void insertBatchSomeColumn(@Param("list") List<UserStudy> userStudyList);
}

測(cè)試代碼,調(diào)用insertBatchSomeColumn方法

    @Resource
    private UserStudyMapper userStudyMapper;
    /*
     *批量插入
     */
    @Override
    public void greatMany() {
        List<UserStudy> userStudyList = new ArrayList<>();
        UserStudy userStudy1 = new UserStudy();
        userStudy1.setName("張三");
        UserStudy userStudy2 = new UserStudy();
        userStudy2.setName("李四");
        userStudyList.add(userStudy1);
        userStudyList.add(userStudy2);
        //調(diào)用insertBatchSomeColumn方法
        userStudyMapper.insertBatchSomeColumn(userStudyList);
        //調(diào)用IService的saveBatch方法
        //this.saveBatch(userStudyList,2000);
    }

測(cè)試結(jié)果,代碼執(zhí)行打印一條SQL,所以可以看出是一條SQL便新增完成

注意:SQL有語(yǔ)句長(zhǎng)度限制,在MySQL中被參數(shù)max_allowed_packet限制,默認(rèn)為1M,如果拼接長(zhǎng)度超過(guò)此限制就會(huì)報(bào)錯(cuò),兩種解決方式,一個(gè)是調(diào)整MySQL的max_allowed_packet 限制,另一個(gè)則是通過(guò)代碼控制每次的提交數(shù)量。

通過(guò)代碼控制每次提交數(shù)量,模擬造五條數(shù)據(jù),每次提交兩條數(shù)據(jù)

    /*
     *批量插入
     */
    @Override
    public void greatMany() {
        List<UserStudy> userStudyList = new ArrayList<>();
        UserStudy userStudy1 = new UserStudy();
        userStudy1.setName("張三");
        UserStudy userStudy2 = new UserStudy();
        userStudy2.setName("李四");
        UserStudy userStudy3 = new UserStudy();
        userStudy3.setName("王五");
        UserStudy userStudy4 = new UserStudy();
        userStudy4.setName("趙六");
        UserStudy userStudy5 = new UserStudy();
        userStudy5.setName("小紅");
        userStudyList.add(userStudy1);
        userStudyList.add(userStudy2);
        userStudyList.add(userStudy3);
        userStudyList.add(userStudy4);
        userStudyList.add(userStudy5);
        //創(chuàng)建入庫(kù)的list
        List<UserStudy> userStudyCount = new ArrayList<>();
        for (int i = 0; i < userStudyList.size(); i++) {
            //調(diào)用insertBatchSomeColumn方法
            userStudyCount.add(userStudyList.get(i));
            //控制每次提交數(shù)量
            if(userStudyCount.size()==2){
                userStudyMapper.insertBatchSomeColumn(userStudyCount);
                //將入庫(kù)的list清空重新新增
                userStudyCount.clear();
            }
        }
        //將list中size不夠2的數(shù)據(jù)在此處新增
        userStudyMapper.insertBatchSomeColumn(userStudyCount);
        //調(diào)用IService的saveBatch方法
        //this.saveBatch(userStudyList,2000);
    }

結(jié)果分析,五條數(shù)據(jù)應(yīng)該請(qǐng)求三次新增,打印三條SQL,完美結(jié)束

總結(jié):

默認(rèn)的insert的方法對(duì)尋常業(yè)務(wù)來(lái)說(shuō)是非常之高效,但對(duì)于批量數(shù)據(jù)的產(chǎn)生確實(shí)災(zāi)難性的,就是慢,很慢,巨慢,IService的saveBatch方法優(yōu)于默認(rèn)的insert方法,但是我選通過(guò)SQL注入器的方法insertBatchSomeColumn。

到此這篇關(guān)于Mybatis-plus批量插入的2種方式總結(jié)的文章就介紹到這了,更多相關(guān)Mybatis-plus批量插入內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java深入淺出數(shù)組的定義與使用上篇

    Java深入淺出數(shù)組的定義與使用上篇

    數(shù)組是有序的元素序列,若將有限個(gè)類型相同的變量的集合命名,那么這個(gè)名稱為數(shù)組名。組成數(shù)組的各個(gè)變量稱為數(shù)組的分量,也稱為數(shù)組的元素,有時(shí)也稱為下標(biāo)變量。數(shù)組是在程序設(shè)計(jì)中,為了處理方便, 把具有相同類型的若干元素按有序的形式組織起來(lái)的一種形式
    2022-03-03
  • 解決MyEclipse下啟動(dòng)項(xiàng)目時(shí)JBoss內(nèi)存溢出的問(wèn)題

    解決MyEclipse下啟動(dòng)項(xiàng)目時(shí)JBoss內(nèi)存溢出的問(wèn)題

    下面小編就為大家?guī)?lái)一篇解決MyEclipse下啟動(dòng)項(xiàng)目時(shí)JBoss內(nèi)存溢出的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-07-07
  • springmvc+mybatis 做分頁(yè)sql 語(yǔ)句實(shí)例代碼

    springmvc+mybatis 做分頁(yè)sql 語(yǔ)句實(shí)例代碼

    本文通過(guò)一段實(shí)例代碼給大家介紹了springmvc+mybatis 做分頁(yè)sql 語(yǔ)句的方法,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧
    2017-07-07
  • SpringBoot中pom.xml配置詳解

    SpringBoot中pom.xml配置詳解

    pom.xml是Maven項(xiàng)目的核心配置文件,用于管理項(xiàng)目的依賴、插件、構(gòu)建配置等,在Spring Boot項(xiàng)目中,pom.xml文件也扮演著重要的角色,本文將給大家詳細(xì)介紹一下SpringBoot中pom.xml配置,需要的朋友可以參考下
    2023-09-09
  • SpringBoot新特性之全局懶加載機(jī)制

    SpringBoot新特性之全局懶加載機(jī)制

    這篇文章主要介紹了SpringBoot新特性之全局懶加載機(jī)制,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • springboot中獲取配置文件中屬性值的幾種方式小結(jié)

    springboot中獲取配置文件中屬性值的幾種方式小結(jié)

    本文主要介紹了springboot中獲取配置文件中屬性值的幾種方式小結(jié),主要介紹了六種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-05-05
  • SpringBoot使用Redis實(shí)現(xiàn)消息隊(duì)列的方法小結(jié)

    SpringBoot使用Redis實(shí)現(xiàn)消息隊(duì)列的方法小結(jié)

    在應(yīng)用中把Redis當(dāng)成消息隊(duì)列來(lái)使用已經(jīng)屢見(jiàn)不鮮了,我想主要原因是當(dāng)代應(yīng)用十有八九都會(huì)用到 Redis,因此不用再引入其他消息隊(duì)列系統(tǒng),而且Redis提供了好幾種實(shí)現(xiàn)消息隊(duì)列的方法,用起來(lái)也簡(jiǎn)單,本文給大家介紹了SpringBoot使用Redis實(shí)現(xiàn)消息隊(duì)列的方法小結(jié)
    2024-04-04
  • MyBatis動(dòng)態(tài)<if>標(biāo)簽使用避坑指南

    MyBatis動(dòng)態(tài)<if>標(biāo)簽使用避坑指南

    這篇文章主要為大家介紹了MyBatis動(dòng)態(tài)<if>標(biāo)簽使用避坑指南,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • 關(guān)于Spring的AnnotationAwareAspectJAutoProxyCreator類解析

    關(guān)于Spring的AnnotationAwareAspectJAutoProxyCreator類解析

    這篇文章主要介紹了關(guān)于Spring的AnnotationAwareAspectJAutoProxyCreator類解析,Spring是一個(gè)開源免費(fèi)的框架 , 容器,是一個(gè)輕量級(jí)的框架 ,需要的朋友可以參考下
    2023-05-05
  • 深入淺出的學(xué)習(xí)Java ThreadLocal

    深入淺出的學(xué)習(xí)Java ThreadLocal

    本文會(huì)基于實(shí)際場(chǎng)景介紹ThreadLocal如何使用以及內(nèi)部實(shí)現(xiàn)機(jī)制。 具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧
    2017-02-02

最新評(píng)論