最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MyBatis-plus的五種批量插入方式對比分析

 更新時間:2023年06月20日 11:56:16   作者:qq_1797801363  
本文主要介紹了MyBatis-plus的五種批量插入方式對比分析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

Mybatis批量插入一直是開發(fā)者重點關注的問題,本文列舉了Mybatis的五種插入方式進行對比分析,驗證了五種批量插入的方式的優(yōu)先級。

1 準備工作

1.1 新建spring項目

略。

1.2 導入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.3 配置yml文件

server:
  port: 8080
spring:
  datasource:
    username: mysql用戶名
    password: mysql密碼
    url: jdbc:mysql://localhost:3306/數(shù)據(jù)庫名字?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
  mapper-locations: classpath:mapping/*.xml

1.4 創(chuàng)建插入模型

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

2 測試

2.1 Mybatis利用For循環(huán)批量插入

1、編寫UserService服務類,測試一萬條數(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、編寫UserMapper接口

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

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>

4、進行單元測試

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

5、輸出結果

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

2.2 MyBatis的手動批量提交

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

@Service
public class UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private SqlSessionTemplate sqlSessionTemplate;
    public void InsertUsers(){
        //關閉自動提交
        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" );
    }
}

2、結果輸出

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

2.3 Mybatis以集合方式批量新增

1、編寫UserService服務類

@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" );
    }
}

2、編寫UserMapper接口

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

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、輸出結果

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

2.4 MyBatis-Plus提供的SaveBatch方法

1、編寫UserService服務

@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" );
    }
}

2、編寫UserMapper接口

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

3、單元測試結果

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

2.5 MyBatis-Plus提供的InsertBatchSomeColumn方法

1、編寫EasySqlInjector 自定義類

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

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

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

3、編寫UserService服務類

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" );
    }
}

4、編寫EasyBaseMapper接口

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

5、編寫UserMapper接口

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

6、單元測試結果

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

2.6 JDBC原生的批量插入

1、編寫JDBC池化工具類

public class JDBCDruidUtils {
? ? private static DataSource dataSource;
? ? private static Connection conn;
? ? /*
? ?創(chuàng)建數(shù)據(jù)Properties集合對象加載加載配置文件
? ? */
? ? static {
? ? ? ? Properties pro = new Properties();
? ? ? ? //加載數(shù)據(jù)庫連接池對象
? ? ? ? try {
? ? ? ? ? ? //獲取數(shù)據(jù)庫連接池對象
? ? ? ? ? ? pro.load(JDBCDruidUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
? ? ? ? ? ? dataSource = DruidDataSourceFactory.createDataSource(pro);
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
? ? /*
? ? 獲取連接
? ? ?*/
? ? public static Connection getConnection() throws SQLException {
? ? ? ? return dataSource.getConnection();
? ? }
? ? /**
? ? ?* 關閉conn,和 statement獨對象資源
? ? ?*
? ? ?* @param connection
? ? ?* @param statement
? ? ?* @MethodName: close
? ? ?* @return: void
? ? ?*/
? ? public static void close(Connection connection, Statement statement) {
? ? ? ? if (connection != null) {
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? connection.close();
? ? ? ? ? ? } catch (SQLException e) {
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? if (statement != null) {
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? statement.close();
? ? ? ? ? ? } catch (SQLException e) {
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? /**
? ? ?* 關閉 conn , statement 和resultset三個對象資源
? ? ?*
? ? ?* @param connection
? ? ?* @param statement
? ? ?* @param resultSet
? ? ?* @MethodName: close
? ? ?* @return: void
? ? ?*/
? ? public static void close(Connection connection, Statement statement, ResultSet resultSet) {
? ? ? ? close(connection, statement);
? ? ? ? if (resultSet != null) {
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? resultSet.close();
? ? ? ? ? ? } catch (SQLException e) { ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? /*
? ? 獲取連接池對象
? ? ?*/
? ? public static DataSource getDataSource() {
? ? ? ? return dataSource;
? ? }
}
# druid.properties配置
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/數(shù)據(jù)庫?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
username=用戶名
password=密碼
initialSize=10
maxActive=50
maxWait=60000

2、編寫UserService服務類

public void InsertUsersByJdbc() {
        long start = System.currentTimeMillis();
        Connection connection = null;
        PreparedStatement ps = null;
        try {
            connection = JDBCDruidUtils.getConnection();
            //控制事務:默認不提交
            connection.setAutoCommit(false);
            String sql = "INSERT INTO user (username, password) VALUES (?, ?)";
            ps = connection.prepareStatement(sql);
            User user;
            for (int i = 0; i < 10000; i++) {
                user = new User();
                user.setUsername("name" + i);
                user.setPassword("password" + i);
                ps.setString(1, user.getUsername());
                ps.setString(2, user.getPassword());
                //將一組參數(shù)添加到此 PreparedStatement 對象的批處理命令中。
                ps.addBatch();
            }
            //執(zhí)行批處理
            ps.executeBatch();
            //手動提交事務
            connection.commit();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCDruidUtils.close(connection, ps);
        }
        long end = System.currentTimeMillis();
        System.out.println("一萬條數(shù)據(jù)總耗時:" + (end - start) + "ms");
    }

3、輸出結果

1萬數(shù)據(jù)總耗時19000ms。

3 總結

大量數(shù)據(jù)的場景下性能對比:InsertBatchSomeColumn>自定義xml以集合的方式>Jdbc原生>SaveBatch>手動for循環(huán)批量>自動for循環(huán)批量。
網(wǎng)上很多人都說JDBC原生性能很好,但是我發(fā)現(xiàn)其非常差,有可能是我使用的是mybatis-plus依賴,如果這是推論正確,那就可以證明mybatis-plus在mybatis的基礎上不僅增強了功能也增強了性能。所以可以得出結論:開發(fā)中用mybatis-plus是沒有錯的,如果想提高性能,只能實施其他方案,比如分庫分表,千萬別想著JDBC原生性能更好。

到此這篇關于MyBatis-plus的五種批量插入方式對比分析的文章就介紹到這了,更多相關MyBatis-plus 批量插入內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 深入分析Spring BeanDefinition的構造元信息

    深入分析Spring BeanDefinition的構造元信息

    Bean Definition是一個包含Bean元數(shù)據(jù)的對象,它描述了如何創(chuàng)建Bean實例、Bean屬性的值以及Bean之間的依賴關系,本文將帶大家深入分析Spring BeanDefinition的構造元信息,需要的朋友可以參考下
    2024-01-01
  • Spring Boot中配置文件application.properties使用

    Spring Boot中配置文件application.properties使用

    這篇文章主要介紹了Spring Boot中配置文件application.properties使用及spring boot讀取application.properties文件的方式,需要的朋友參考下吧
    2018-01-01
  • 從Maven到Spring Boot實戰(zhàn)教程

    從Maven到Spring Boot實戰(zhàn)教程

    本文系統(tǒng)梳理了Java后端開發(fā)的核心技術棧,從Maven項目管理到Spring Boot實戰(zhàn),從MyBatis持久層到Spring MVC Web開發(fā),涵蓋了項目開發(fā)的全流程與關鍵知識點,感興趣的朋友跟隨小編一起看看吧
    2025-11-11
  • SpringBoot工程搭建打包、啟動jar包和war包的教程圖文詳解

    SpringBoot工程搭建打包、啟動jar包和war包的教程圖文詳解

    這篇文章主要介紹了SpringBoot工程搭建打包、啟動jar包和war包的教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • Java中將bean放入Spring容器中的幾種方式詳解

    Java中將bean放入Spring容器中的幾種方式詳解

    這篇文章主要介紹了Java中將bean放入Spring容器中的幾種方式詳解,在Spring框架中,有多種方式可以將Bean(即對象)放入Spring容器中,今天我們就來詳細說一下這幾種方式,需要的朋友可以參考下
    2023-07-07
  • JAVA利用HttpClient進行HTTPS接口調用的方法

    JAVA利用HttpClient進行HTTPS接口調用的方法

    本篇文章主要介紹了JAVA利用HttpClient進行HTTPS接口調用的方法,具有一定的參考價值,有興趣的可以了解一下
    2017-08-08
  • Java?九宮重排(滿分解法)

    Java?九宮重排(滿分解法)

    本文主要介紹了Java?九宮重排(滿分解法),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • 全解史上最快的JOSN解析庫alibaba Fastjson

    全解史上最快的JOSN解析庫alibaba Fastjson

    這篇文章主要介紹了史上最快的JOSN解析庫alibaba Fastjson,對FastJson感興趣的同學,一定要看一下
    2021-04-04
  • JavaFx UI控件與代碼間的綁定方法

    JavaFx UI控件與代碼間的綁定方法

    這篇文章主要為大家詳細介紹了JavaFx UI控件與代碼間如何綁定,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • 詳解Spring獲取配置的三種方式

    詳解Spring獲取配置的三種方式

    這篇文章主要為大家詳細介紹了Spring獲取配置的三種方式:@Value方式動態(tài)獲取單個配置、@ConfigurationProperties+前綴方式批量獲取配置以及Environment動態(tài)獲取單個配置,感興趣的可以了解一下
    2022-03-03

最新評論

德昌县| 双流县| 双桥区| 日喀则市| 达孜县| 商水县| 沂南县| 岑溪市| 安岳县| 桦南县| 渭源县| 南木林县| 霍邱县| 云南省| 井冈山市| 嘉黎县| 迁安市| 垦利县| 惠水县| 邢台市| 依兰县| 两当县| 大同县| 拜泉县| 石台县| 来凤县| 玛曲县| 安徽省| 丰城市| 大田县| 宿松县| 临沭县| 修水县| 铅山县| 绩溪县| 镇赉县| 泰顺县| 永济市| 宁晋县| 汉阴县| 长寿区|