您的位置:首页技术文章
文章详情页

spring中使用mybatis实现批量插入的示例代码

【字号: 日期:2023-09-04 08:26:00浏览:4作者:猪猪

有3种实现方式:foreach,spring事务,以及ExecutorType.BATCH.

1. foreach方式

这种方式实际是对SQL语句进行拼接,生成一个长长的SQL,对很多变量进行绑定。如果数据量不大(1000个以内),可以用这种方式。如果数据量太大,可能数据库会报错。

定义接口

public interface StudentMapper05 { public void insertStudent(List<Student> studentList);}

定义mapper

适用于Oracle数据库

<insert id='insertStudent'> BEGIN <foreach collection='list' item='student' index='index' separator=''> INSERT INTO test_student(ID, NAME, BRANCH, PERCENTAGE, PHONE, EMAIL) VALUES (SEQ_ID.nextval, #{student.name}, #{student.branch}, #{student.percentage}, #{student.phone}, #{student.email}); </foreach> END;</insert>

这个mapper的含义,就是把上送的studentList拼接成一个长SQL,拼成的SQL类似:

BEGININSERT INTO test_student(ID, NAME, BRANCH, PERCENTAGE, PHONE, EMAIL) VALUES (SEQ_ID.nextval, ?, ?, ?, ?, ?);INSERT INTO test_student(ID, NAME, BRANCH, PERCENTAGE, PHONE, EMAIL) VALUES (SEQ_ID.nextval, ?, ?, ?, ?, ?);INSERT INTO test_student(ID, NAME, BRANCH, PERCENTAGE, PHONE, EMAIL) VALUES (SEQ_ID.nextval, ?, ?, ?, ?, ?);...END;

studentList有几个,就会生成多少个insert语句拼接到一起,每个?都会进行变量绑定,所以当studentList中数据量较多时,生成的SQL会很长,导致数据库执行报错。

dao

public class StudentDao05 { private StudentMapper05 studentMapper; // 省略getter和setter public void insertStudentList(List<Student> studentList) { studentMapper.insertStudent(studentList); }}

beans

mybatis-spring-05.xml:

<bean class='org.mybatis.spring.SqlSessionFactoryBean'> <property name='dataSource' ref='oracleDataSource' /> <property name='configLocation' value='classpath:mybatis/config/mybatis-config-05.xml'/></bean><bean class='org.mybatis.spring.mapper.MapperFactoryBean'> <property name='mapperInterface' value='com.ws.experiment.spring.mybatis.mapper.StudentMapper05' /> <property name='sqlSessionFactory' ref='sqlSessionFactory' /></bean><bean class='com.ws.experiment.spring.mybatis.dao.StudentDao05'> <property name='studentMapper' ref='studentMapper05' /></bean>

main函数

public static void main(String[] args) { String[] configFiles = new String[]{'spring-beans-config.xml', 'mybatis/mybatis-spring-05.xml'}; // 分别配置datasource和mybatis相关bean ApplicationContext context = new ClassPathXmlApplicationContext(configFiles); StudentDao05 studentDao = (StudentDao05)context.getBean('studentDao05'); int counts[] = new int[]{10, 50, 100, 200, 500, 1000, 2000, 3000, 5000, 8000}; for (int count : counts) { List<Student> studentList = new ArrayList<>(); for (int i = 0; i < count; i++) { Student st = new Student(); st.setName('name'); st.setBranch(''); st.setEmail(''); st.setPercentage(0); st.setPhone(0); studentList.add(st); } long startTime = System.currentTimeMillis(); studentDao.insertStudentList(studentList); long endTime = System.currentTimeMillis(); System.out.println('插入' + count + '笔数据耗时: ' + (endTime - startTime) +' ms'); }}

测试结果

插入100笔数据耗时: 197 ms插入200笔数据耗时: 232 ms插入500笔数据耗时: 421 ms插入1000笔数据耗时: 650 ms插入2000笔数据耗时: 1140 ms插入3000笔数据耗时: 27113 ms插入5000笔数据耗时: 98213 ms插入8000笔数据耗时: 301101 ms

2. 借助spring事务

借助spring事务,插入一组数据

开启spring事务

<bean class='org.springframework.jdbc.datasource.DataSourceTransactionManager'> <property name='dataSource' ref='oracleDataSource' /></bean><tx:annotation-driven transaction-manager='transactionManager' />

定义接口

public interface StudentMapper06 { public void insertStudent(@Param('student') Student student);}

mapper

<insert id='insertStudent'> INSERT INTO test_student(ID, NAME, BRANCH, PERCENTAGE, PHONE, EMAIL) VALUES (SEQ_ID.nextval, #{student.name}, #{student.branch}, #{student.percentage}, #{student.phone}, #{student.email})</insert>

dao

public class StudentDao06 { private StudentMapper06 studentMapper; // 省略getter和setter @Transactional // spring事务控制 public void insertStudentList(List<Student> students) { for (Student student : students) { studentMapper.insertStudent(student); } }}

beans

<bean class='org.mybatis.spring.SqlSessionFactoryBean'> <property name='dataSource' ref='oracleDataSource' /> <property name='configLocation' value='classpath:mybatis/config/mybatis-config-06.xml'/></bean><bean class='org.mybatis.spring.mapper.MapperFactoryBean'> <property name='mapperInterface' value='com.ws.experiment.spring.mybatis.mapper.StudentMapper06' /> <property name='sqlSessionFactory' ref='sqlSessionFactory' /></bean><bean class='com.ws.experiment.spring.mybatis.dao.StudentDao06'> <property name='studentMapper' ref='studentMapper06' /></bean>

main

测试结果

batchInsert001插入10笔数据耗时: 602 msbatchInsert001插入50笔数据耗时: 196 msbatchInsert001插入100笔数据耗时: 284 msbatchInsert001插入200笔数据耗时: 438 msbatchInsert001插入500笔数据耗时: 944 msbatchInsert001插入1000笔数据耗时: 1689 msbatchInsert001插入2000笔数据耗时: 3138 msbatchInsert001插入3000笔数据耗时: 4427 msbatchInsert001插入5000笔数据耗时: 7368 msbatchInsert001插入8000笔数据耗时: 11832 ms

3. 使用ExecutorType.BATCH

基本原理是SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);,设置BATCH方式的sqlSession

有三种设置方式:

3.1 在mybatis的config文件中设置

SqlSessionFactoryBean中可以配置配置文件:

<bean class='org.mybatis.spring.SqlSessionFactoryBean'> <property name='dataSource' ref='oracleDataSource' /> <property name='configLocation' value='classpath:mybatis/config/mybatis-config-06.xml'/></bean>

这个mybatis配置文件中,设置BATCH方式:

<configuration> <settings> <!-- 默认打开BATCH的Executor --> <setting name='defaultExecutorType' value='BATCH' /> </settings> <mappers> <mapper /> </mappers></configuration>

这样,默认打开的sqlSession就都是BATCH方式的。再与spring的事务结合(参看上一节中的spring事务设置),就可以实现批量插入。

测试结果:

batchInsert001插入10笔数据耗时: 565 msbatchInsert001插入50笔数据耗时: 117 msbatchInsert001插入100笔数据耗时: 98 msbatchInsert001插入200笔数据耗时: 106 msbatchInsert001插入500笔数据耗时: 145 msbatchInsert001插入1000笔数据耗时: 132 msbatchInsert001插入2000笔数据耗时: 154 msbatchInsert001插入3000笔数据耗时: 163 msbatchInsert001插入5000笔数据耗时: 200 msbatchInsert001插入8000笔数据耗时: 250 ms

3.2 自己创建sqlSession,手工commit

SqlSessionFactory sqlSessionFactory = (SqlSessionFactory)context.getBean('sqlSessionFactory');SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);StudentMapper06 studentMapper = sqlSession.getMapper(StudentMapper06.class);for (int i = 0; i < count; i++) { Student st = new Student(); st.setName('name'); ... studentMapper.insertStudent(st);}sqlSession.commit();sqlSession.clearCache();sqlSession.close();

测试结果:

batchInsert002插入10笔数据耗时: 568 msbatchInsert002插入50笔数据耗时: 157 msbatchInsert002插入100笔数据耗时: 132 msbatchInsert002插入200笔数据耗时: 135 msbatchInsert002插入500笔数据耗时: 148 msbatchInsert002插入1000笔数据耗时: 139 msbatchInsert002插入2000笔数据耗时: 151 msbatchInsert002插入3000笔数据耗时: 139 msbatchInsert002插入5000笔数据耗时: 207 msbatchInsert002插入8000笔数据耗时: 299 ms

3.3 使用sqlSessionTemplate在XML文件中创建bean

创建一个SqlSessionTemplate,然后注入到MapperFactoryBean中,生成对应的mapper:

<!-- 以ExecutorType.BATCH方式插入数据库 --><bean class='org.mybatis.spring.SqlSessionTemplate'> <constructor-arg name='sqlSessionFactory' ref='sqlSessionFactory' /> <constructor-arg name='executorType' value='BATCH' /></bean><bean class='org.mybatis.spring.mapper.MapperFactoryBean'> <property name='mapperInterface' value='com.ws.experiment.spring.mybatis.mapper.StudentMapper06' /> <property name='sqlSessionTemplate' ref='batchSqlSessionTemplate' /></bean><bean class='com.ws.experiment.spring.mybatis.dao.StudentDao06'> <property name='studentMapper' ref='studentMapper06_batch' /></bean>

与spring的事务结合后(参看上一节中的spring事务设置),就可以实现批量插入

测试结果

batchInsert003插入10笔数据耗时: 651 msbatchInsert003插入50笔数据耗时: 133 msbatchInsert003插入100笔数据耗时: 124 msbatchInsert003插入200笔数据耗时: 129 msbatchInsert003插入500笔数据耗时: 144 msbatchInsert003插入1000笔数据耗时: 179 msbatchInsert003插入2000笔数据耗时: 229 msbatchInsert003插入3000笔数据耗时: 241 msbatchInsert003插入5000笔数据耗时: 216 msbatchInsert003插入8000笔数据耗时: 259 ms

到此这篇关于spring中使用mybatis实现批量插入的示例代码的文章就介绍到这了,更多相关spring mybatis批量插入内容请搜索好吧啦网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好吧啦网!

标签: Spring
相关文章: