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

MyBatis批量插入的五種方式小結(jié)(MyBatis以集合方式批量新增)

 更新時間:2023年01月09日 15:22:47   作者:小徐敲java  
本文主要介紹了MyBatis批量插入的五種方式小結(jié)(MyBatis以集合方式批量新增),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

一、準備工作

1.1、導(dǎo)入pom.xml依賴

<dependency>
? ? <groupId>mysql</groupId>
? ? <artifactId>mysql-connector-java</artifactId>
? ? <scope>runtime</scope>
</dependency>

<!--Mybatis依賴-->
<dependency>
? ? <groupId>org.mybatis.spring.boot</groupId>
? ? <artifactId>mybatis-spring-boot-starter</artifactId>
? ? <version>2.2.2</version>
</dependency>

<!--Mybatis-Plus依賴-->
<dependency>
? ? <groupId>com.baomidou</groupId>
? ? <artifactId>mybatis-plus-boot-starter</artifactId>
? ? <version>3.5.2</version>
</dependency>

<dependency>
? ? <groupId>org.projectlombok</groupId>
? ? <artifactId>lombok</artifactId>
? ? <optional>true</optional>
</dependency>

1.2、配置yml文件

server:
  port: 8080
 
spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver
 
mybatis:
  mapper-locations: classpath:mapping/*.xml

1.3、公用的User類

@Data
public class User {
 
    private int id;
    private String username;
    private String password;
}

二、MyBatis利用For循環(huán)批量插入

2.1、編寫UserService服務(wù)類,測試一萬條數(shù)據(jù)耗時情況

@Service
public class UserService {
?
? ? @Resource
? ? private UserMapper userMapper;
?
? ? public void InsertUsers(){
? ? ? ? long start = System.currentTimeMillis();
? ? ? ? for(int i = 0 ;i < 10000; i++) {
? ? ? ? ? ? User user = new User();
? ? ? ? ? ? user.setUsername("name" + i);
? ? ? ? ? ? user.setPassword("password" + i);
? ? ? ? ? ? userMapper.insertUsers(user);
? ? ? ? }
? ? ? ? long end = System.currentTimeMillis();
? ? ? ? System.out.println("一萬條數(shù)據(jù)總耗時:" + (end-start) + "ms" );
? ? }
?
}

2.2、編寫UserMapper接口

@Mapper
public interface UserMapper {
 
    Integer insertUsers(User user);
}

2.3、編寫UserMapper.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ithuang.demo.mapper.UserMapper">
    <insert id="insertUsers">
        INSERT INTO user (username, password)
        VALUES(#{username}, #{password})
    </insert>
</mapper>

2.4、進行單元測試

@SpringBootTest
class DemoApplicationTests {
 
    @Resource
    private UserService userService;
 
    @Test
    public void insert(){
        userService.InsertUsers();
    }
 
}

2.5、結(jié)果輸出

一萬條數(shù)據(jù)總耗時:26348ms

三、MyBatis的手動批量提交

3.1、其他保持不變,Service層作稍微的變化

@Service
public class UserService {
?
? ? @Resource
? ? private UserMapper userMapper;
?
? ? @Resource
? ? private SqlSessionTemplate sqlSessionTemplate;
?
? ? public void InsertUsers(){
? ? ? ? //關(guān)閉自動提交
? ? ? ? SqlSession sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);
? ? ? ? UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
? ? ? ? long start = System.currentTimeMillis();
? ? ? ? for(int i = 0 ;i < 10000; i++) {
? ? ? ? ? ? User user = new User();
? ? ? ? ? ? user.setUsername("name" + i);
? ? ? ? ? ? user.setPassword("password" + i);
? ? ? ? ? ? userMapper.insertUsers(user);
? ? ? ? }
? ? ? ? sqlSession.commit();
? ? ? ? long end = System.currentTimeMillis();
? ? ? ? System.out.println("一萬條數(shù)據(jù)總耗時:" + (end-start) + "ms" );
? ? }
?
}

3.2、結(jié)果輸出

一萬條數(shù)據(jù)總耗時:24516ms

四、MyBatis以集合方式批量新增(推薦)

4.1、編寫UserService服務(wù)類

@Service
public class UserService {
?
? ? @Resource
? ? private UserMapper userMapper;
?
? ? public void InsertUsers(){
? ? ? ? long start = System.currentTimeMillis();
? ? ? ? List<User> userList = new ArrayList<>();
? ? ? ? User user;
? ? ? ? for(int i = 0 ;i < 10000; i++) {
? ? ? ? ? ? user = new User();
? ? ? ? ? ? user.setUsername("name" + i);
? ? ? ? ? ? user.setPassword("password" + i);
? ? ? ? ? ? userList.add(user);
? ? ? ? }
? ? ? ? userMapper.insertUsers(userList);
? ? ? ? long end = System.currentTimeMillis();
? ? ? ? System.out.println("一萬條數(shù)據(jù)總耗時:" + (end-start) + "ms" );
? ? }
?
}

4.2、編寫UserMapper接口

@Mapper
public interface UserMapper {
 
    Integer insertUsers(List<User> userList);
}

4.3、編寫UserMapper.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ithuang.demo.mapper.UserMapper">
    <insert id="insertUsers">
        INSERT INTO user (username, password)
        VALUES
        <foreach collection ="userList" item="user" separator =",">
            (#{user.username}, #{user.password})
        </foreach>
    </insert>
</mapper>

4.4、輸出結(jié)果

一萬條數(shù)據(jù)總耗時:521ms

五、MyBatis-Plus提供的SaveBatch方法

5.1、編寫UserService服務(wù)

@Service
public class UserService extends ServiceImpl<UserMapper, User> implements IService<User> {
?
? ? public void InsertUsers(){
? ? ? ? long start = System.currentTimeMillis();
? ? ? ? List<User> userList = new ArrayList<>();
? ? ? ? User user;
? ? ? ? for(int i = 0 ;i < 10000; i++) {
? ? ? ? ? ? user = new User();
? ? ? ? ? ? user.setUsername("name" + i);
? ? ? ? ? ? user.setPassword("password" + i);
? ? ? ? ? ? userList.add(user);
? ? ? ? }
? ? ? ? saveBatch(userList);
? ? ? ? long end = System.currentTimeMillis();
? ? ? ? System.out.println("一萬條數(shù)據(jù)總耗時:" + (end-start) + "ms" );
? ? }
}

5.2、編寫UserMapper接口

@Mapper
public interface UserMapper extends BaseMapper<User> {
 
}

5.3、單元測試結(jié)果

一萬條數(shù)據(jù)總耗時:24674ms

六、MyBatis-Plus提供的InsertBatchSomeColumn方法(推薦)

6.1、編寫EasySqlInjector 自定義類

public class EasySqlInjector extends DefaultSqlInjector {
 
    @Override
    public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
        // 注意:此SQL注入器繼承了DefaultSqlInjector(默認注入器),調(diào)用了DefaultSqlInjector的getMethodList方法,保留了mybatis-plus的自帶方法
        List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
        methodList.add(new InsertBatchSomeColumn(i -> i.getFieldFill() != FieldFill.UPDATE));
        return methodList;
    }
 
}

6.2、定義核心配置類注入此Bean

@Configuration
public class MybatisPlusConfig {
 
    @Bean
    public EasySqlInjector sqlInjector() {
        return new EasySqlInjector();
    }
}

6.3、編寫UserService服務(wù)類

public class UserService{
 
    @Resource
    private UserMapper userMapper;
    public void InsertUsers(){
        long start = System.currentTimeMillis();
        List<User> userList = new ArrayList<>();
        User user;
        for(int i = 0 ;i < 10000; i++) {
            user = new User();
            user.setUsername("name" + i);
            user.setPassword("password" + i);
            userList.add(user);
        }
        userMapper.insertBatchSomeColumn(userList);
        long end = System.currentTimeMillis();
        System.out.println("一萬條數(shù)據(jù)總耗時:" + (end-start) + "ms" );
    }
}

6.4、編寫EasyBaseMapper接口

public interface EasyBaseMapper<T> extends BaseMapper<T> {
    /**
     * 批量插入 僅適用于mysql
     *
     * @param entityList 實體列表
     * @return 影響行數(shù)
     */
    Integer insertBatchSomeColumn(Collection<T> entityList);
}

