码迷,mamicode.com
首页 > 其他好文 > 详细

myBatis批量添加,修改和删除

时间:2014-05-26 22:43:20      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:style   c   class   blog   code   java   

摘自: http://blog.csdn.net/myjlvzlp/article/details/8434376

 

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

bubuko.com,布布扣
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>
bubuko.com,布布扣

 

2、批量修改session. insert (String string,Object o)

bubuko.com,布布扣
实例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>
bubuko.com,布布扣

3、批量删除session.delete(String string,Object o)

bubuko.com,布布扣
public void batchDeleteStudent(){
    List<Integer> ls = new ArrayList<Integer>();
    for(int i = 4;i < 8;i++){
        ls.add(i);
    }
    SqlSession session = SessionFactoryUtil.getSqlSessionFactory().openSession();
    session.delete("mybatisdemo.domain.Student.batchDeleteStudent",ls);
    session.commit();
    session.close();
}
<delete id="batchDeleteStudent" parameterType="java.util.List">
    DELETE FROM STUDENT WHERE id IN
    <foreach collection="list" index="index" item="item" open="(" separator="," close=")"> 
        #{item} 
    </foreach>
</delete>
bubuko.com,布布扣

 

 

 

 

myBatis批量添加,修改和删除,布布扣,bubuko.com

myBatis批量添加,修改和删除

标签:style   c   class   blog   code   java   

原文地址:http://www.cnblogs.com/wuyifu/p/3745258.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!