myBatis批量添加,修改和删除

1、批量添加元素session.insert(String string,Object o)

  1. public void batchInsertStudent(){ 
  2.     List<Student> ls = new ArrayList<Student>(); 
  3.     for(int i = 5;i < 8;i++){ 
  4.         Student student = new Student(); 
  5.         student.setId(i); 
  6.         student.setName("maoyuanjun" + i); 
  7.         student.setSex("man" + i); 
  8.         student.setTel("tel" + i); 
  9.         student.setAddress("浙江省" + i); 
  10.         ls.add(student); 
  11.     } 
  12.     SqlSession session = SessionFactoryUtil.getSqlSessionFactory().openSession(); 
  13.     session.insert("mybatisdemo.domain.Student.batchInsertStudent", ls); 
  14.     session.commit(); 
  15.     session.close(); 
  16.  
  17. <insert id="batchInsertStudent" parameterType="java.util.List"> 
  18.     INSERT INTO STUDENT (id,name,sex,tel,address) 
  19.     VALUES  
  20.     <foreach collection="list" item="item" index="index" separator="," > 
  21.         (#{item.id},#{item.name},#{item.sex},#{item.tel},#{item.address}) 
  22.     </foreach> 
  23. </insert> 
public void batchInsertStudent(){
	List<Student> ls = new ArrayList<Student>();
	for(int i = 5;i < 8;i++){
		Student student = new Student();
		student.setId(i);
		student.setName("maoyuanjun" + i);
		student.setSex("man" + i);
		student.setTel("tel" + i);
		student.setAddress("浙江省" + i);
		ls.add(student);
	}
	SqlSession session = SessionFactoryUtil.getSqlSessionFactory().openSession();
	session.insert("mybatisdemo.domain.Student.batchInsertStudent", ls);
	session.commit();
	session.close();
}

<insert id="batchInsertStudent" parameterType="java.util.List">
	INSERT INTO STUDENT (id,name,sex,tel,address)
	VALUES 
	<foreach collection="list" item="item" index="index" separator="," >
		(#{item.id},#{item.name},#{item.sex},#{item.tel},#{item.address})
	</foreach>
</insert>

2、批量修改session. insert (String string,Object o)
  1. 实例1: 
  2. public void batchUpdateStudent(){ 
  3.     List<Integer> ls = new ArrayList<Integer>(); 
  4.     for(int i = 2;i < 8;i++){ 
  5.         ls.add(i); 
  6.     } 
  7.     SqlSession session = SessionFactoryUtil.getSqlSessionFactory().openSession(); 
  8.     session.insert("mybatisdemo.domain.Student.batchUpdateStudent",ls); 
  9.     session.commit(); 
  10.     session.close(); 
  11. <update id="batchUpdateStudent" parameterType="java.util.List"> 
  12.     UPDATE STUDENT SET name = "5566" WHERE id IN 
  13.     <foreach collection="list" item="item" index="index" open="(" separator="," close=")" > 
  14.         #{item} 
  15.     </foreach> 
  16. </update> 
  17.  
  18. 实例2: 
  19. public void batchUpdateStudentWithMap(){ 
  20.     List<Integer> ls = new ArrayList<Integer>(); 
  21.     for(int i = 2;i < 8;i++){ 
  22.         ls.add(i); 
  23.     } 
  24.     Map<String,Object> map = new HashMap<String,Object>(); 
  25.     map.put("idList", ls); 
  26.     map.put("name", "mmao789"); 
  27.     SqlSession session = SessionFactoryUtil.getSqlSessionFactory().openSession(); 
  28.     session.insert("mybatisdemo.domain.Student.batchUpdateStudentWithMap",map); 
  29.     session.commit(); 
  30.     session.close(); 
  31. <update id="batchUpdateStudentWithMap" parameterType="java.util.Map" > 
  32.     UPDATE STUDENT SET name = #{name} WHERE id IN  
  33.     <foreach collection="idList" index="index" item="item" open="(" separator="," close=")">  
  34.         #{item}  
  35.     </foreach> 
  36. </update> 
实例1:
public void batchUpdateStudent(){
	List<Integer> ls = new ArrayList<Integer>();
	for(int i = 2;i < 8;i++){
		ls.add(i);
	}
	SqlSession session = SessionFactoryUtil.getSqlSessionFactory().openSession();
	session.insert("mybatisdemo.domain.Student.batchUpdateStudent",ls);
	session.commit();
	session.close();
}
<update id="batchUpdateStudent" parameterType="java.util.List">
	UPDATE STUDENT SET name = "5566" WHERE id IN
	<foreach collection="list" item="item" index="index" open="(" separator="," close=")" >
		#{item}
	</foreach>
</update>

实例2:
public void batchUpdateStudentWithMap(){
	List<Integer> ls = new ArrayList<Integer>();
	for(int i = 2;i < 8;i++){
		ls.add(i);
	}
	Map<String,Object> map = new HashMap<String,Object>();
	map.put("idList", ls);
	map.put("name", "mmao789");
	SqlSession session = SessionFactoryUtil.getSqlSessionFactory().openSession();
	session.insert("mybatisdemo.domain.Student.batchUpdateStudentWithMap",map);
	session.commit();
	session.close();
}
<update id="batchUpdateStudentWithMap" parameterType="java.util.Map" >
	UPDATE STUDENT SET name = #{name} WHERE id IN 
	<foreach collection="idList" index="index" item="item" open="(" separator="," close=")"> 
		#{item} 
	</foreach>
</update>

3、批量删除session.delete(String string,Object o)
  1. public void batchDeleteStudent(){ 
  2.     List<Integer> ls = new ArrayList<Integer>(); 
  3.     for(int i = 4;i < 8;i++){ 
  4.         ls.add(i); 
  5.     } 
  6.     SqlSession session = SessionFactoryUtil.getSqlSessionFactory().openSession(); 
  7.     session.delete("mybatisdemo.domain.Student.batchDeleteStudent",ls); 
  8.     session.commit(); 
  9.     session.close(); 
  10. <delete id="batchDeleteStudent" parameterType="java.util.List"> 
  11.     DELETE FROM STUDENT WHERE id IN 
  12.     <foreach collection="list" index="index" item="item" open="(" separator="," close=")">  
  13.         #{item}  
  14.     </foreach> 
  15. </delete> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值