6.5、編寫UserMapper接口

@Mapper
public interface UserMapper<T> extends EasyBaseMapper<User> {
    
}

6.6、單元測試結(jié)果

一萬條數(shù)據(jù)總耗時:575ms

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

相關(guān)文章

  • Java實現(xiàn)雙鏈表互相交換任意兩個節(jié)點的方法示例

    Java實現(xiàn)雙鏈表互相交換任意兩個節(jié)點的方法示例

    這篇文章主要介紹了Java實現(xiàn)雙鏈表互相交換任意兩個節(jié)點的方法,簡單講述了雙鏈表的概念,并結(jié)合實例形式給出了java雙鏈表實現(xiàn)任意兩個節(jié)點交換的操作技巧,需要的朋友可以參考下
    2017-11-11
  • Tomcat和Spring中的事件機制深入講解

    Tomcat和Spring中的事件機制深入講解

    這篇文章主要給大家介紹了關(guān)于Tomcat和Spring中事件機制的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧
    2018-12-12
  • JAVA 對數(shù)組進行初始化填充的方法示例

    JAVA 對數(shù)組進行初始化填充的方法示例

    這篇文章主要介紹了JAVA 對數(shù)組進行初始化填充的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Java實現(xiàn)并查集

    Java實現(xiàn)并查集

    這篇文章主要為大家詳細介紹了Java實現(xiàn)并查集,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • SpringBoot ApplicationListener事件監(jiān)聽接口使用問題探究

    SpringBoot ApplicationListener事件監(jiān)聽接口使用問題探究

    這篇文章主要介紹了SpringBoot ApplicationListener事件監(jiān)聽接口使用問題,自定義監(jiān)聽器需要實現(xiàn)ApplicationListener接口,實現(xiàn)對應(yīng)的方法來完成自己的業(yè)務(wù)邏輯。SpringBoot Application共支持6種事件監(jiān)聽
    2023-04-04
  • jedis的borrow行為方法源碼解讀

    jedis的borrow行為方法源碼解讀

    這篇文章主要為大家介紹了jedis的borrow行為方法源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-09-09
  • Java中MyBatis的動態(tài)語句詳解

    Java中MyBatis的動態(tài)語句詳解

    這篇文章主要介紹了Java中MyBatis的動態(tài)語句詳解,動態(tài) SQL 是 MyBatis 的強大特性之一,通過不同參數(shù)生成不同的 SQL,可以動態(tài)地對數(shù)據(jù)持久層進行操作,而不需要每個數(shù)據(jù)訪問操作都要進行手動地拼接 SQL 語句,需要的朋友可以參考下
    2023-08-08
  • 深入探究Java線程的狀態(tài)與生命周期

    深入探究Java線程的狀態(tài)與生命周期

    在java中,任何對象都要有生命周期,線程也不例外,它也有自己的生命周期。線程的整個生命周期可以分為5個階段,分別是新建狀態(tài)、就緒狀態(tài)、運行狀態(tài)、阻塞狀態(tài)和死亡狀態(tài)
    2022-04-04
  • SpringBoot?整合Redis?數(shù)據(jù)庫的方法

    SpringBoot?整合Redis?數(shù)據(jù)庫的方法

    Redis是一個基于內(nèi)存的日志型可持久化的緩存數(shù)據(jù)庫,保存形式為key-value格式,Redis完全免費開源,它使用ANSI?C語言編寫。這篇文章主要介紹了SpringBoot?整合Redis?數(shù)據(jù)庫的方法,需要的朋友可以參考下
    2018-03-03
  • java后臺驗證碼生成的實現(xiàn)方法

    java后臺驗證碼生成的實現(xiàn)方法

    在我們使用進行系統(tǒng)開發(fā)時,為了提高系統(tǒng)的安全性,在登錄的時候多數(shù)人都會要求輸入驗證,本文介紹了java后臺驗證碼生成的實現(xiàn)方法,感興趣的一起來了解一下
    2021-05-05

最新評論