程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> MyBatis批量添加、修正和刪除

MyBatis批量添加、修正和刪除

編輯:關於JAVA

MyBatis批量添加、修正和刪除。本站提示廣大學習愛好者:(MyBatis批量添加、修正和刪除)文章只能為提供參考,不一定能成為您想要的結果。以下是MyBatis批量添加、修正和刪除正文


空話不多說了,直接步入正題了。

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>

好了,本文到此停止,願望對年夜家有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved