发表于: 2020-06-16 23:41:30

1 1576


今天作为:spring和mybatis整合的批量插入数据。

使用<foreach>搭配集合插入数据

<!--批量插入数据-->
<insert id="insertBatch" >
   INSERT INTO student_task1
( student_name,enter_time,qq,school,student_number,study_type,log_link,slogan,brother)
VALUES
<foreach collection="list" item="student" separator=",">
       ( #{student.name}, #{student.enterTime},#{student.qqNumber},#{student.school},#{student.studentNumber},#{student.type},#{student.logLink},#{student.slogan},#{student.brother})
</foreach>
</insert>


引入sqlSessionTemplate设置为按批量提交。

<!--设置插入方法为批量插入,传入BATCH-->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
   <constructor-arg index="0" ref="sqlSessionFactory" />
   <constructor-arg index="1" value="BATCH" />

</bean>


<!--studentDao包传入模板-->
<bean id="studentDao" class="dao.StudentDaoImpl">
   <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
   <property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
</bean>


明天的计划:

1.把这个插数据测试索引任务完成。

2.学习github


遇到的问题:

1.mybatis和mybatis-spring这两个包的版本兼容问题。

解决办法:mybatis的3.3.0版本的包只能搭配mybatis-spring 1.3.0以下版本的包。

2.插入超过10万的数据时报下面这个错误。

解决思路:设置一个阈值分批提交数据。

3.因为设计的批量插入代码的逻辑不对,导致分批提交数据失败。


收获:学会了mybatis的批量插入数据。



返回列表 返回列表
评论

    分享到