MyBatis批量插入的五種方式
阿新 • • 發佈:2022-12-01
前言
這裡我列舉了MyBatis和MyBatis-Plus常用的五種批量插入的方式,進行了詳細的總結歸納。
- 準備工作
- MyBatis利用For迴圈批量插入
- MyBatis的手動批量提交
- MyBatis以集合方式批量新增(推薦)
- MyBatis-Plus提供的SaveBatch方法
- MyBatis-Plus提供的InsertBatchSomeColumn方法(推薦)
- 總結
一、準備工作
1、匯入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>
2、配置yml檔案
server:
port: 8080
spring:
datasource:
username: mysql使用者名稱
password: mysql密碼
url: jdbc:mysql://localhost:3306/資料庫名字?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:mapping/*.xml
3、公用的User類
@Data
public class User {
private int id;
private String username;
private String password;
}
二、MyBatis利用For迴圈批量插入
1、編寫UserService服務類,測試一萬條資料耗時情況
@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("一萬條資料總耗時:" + (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、結果輸出
一萬條資料總耗時:26348ms
三、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("一萬條資料總耗時:" + (end-start) + "ms" );
}
}
2、結果輸出
一萬條資料總耗時:24516ms
四、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("一萬條資料總耗時:" + (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、輸出結果
一萬條資料總耗時:521ms
五、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("一萬條資料總耗時:" + (end-start) + "ms" );
}
}
2、編寫UserMapper介面
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
3、單元測試結果
一萬條資料總耗時:24674ms
六、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("一萬條資料總耗時:" + (end-start) + "ms" );
}
}
4、編寫EasyBaseMapper介面
public interface EasyBaseMapper<T> extends BaseMapper<T> {
/**
* 批量插入 僅適用於mysql
*
* @param entityList 實體列表
* @return 影響行數
*/
Integer insertBatchSomeColumn(Collection<T> entityList);
}
5、編寫UserMapper介面
@Mapper
public interface UserMapper<T> extends EasyBaseMapper<User> {
}
6、單元測試結果
一萬條資料總耗時:575ms
七、總結
以上就是我對目前MyBatis常用的批量插入方法的總結
來源:blog.csdn.net/HJW_233/article/details/126909439