MyBatis批量增删改

在项目中用到了MyBatis,以前没仔细看过MyBatis框架,  下面给大家一段代码(批量增删改的操作)  ------>  (以下有抄袭大牛的代码 忘请见谅)

  

1.批量增加数据的操作

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>

1.1批量修改的操作

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();  
}
<-- 注意 collection="idList" 是对应上面代码Map插入值时的Key值为   idList(此名字随意起)    -->
<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> 

1.1.2(批量修改操作之二)

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>

1.2 (批量删除)-->ps:一般在数据库中不真正删除数据(只是修改表的状态)

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> 


 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值