MyBatis-plus的五種批量插入方式對(duì)比分析
Mybatis批量插入一直是開(kāi)發(fā)者重點(diǎn)關(guān)注的問(wèn)題,本文列舉了Mybatis的五種插入方式進(jìn)行對(duì)比分析,驗(yàn)證了五種批量插入的方式的優(yōu)先級(jí)。
1 準(zhǔn)備工作
1.1 新建spring項(xiàng)目
略。
1.2 導(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.3 配置yml文件
server: port: 8080 spring: datasource: username: mysql用戶名 password: mysql密碼 url: jdbc:mysql://localhost:3306/數(shù)據(jù)庫(kù)名字?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 測(cè)試
2.1 Mybatis利用For循環(huán)批量插入
1、編寫(xiě)UserService服務(wù)類,測(cè)試一萬(wàn)條數(shù)據(jù)的耗時(shí)情況:
@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("一萬(wàn)條數(shù)據(jù)總耗時(shí):" + (end-start) + "ms" ); } }
2、編寫(xiě)UserMapper接口
@Mapper public interface UserMapper { Integer insertUsers(User user); }
3、編寫(xiě)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、進(jìn)行單元測(cè)試
@SpringBootTest class DemoApplicationTests { @Resource private UserService userService; @Test public void insert(){ userService.InsertUsers(); } }
5、輸出結(jié)果
一萬(wàn)條數(shù)據(jù)耗時(shí)26348ms
2.2 MyBatis的手動(dòng)批量提交
1、其他保持不變,Service層作稍微的變化
@Service public class UserService { @Resource private UserMapper userMapper; @Resource private SqlSessionTemplate sqlSessionTemplate; public void InsertUsers(){ //關(guān)閉自動(dòng)提交 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("一萬(wàn)條數(shù)據(jù)總耗時(shí):" + (end-start) + "ms" ); } }
2、結(jié)果輸出
一萬(wàn)條數(shù)據(jù)總耗時(shí):24516ms。
2.3 Mybatis以集合方式批量新增
1、編寫(xiě)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("一萬(wàn)條數(shù)據(jù)總耗時(shí):" + (end-start) + "ms" ); } }
2、編寫(xiě)UserMapper接口
@Mapper public interface UserMapper { Integer insertUsers(List<User> userList); }
3、編寫(xiě)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、輸出結(jié)果
一萬(wàn)條數(shù)據(jù)總耗時(shí):521ms
2.4 MyBatis-Plus提供的SaveBatch方法
1、編寫(xiě)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("一萬(wàn)條數(shù)據(jù)總耗時(shí):" + (end-start) + "ms" ); } }
2、編寫(xiě)UserMapper接口
@Mapper public interface UserMapper extends BaseMapper<User> { }
3、單元測(cè)試結(jié)果
一萬(wàn)條數(shù)據(jù)總耗時(shí):24674ms
2.5 MyBatis-Plus提供的InsertBatchSomeColumn方法
1、編寫(xiě)EasySqlInjector 自定義類
public class EasySqlInjector extends DefaultSqlInjector { @Override public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) { // 注意:此SQL注入器繼承了DefaultSqlInjector(默認(rèn)注入器),調(diào)用了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、編寫(xiě)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("一萬(wàn)條數(shù)據(jù)總耗時(shí):" + (end-start) + "ms" ); } }
4、編寫(xiě)EasyBaseMapper接口
public interface EasyBaseMapper<T> extends BaseMapper<T> { /** * 批量插入 僅適用于mysql * * @param entityList 實(shí)體列表 * @return 影響行數(shù) */ Integer insertBatchSomeColumn(Collection<T> entityList); }
5、編寫(xiě)UserMapper接口
@Mapper public interface UserMapper<T> extends EasyBaseMapper<User> { }
6、單元測(cè)試結(jié)果
一萬(wàn)條數(shù)據(jù)總耗時(shí):575ms
2.6 JDBC原生的批量插入
1、編寫(xiě)JDBC池化工具類
public class JDBCDruidUtils { ? ? private static DataSource dataSource; ? ? private static Connection conn; ? ? /* ? ?創(chuàng)建數(shù)據(jù)Properties集合對(duì)象加載加載配置文件 ? ? */ ? ? static { ? ? ? ? Properties pro = new Properties(); ? ? ? ? //加載數(shù)據(jù)庫(kù)連接池對(duì)象 ? ? ? ? try { ? ? ? ? ? ? //獲取數(shù)據(jù)庫(kù)連接池對(duì)象 ? ? ? ? ? ? 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(); ? ? } ? ? /** ? ? ?* 關(guān)閉conn,和 statement獨(dú)對(duì)象資源 ? ? ?* ? ? ?* @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(); ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? /** ? ? ?* 關(guān)閉 conn , statement 和resultset三個(gè)對(duì)象資源 ? ? ?* ? ? ?* @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(); ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? /* ? ? 獲取連接池對(duì)象 ? ? ?*/ ? ? public static DataSource getDataSource() { ? ? ? ? return dataSource; ? ? } }
# druid.properties配置 driverClassName=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/數(shù)據(jù)庫(kù)?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC username=用戶名 password=密碼 initialSize=10 maxActive=50 maxWait=60000
2、編寫(xiě)UserService服務(wù)類
public void InsertUsersByJdbc() { long start = System.currentTimeMillis(); Connection connection = null; PreparedStatement ps = null; try { connection = JDBCDruidUtils.getConnection(); //控制事務(wù):默認(rèn)不提交 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 對(duì)象的批處理命令中。 ps.addBatch(); } //執(zhí)行批處理 ps.executeBatch(); //手動(dòng)提交事務(wù) connection.commit(); } catch (SQLException e) { throw new RuntimeException(e); } finally { JDBCDruidUtils.close(connection, ps); } long end = System.currentTimeMillis(); System.out.println("一萬(wàn)條數(shù)據(jù)總耗時(shí):" + (end - start) + "ms"); }
3、輸出結(jié)果
1萬(wàn)數(shù)據(jù)總耗時(shí)19000ms。
3 總結(jié)
大量數(shù)據(jù)的場(chǎng)景下性能對(duì)比:InsertBatchSomeColumn>自定義xml以集合的方式>Jdbc原生>SaveBatch>手動(dòng)for循環(huán)批量>自動(dòng)for循環(huán)批量。
網(wǎng)上很多人都說(shuō)JDBC原生性能很好,但是我發(fā)現(xiàn)其非常差,有可能是我使用的是mybatis-plus依賴,如果這是推論正確,那就可以證明mybatis-plus在mybatis的基礎(chǔ)上不僅增強(qiáng)了功能也增強(qiáng)了性能。所以可以得出結(jié)論:開(kāi)發(fā)中用mybatis-plus是沒(méi)有錯(cuò)的,如果想提高性能,只能實(shí)施其他方案,比如分庫(kù)分表,千萬(wàn)別想著JDBC原生性能更好。
到此這篇關(guān)于MyBatis-plus的五種批量插入方式對(duì)比分析的文章就介紹到這了,更多相關(guān)MyBatis-plus 批量插入內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java 判斷實(shí)體對(duì)象及所有屬性是否為空的操作
這篇文章主要介紹了Java 判斷實(shí)體對(duì)象及所有屬性是否為空的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12windows10 JDK安裝及配置環(huán)境變量與Eclipse安裝教程
這篇文章主要介紹了windows10 JDK安裝及配置環(huán)境變量與Eclipse安裝,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10Ubuntu安裝JDK與IntelliJ?IDEA的詳細(xì)過(guò)程
APT是Linux系統(tǒng)上的包管理工具,能自動(dòng)解決軟件包依賴關(guān)系并從遠(yuǎn)程存儲(chǔ)庫(kù)中獲取安裝軟件包,這篇文章主要介紹了Ubuntu安裝JDK與IntelliJ?IDEA的過(guò)程,需要的朋友可以參考下2023-08-08Java依賴-關(guān)聯(lián)-聚合-組合之間區(qū)別_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Java依賴-關(guān)聯(lián)-聚合-組合之間區(qū)別理解,依賴關(guān)系比較好區(qū)分,它是耦合度最弱的一種,下文給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2017-08-08Java得到一個(gè)整數(shù)的絕對(duì)值,不使用任何判斷和比較語(yǔ)句,包括API
Java得到一個(gè)整數(shù)的絕對(duì)值,不使用任何判斷和比較語(yǔ)句,包括API2009-09-09springboot tomcat最大線程數(shù)與最大連接數(shù)解析
這篇文章主要介紹了springboot tomcat最大線程數(shù)與最大連接數(shù)解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06Mybatis千萬(wàn)級(jí)數(shù)據(jù)查詢的解決方式,避免OOM問(wèn)題
這篇文章主要介紹了Mybatis千萬(wàn)級(jí)數(shù)據(jù)查詢的解決方式,避免OOM問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01