MyBatis批量插入的五種方式
一:預(yù)備工作
1:pom依賴:
<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>2:配置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/*.xml3:公用的User類:
@Data
public class User {
private int id;
private String username;
private String password;
}二、不同的實(shí)現(xiàn)方法
1:MyBatis利用For循環(huán)批量插入
(1)、編寫UserService服務(wù)類,測試一萬條數(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("一萬條數(shù)據(jù)總耗時(shí):" + (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)、進(jìn)行單元測試
@SpringBootTest
class DemoApplicationTests {
@Resource
private UserService userService;
@Test
public void insert(){
userService.InsertUsers();
}
}(5)、結(jié)果輸出
一萬條數(shù)據(jù)總耗時(shí):26348ms
2:MyBatis的手動批量提交
(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ù)總耗時(shí):" + (end-start) + "ms" );
}
}(2)、結(jié)果輸出
一萬條數(shù)據(jù)總耗時(shí):24516ms
3:MyBatis以集合方式批量新增(推薦)
(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ù)總耗時(shí):" + (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)、輸出結(jié)果
一萬條數(shù)據(jù)總耗時(shí):521ms
4:MyBatis-Plus提供的SaveBatch方法
(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ù)總耗時(shí):" + (end-start) + "ms" );
}
}(2)、編寫UserMapper接口
@Mapper
public interface UserMapper extends BaseMapper<User> {
}(3)、單元測試結(jié)果
一萬條數(shù)據(jù)總耗時(shí):24674ms
5:MyBatis-Plus提供的InsertBatchSomeColumn方法(推薦)
(1)、編寫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)、編寫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ù)總耗時(shí):" + (end-start) + "ms" );
}
}(4)、編寫EasyBaseMapper接口
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ù)總耗時(shí):" + (end-start) + "ms" );
}
}(5)、編寫UserMapper接口
@Mapper
public interface UserMapper<T> extends EasyBaseMapper<User> {
}(6)、單元測試結(jié)果
一萬條數(shù)據(jù)總耗時(shí):575ms
到此這篇關(guān)于MyBatis批量插入的五種方式的文章就介紹到這了,更多相關(guān)MyBatis批量插入內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring入門實(shí)戰(zhàn)之Profile詳解
什么是spring profile?簡單講profile就是一組配置,不同profile提供不同組合的配置,程序運(yùn)行時(shí)可以選擇使用哪些profile來適應(yīng)環(huán)境。下面這篇文章主要介紹了Spring中Profile實(shí)戰(zhàn)的相關(guān)資料,需要的朋友可以參考借鑒。2017-02-02
Java并發(fā)工具之Exchanger線程間交換數(shù)據(jù)詳解
這篇文章主要介紹了Java并發(fā)工具之Exchanger線程間交換數(shù)據(jù)詳解,Exchanger是一個(gè)用于線程間協(xié)作的工具類,Exchanger用于進(jìn)行線程間的數(shù)據(jù)交 換,它提供一個(gè)同步點(diǎn),在這個(gè)同步點(diǎn),兩個(gè)線程可以交換彼此的數(shù)據(jù),需要的朋友可以參考下2023-12-12
如何使用IDEA開發(fā)Spark SQL程序(一文搞懂)
Spark SQL 是一個(gè)用來處理結(jié)構(gòu)化數(shù)據(jù)的spark組件。它提供了一個(gè)叫做DataFrames的可編程抽象數(shù)據(jù)模型,并且可被視為一個(gè)分布式的SQL查詢引擎。這篇文章主要介紹了如何使用IDEA開發(fā)Spark SQL程序(一文搞懂),需要的朋友可以參考下2021-08-08
淺談Java中的final關(guān)鍵字與C#中的const, readonly關(guān)鍵字
下面小編就為大家?guī)硪黄獪\談Java中的final關(guān)鍵字與C#中的const, readonly關(guān)鍵字。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10
IDEA2020.1啟動SpringBoot項(xiàng)目出現(xiàn)java程序包:xxx不存在
這篇文章主要介紹了IDEA2020.1啟動SpringBoot項(xiàng)目出現(xiàn)java程序包:xxx不存在,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06

