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)
實(shí)例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>
實(shí)例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>
好了,本文到此結(jié)束,希望對大家有所幫助。
相關(guān)文章
Spring Mvc中傳遞參數(shù)方法之url/requestMapping詳解
在開發(fā)中,參數(shù)傳遞是必不可少的一個(gè)功能,下面這篇文章主要給大家介紹了關(guān)于Spring Mvc中傳遞參數(shù)方法之url/requestMapping的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-07-07基于SpringBoot+Redis實(shí)現(xiàn)分布式鎖
本文主要介紹了基于SpringBoot+Redis實(shí)現(xiàn)分布式鎖,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05基于OpenCV與JVM實(shí)現(xiàn)矩陣處理圖像
本文主要介紹了Java圖像處理實(shí)戰(zhàn)之基于OpenCV與JVM實(shí)現(xiàn)矩陣處理圖像。文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)圖像處理有一定的幫助,感興趣的可以試一試2022-01-01Spring注解驅(qū)動之BeanFactoryPostProcessor原理解析
這篇文章主要介紹了Spring注解驅(qū)動之BeanFactoryPostProcessor原理,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09NoHttpResponseException問題分析解決記錄
這篇文章主要為大家介紹了NoHttpResponseException問題分析解決記錄,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08Mybatis聯(lián)合查詢的實(shí)現(xiàn)方法
本文主要介紹了 Mybatis聯(lián)合查詢的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01