myBatis批量添加,修改和删除

来源:互联网 发布:python余弦相似度 编辑:程序博客网 时间:2024/05/16 02:05

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,实际上如果你在传入参数的时候,在breast里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key.

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

[plain] view plaincopy
  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.   
  18. <insert id="batchInsertStudent" parameterType="java.util.List">  
  19.     INSERT INTO STUDENT (id,name,sex,tel,address)  
  20.     VALUES   
  21.     <foreach collection="list" item="item" index="index" separator="," >  
  22.         (#{item.id},#{item.name},#{item.sex},#{item.tel},#{item.address})  
  23.     </foreach>  
  24. </insert>  

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

3、批量删除session.delete(String string,Object o)
[plain] view plaincopy
  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. }  
  11. <delete id="batchDeleteStudent" parameterType="java.util.List">  
  12.     DELETE FROM STUDENT WHERE id IN  
  13.     <foreach collection="list" index="index" item="item" open="(" separator="," close=")">   
  14.         #{item}   
  15.     </foreach>  
  16. </delete>  

0 0
原创粉丝点击