测试类:
public class StudentTest { private static Logger logger = Logger.getLogger(StudentTest.class); private SqlSession sqlSession = null; private StudentMapper studentMapper = null; /** * 测试方法前调用 * * @throws Exception */ @Before public void setUp() throws Exception { sqlSession = SqlSessionFactoryUtil.openSession(); studentMapper = sqlSession.getMapper(StudentMapper.class); } /** * 测试方法后调用 * * @throws Exception */ @After public void tearDown() throws Exception { sqlSession.close(); } @Test public void testBatchUpdateStudent1() { logger.info("更新学生(带条件)"); List<Integer> list = new ArrayList<Integer>(); list.add(5); list.add(7); int n = studentMapper.batchUpdate1(list); System.out.println("成功更新" + n + "条记录"); sqlSession.commit(); } @Test public void batchInsertStudent() { List<Student> list = new ArrayList<Student>(); for (int i = 15; i < 18; i++) { Student student = new Student(); student.setId(i); student.setName("test" + i); list.add(student); } int n = studentMapper.batchInsertStudent(list); System.out.println("成功插入" + n + "条记录"); sqlSession.commit(); } @Test public void testBatchUpdate1() { logger.info("更新学生(带条件)"); List<Student> list = new ArrayList<Student>(); list.add(new Student(8, "张三aa")); list.add(new Student(9, "李四aa")); int n = studentMapper.batchUpdate2(list); System.out.println("成功更新" + n + "条记录"); sqlSession.commit(); } @Test public void testBatchUpdateStudent2() { logger.info("更新学生(带条件)"); List<Student> list = new ArrayList<Student>(); list.add(new Student(8, "bb")); list.add(new Student(9, "cc")); int n = studentMapper.batchUpdate2(list); System.out.println("成功更新" + n + "条记录"); sqlSession.commit(); } @Test public void batchUpdateStudentWithMap() { List<Integer> ls = new ArrayList<Integer>(); for (int i = 8; i < 10; i++) { ls.add(i); } Map<String, Object> map = new HashMap<String, Object>(); map.put("idList", ls); map.put("name", "小群"); int n = studentMapper.batchUpdateStudentWithMap(map); System.out.println("成功更新" + n + "条记录"); sqlSession.commit(); } /** * 批量删除 */ @Test public void testBatchDelete1() { logger.info("更新学生(带条件)"); int array[] = new int[] { 3, 4 }; studentMapper.batchDelete1(array); sqlSession.commit(); } @Test public void testBatchDelete2() { List<Integer> list = new ArrayList<Integer>(); for (int i = 0; i <= 3; i++) { list.add(i); } studentMapper.batchDelete2(list); sqlSession.commit(); } @Test public void testBatchDelete3() { List<Student> list = new ArrayList<Student>(); for (int i = 5; i <= 7; i++) { Student vo = new Student(); vo.setId(i); list.add(vo); } studentMapper.batchDelete3(list); sqlSession.commit(); } @Test // 不推荐使用此种方式 public void testBatchDelete4() { List<Student> list = new ArrayList<Student>(); for (int i = 5; i <= 7; i++) { Student vo = new Student(); vo.setId(i); list.add(vo); } studentMapper.batchDelete4(list); sqlSession.commit(); } }
实体类:
public class Student implements Serializable { private Integer id; private String name; private byte[] pic;// blob private String remark;// clob public Student() { super(); // TODO Auto-generated constructor stub } public Student(Integer id, String name) { super(); this.id = id; this.name = name; } public Student(String namee) { super(); this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public byte[] getPic() { return pic; } public void setPic(byte[] pic) { this.pic = pic; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", remark=" + remark + "]"; } }
MyBatis接口
package com.mybatis.mappers; import java.util.List; import java.util.Map; import com.mybatis.model.Student; public interface StudentMapper { public int batchInsertStudent(List<Student> list); public int batchUpdate1(List<Integer> list); public int batchUpdate2(List<Student> list); public int batchUpdateStudentWithMap(Map<String, Object> map); public int batchDelete1(int array[]); public int batchDelete2(List<Integer> list); public int batchDelete3(List<Student> list); public int batchDelete4(List<Student> list); }
批量操作Mapper
<?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="com.mybatis.mappers.StudentMapper"> <!-- 1,size:表示缓存cache中能容纳的最大元素数。默认是1024; 2,flushInterval:定义缓存刷新周期,以毫秒计; 3,eviction:定义缓存的移除机制;默认是LRU(least recently userd,最近最少使用),还有FIFO(first in first out,先进先出) 4,readOnly:默认值是false,假如是true的话,缓存只能读。 --> <cache size="1024" flushInterval="60000" eviction="LRU" readOnly="false" /> <resultMap type="Student" id="StudentResult"> <id property="id" column="id" /> <result property="name" column="name" /> </resultMap> <!-- 批量更新 --> <insert id="batchInsertStudent" parameterType="List"> insert /*+append_values */ into t_student(id,name) <foreach collection="list" item="item" index="index" separator="union all"> select #{item.id}, #{item.name} from dual </foreach> </insert> <update id="batchUpdate1" parameterType="java.util.List"> update t_student set name='test' where id in <foreach collection="list" item="idItem" index="index" open="(" separator="," close=")"> #{idItem} </foreach> </update> <update id="batchUpdate2" parameterType="java.util.List"> <foreach collection="list" item="item" index="index" open="begin" close=";end;" separator=";"> update t_student <set> name = #{item.name} </set> where id = #{item.id} </foreach> </update> <!-- 根据id,批理更改字段值 --> <update id="batchUpdateStudentWithMap" parameterType="java.util.Map"> update t_student set name = #{name} where id in <foreach collection="idList" index="index" item="iditem" open="(" separator="," close=")"> #{iditem} </foreach> </update> <!-- t_stude:表名 id :字段名 collection里表示类型,这里是array,还可以是list idItem不用管,相当于一个变量 --> <delete id="batchDelete1" parameterType="java.lang.String"> delete from t_student where id in <foreach item="idItem" collection="array" open="(" separator="," close=")"> #{idItem} </foreach> </delete> <!-- 推荐 --> <delete id="batchDelete2" parameterType="java.util.List"> delete from t_student where id in <foreach collection="list" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </delete> <!-- 推荐 --> <delete id="batchDelete3" parameterType="java.util.List"> delete from t_student where id in <foreach collection="list" index="index" item="item" open="(" separator="," close=")"> #{item.id} </foreach> </delete> <!--效率低,不推荐 --> <delete id="batchDelete4" parameterType="java.util.List"> <foreach collection="list" item="item" index="index" open="begin" close=";end;" separator=";"> delete from t_student where id = #{item.id} </foreach> </delete> </mapper>
SqlSessionFactoryUtil
public class SqlSessionFactoryUtil { private static SqlSessionFactory sqlSessionFactory; public static SqlSessionFactory getSqlSessionFactory() { if (sqlSessionFactory == null) { InputStream inputStream = null; try { inputStream = Resources.getResourceAsStream("mybatis-config.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (Exception e) { e.printStackTrace(); } } return sqlSessionFactory; } public static SqlSession openSession() { return getSqlSessionFactory().openSession(); } }