先记录一下参考的博客:
Mybatis 批量插入、存储过程
这篇博客介绍了批量插入的几种方式:simple方式(基本数据库逐条访问)、batch方式(批量操作)、procedure方式(存储过程)
并且通过测试,说明了他们的效率
MyBatis的关于批量数据操作的体会
这篇博客介绍了专门为批量操作准备的方法和普通方法的对比,
session = sessionFactory.openSession(ExecutorType.BATCH, true);//用于批量update
http://blog.csdn.net/rootsuper/article/category/1333766
teacher-mapper.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="org.ygy.demo.batch.mapper.TeacherMapper"> <resultMap type="TeacherEntity" id="TeacherMap"> <id column="TEACHER_ID" property="id"/> <result column="TEACHER_NAME" property="name"/> <collection property="students" resultMap="org.ygy.demo.mybatis.revelance.mapper.StudentMapper.StudentMap"></collection> </resultMap> <select id="queryAll" resultMap="TeacherMap"> SELECT STUDENT.ID STUDENT_ID , STUDENT.NAME STUDENT_NAME ,TEACHER.ID TEACHER_ID , TEACHER.NAME TEACHER_NAME FROM T_YGY_DEMO_TEACHER TEACHER JOIN T_YGY_DEMO_STUDENT STUDENT ON STUDENT.TEACHERID = TEACHER.ID </select> <!-- 批量插入,手动插入主键 --> <insert id="batchInsert" parameterType="list"> INSERT INTO T_YGY_DEMO_TEACHER(ID , NAME) <foreach collection="list" item="teacher" separator="union all"> SELECT #{teacher.id} , #{teacher.name} FROM DUAL </foreach> </insert> <!-- 批量删除 --> <delete id="batchDelete" parameterType="list"> DELETE FROM T_YGY_DEMO_TEACHER WHERE ID IN <foreach collection="list" item="teacher" open="(" separator="," close=")"> #{teacher.id} </foreach> </delete> </mapper>
package org.ygy.demo.batch.dao.impl; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.ygy.demo.batch.dao.ITeacherDao; import org.ygy.demo.batch.entity.TeacherEntity; import org.ygy.demo.mybatis.common.util.MyBatisUtil; public class TeacherDao implements ITeacherDao { private static final String NAMESPACE = "org.ygy.demo.batch.mapper.TeacherMapper"; @Override public List<TeacherEntity> queryAll() { SqlSession session = MyBatisUtil.getSession(); List<TeacherEntity> teachers = session.selectList(NAMESPACE + ".queryAll"); session.close(); return teachers; } @Override public void batchInsert(List<TeacherEntity> teachers) { SqlSession session = MyBatisUtil.getSession(); session.insert(NAMESPACE + ".batchInsert" , teachers); session.commit(); session.close(); } @Override public void batchDelete(List<TeacherEntity> teachers) { SqlSession session = MyBatisUtil.getSession(); session.delete(NAMESPACE + ".batchDelete" , teachers); session.commit(); session.close(); } }
MyBatis批量插入(Oracle数据库)
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。foreach元素的属性主要有 item,index,collection,open,separator,close。item表示集合中每一个元素进行迭代时的别名,index指 定一个名字,用于表示在迭代过程中,每次迭代到的位置,open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔 符,close表示以什么结束,在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况 下,该属性的值是不一样的,主要有一下3种情况:
1.如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
2.如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
3.如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map
这是其他同学记录的错误,感觉可能会遇到,这里记录一下