手机
当前位置:查字典教程网 >编程开发 >Java >MyBatis批量添加、修改和删除
MyBatis批量添加、修改和删除
摘要:废话不多说了,直接步入正题了。1、批量添加元素session.insert(Stringstring,Objecto)publicvoidb...

废话不多说了,直接步入正题了。

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

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:

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)

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>

好了,本文到此结束,希望对大家有所帮助。

【MyBatis批量添加、修改和删除】相关文章:

java比较器Comparable接口与Comaprator接口的深入分析

java代码之谜运算符篇

JAVA TIMER简单用法学习

java向文件末尾添加内容示例分享

java 中的static关键字和final关键字的不同之处

java调用百度定位api服务获取地理位置示例

java 集合并发操作出现的异常ConcurrentModificationException

try catch finally的执行顺序深入分析

jdk中密钥和证书管理工具keytool常用命令详解

可视化Swing中JTable控件绑定SQL数据源的两种方法深入解析

精品推荐
分类导航