網(wǎng)站首頁 編程語言 正文
一、準(zhǔn)備工作
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ù)類,測(cè)試一萬條數(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.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、進(jìn)行單元測(cè)試
@SpringBootTest
class DemoApplicationTests {
@Resource
private UserService userService;
@Test
public void insert(){
userService.InsertUsers();
}
}
2.5、結(jié)果輸出
一萬條數(shù)據(jù)總耗時(shí):26348ms
三、MyBatis的手動(dòng)批量提交
3.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("一萬條數(shù)據(jù)總耗時(shí):" + (end-start) + "ms" );
}
}
3.2、結(jié)果輸出
一萬條數(shù)據(jù)總耗時(shí):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ù)總耗時(shí):" + (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ù)總耗時(shí):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ù)總耗時(shí):" + (end-start) + "ms" );
}
}
5.2、編寫UserMapper接口
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
5.3、單元測(cè)試結(jié)果
一萬條數(shù)據(jù)總耗時(shí):24674ms
六、MyBatis-Plus提供的InsertBatchSomeColumn方法(推薦)
6.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;
}
}
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ù)總耗時(shí):" + (end-start) + "ms" );
}
}
6.4、編寫EasyBaseMapper接口
public interface EasyBaseMapper<T> extends BaseMapper<T> {
/**
* 批量插入 僅適用于mysql
*
* @param entityList 實(shí)體列表
* @return 影響行數(shù)
*/
Integer insertBatchSomeColumn(Collection<T> entityList);
}
6.5、編寫UserMapper接口
@Mapper
public interface UserMapper<T> extends EasyBaseMapper<User> {
}
6.6、單元測(cè)試結(jié)果
一萬條數(shù)據(jù)總耗時(shí):575ms
原文鏈接:https://blog.csdn.net/qq_19891197/article/details/128463552
- 上一篇:沒有了
- 下一篇:沒有了
相關(guān)推薦
- 2021-12-06 Ubuntu編譯內(nèi)核模塊,內(nèi)容體現(xiàn)系統(tǒng)日志中_Linux
- 2022-05-26 Flutter實(shí)現(xiàn)抽屜動(dòng)畫_Android
- 2022-12-29 React引入css的三種方式小結(jié)_React
- 2022-09-14 Redis核心原理詳細(xì)解說_Redis
- 2022-12-11 React?state狀態(tài)屬性用法講解_React
- 2022-11-01 Python如何使用qrcode生成指定內(nèi)容的二維碼并在GUI界面顯示_python
- 2022-09-03 Python?groupby函數(shù)圖文詳解_python
- 2022-06-08 Python使用PyYAML庫讀寫yaml文件的方法_python
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支