Mybatis批量插入1万条数据,是真的实用

前言之前聊到自己做过的功能优化,就说了通讯录同步的优化,详细见通讯录同步效率优化,提到用Mybatis批量插入数据,把上限1万条数据一次性的插入到表中。面试官对一次性插入1万条数据有疑问,认为不可以插入这么多数据,但是我做这个功能的时候确实是成功的,那具体能一次插入数据的上限我也不确定,后面就找时间做了下面这个实验。首先自己搭建了SpringBoot+Mybatis的项目测试的,搭建步骤如下1. 搭建测试工程idea构建SpringBoot+MyBatis项目gitee上代码:http
摘要由CSDN通过智能技术生成

前言

之前聊到自己做过的功能优化,就说了通讯录同步的优化,详细见通讯录同步效率优化,提到用Mybatis批量插入数据,把上限1万条数据一次性的插入到表中。面试官对一次性插入1万条数据有疑问,认为不可以插入这么多数据,但是我做这个功能的时候确实是成功的,那具体能一次插入数据的上限我也不确定,后面就找时间做了下面这个实验。

首先自己搭建了SpringBoot+Mybatis的项目测试的,搭建步骤如下

1. 搭建测试工程

idea构建SpringBoot+MyBatis项目

gitee上代码:https://gitee.com/AJiSun/SpringBoot-MyBatis

File->New->Project

image.png

image.png

image.png

依赖:不选也行,后续在pom中添加,这里就选了一个mysql的依赖

image.png

image.png

添加需要的pom

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.0</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
复制代码

然后就是新建文件夹,新建需要的文件,我的目录结构如下

image.png

application.yml中的配置

server:
port: 7070
spring:
application:
  name: ajisun-mybatis
datasource:
  driver-class-name: com.mysql.jdbc.Driver
  url: jdbc:mysql://localhost:3306/ajisun_mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=false&useInformationSchema=true
  username: root
  password: root
​
mybatis:
mapperLocations: classpath:mapper/*.xml
typeAliasesPackage: com.ajisun.coding.ajisunmybatis.entity
 #开启驼峰命名
configuration:
    map-underscore-to-camel-case: true
复制代码

启动类中加上注解@MapperScan(启动的时候能够扫描到mapper)

@SpringBootApplication
@MapperScan("com.ajisun.coding.ajisunmybatis.mapper")
public class AjisunMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(AjisunMybatisApplication.class, args);
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Oracle的批量更新功能和MyBatis来实现批量更新几万条数据。 首先,你需要在MyBatis的映射文件中编写一个更新语句,例如: ```xml <update id="batchUpdate" parameterType="java.util.List"> <!-- 这里是你的更新语句,使用动态SQL来处理批量更新 --> UPDATE your_table SET column1 = #{listItem.value1}, column2 = #{listItem.value2} WHERE id = #{listItem.id} </update> ``` 然后,在Java代码中,你可以使用MyBatis的`SqlSession`对象来批量执行该更新语句。你需要将要更新的数据封装到一个List中,然后调用`update`方法来执行批量更新,示例如下: ```java List<YourEntity> dataList = new ArrayList<>(); // 要更新的数据列表 for (int i = 0; i < dataSize; i++) { YourEntity entity = new YourEntity(); // 设置要更新的字段值 entity.setId(idList.get(i)); entity.setValue1(value1List.get(i)); entity.setValue2(value2List.get(i)); dataList.add(entity); } try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH)) { // 批量更新 int batchSize = 1000; // 每批次更新的数据量 int totalSize = dataList.size(); int batchCount = totalSize / batchSize + (totalSize % batchSize == 0 ? 0 : 1); for (int i = 0; i < batchCount; i++) { int fromIndex = i * batchSize; int toIndex = Math.min((i + 1) * batchSize, totalSize); List<YourEntity> batchList = dataList.subList(fromIndex, toIndex); sqlSession.update("yourMapper.batchUpdate", batchList); sqlSession.commit(); // 提交事务 sqlSession.clearCache(); // 清理缓存 } } ``` 这样就实现了批量更新几万条数据的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值