当前位置: 移动技术网 > IT编程>开发语言>Java > spring中使用mybatis实现批量插入的示例代码

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

2020年06月13日  | 移动技术网IT编程  | 我要评论

有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类似:

begin
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, ?, ?, ?, ?, ?);
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 id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean">
  <property name="datasource" ref="oracledatasource" />
  <property name="configlocation" value="classpath:mybatis/config/mybatis-config-05.xml"/>
</bean>
<bean id="studentmapper05" class="org.mybatis.spring.mapper.mapperfactorybean">
  <property name="mapperinterface" value="com.ws.experiment.spring.mybatis.mapper.studentmapper05" />
  <property name="sqlsessionfactory" ref="sqlsessionfactory" />
</bean>
<bean id="studentdao05" 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 id="transactionmanager" 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 id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean">
  <property name="datasource" ref="oracledatasource" />
  <property name="configlocation" value="classpath:mybatis/config/mybatis-config-06.xml"/>
</bean>
<bean id="studentmapper06" class="org.mybatis.spring.mapper.mapperfactorybean">
  <property name="mapperinterface" value="com.ws.experiment.spring.mybatis.mapper.studentmapper06" />
  <property name="sqlsessionfactory" ref="sqlsessionfactory" />
</bean>
<bean id="studentdao06" class="com.ws.experiment.spring.mybatis.dao.studentdao06">
  <property name="studentmapper" ref="studentmapper06" />
</bean>

main

测试结果

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

3. 使用executortype.batch

基本原理是sqlsession sqlsession = sqlsessionfactory.opensession(executortype.batch, false);,设置batch方式的sqlsession

有三种设置方式:

3.1 在mybatis的config文件中设置

sqlsessionfactorybean中可以配置配置文件:

<bean id="sqlsessionfactory" 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 class="com.ws.experiment.spring.mybatis.mapper.studentmapper06" />
  </mappers>
</configuration>

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

测试结果:

batchinsert001插入10笔数据耗时: 565 ms
batchinsert001插入50笔数据耗时: 117 ms
batchinsert001插入100笔数据耗时: 98 ms
batchinsert001插入200笔数据耗时: 106 ms
batchinsert001插入500笔数据耗时: 145 ms
batchinsert001插入1000笔数据耗时: 132 ms
batchinsert001插入2000笔数据耗时: 154 ms
batchinsert001插入3000笔数据耗时: 163 ms
batchinsert001插入5000笔数据耗时: 200 ms
batchinsert001插入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 ms
batchinsert002插入50笔数据耗时: 157 ms
batchinsert002插入100笔数据耗时: 132 ms
batchinsert002插入200笔数据耗时: 135 ms
batchinsert002插入500笔数据耗时: 148 ms
batchinsert002插入1000笔数据耗时: 139 ms
batchinsert002插入2000笔数据耗时: 151 ms
batchinsert002插入3000笔数据耗时: 139 ms
batchinsert002插入5000笔数据耗时: 207 ms
batchinsert002插入8000笔数据耗时: 299 ms

3.3 使用sqlsessiontemplate在xml文件中创建bean

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

<!-- 以executortype.batch方式插入数据库 -->
<bean id="batchsqlsessiontemplate" class="org.mybatis.spring.sqlsessiontemplate">
  <constructor-arg name="sqlsessionfactory" ref="sqlsessionfactory" />
  <constructor-arg name="executortype" value="batch" />
</bean>
<bean id="studentmapper06_batch" class="org.mybatis.spring.mapper.mapperfactorybean">
  <property name="mapperinterface" value="com.ws.experiment.spring.mybatis.mapper.studentmapper06" />
  <property name="sqlsessiontemplate" ref="batchsqlsessiontemplate" />
</bean>
<bean id="studentdao06_batch" class="com.ws.experiment.spring.mybatis.dao.studentdao06">
  <property name="studentmapper" ref="studentmapper06_batch" />
</bean>

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

测试结果

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

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网