批量更新:java
在實際項目開發過程當中,常有這樣的需求:根據ids更新表的某一個字段值,這時的sql(批量刪除同理)語句是:mysql
public interface IStaffDao { void batchUpdate(@Param("list") List<Long> list); }
<select id="getStaffsByIds" resultMap="staff_Mapper"> update staff set status = 0 where id in <foreach collection="list" item="item" index="index" open="(" separator="," close=")" > #{item} </foreach> ORDER BY id </select>
還有一種狀況:根據ids更新表的多個值,而且每一個id對應的值也不同,這時上述語句已經知足不了需求,須要另外一種批量更新sql語句spring
public interface IStaffDao { void batchUpdate(@Param("list") List<Staff> list); }
<update id="batchUpdate" parameterType="java.util.List" > <foreach collection="list" item="item" index="index" separator=";"> UPDATE staff set count = #{item.count} , code = #{item.code} , invalid_time = #{item.time} WHERE id = #{item.id} </foreach> </update>
因爲這種批量更新是一次執行多個update語句,因此mybatis須要額外的配置:sql
在spring.datasource.url後加上allowMultiQueries=true
如:jdbc:mysql://10.10.20.36:3306/test?allowMultiQueries=true數組
不然,會報錯session
批量新增:mybatis
<insert id="batchAdd" parameterType="java.util.List"> insert into sys_config_content <trim prefix="(" suffix=")" suffixOverrides=","> auto_id, config_id, parent_id, config_name, </trim> values <foreach collection="list" item="item" separator=","> <trim prefix="(" suffix=")" suffixOverrides=","> #{item.autoId}, #{item.configId}, #{parentId}, #{item.configName}, </trim> </foreach> </insert>
foreach的主要用在構建in條件中,它能夠在SQL語句中進行迭代一個集合。foreach元素的屬性主要有 item,index,collection,open,separator,close。item表示集合中每個元素進行迭代時的別名,index指 定一個名字,用於表示在迭代過程當中,每次迭代到的位置,open表示該語句以什麼開始,separator表示在每次進行迭代之間以什麼符號做爲分隔 符,close表示以什麼結束,在使用foreach的時候最關鍵的也是最容易出錯的就是collection屬性,該屬性是必須指定的,可是在不一樣狀況 下,該屬性的值是不同的,主要有一下3種狀況:app
<select id="dynamicForeachTest" resultType="Blog"> select * from t_blog where id in <foreach collection="list" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>
上述collection的值爲list,對應的Mapper是這樣的:ide
public List<Blog> dynamicForeachTest(List<Integer> ids);
測試代碼: 測試
@Test public void dynamicForeachTest() { SqlSession session = Util.getSqlSessionFactory().openSession(); BlogMapper blogMapper = session.getMapper(BlogMapper.class); List<Integer> ids = new ArrayList<Integer>(); ids.add(1); ids.add(3); ids.add(6); List<Blog> blogs = blogMapper.dynamicForeachTest(ids); for (Blog blog : blogs) System.out.println(blog); session.close(); }
2.單參數array數組的類型:
<select id="dynamicForeach2Test" resultType="Blog"> select * from t_blog where id in <foreach collection="array" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>
上述collection爲array,對應的Mapper代碼:
public List<Blog> dynamicForeach2Test(int[] ids);
對應的測試代碼:
@Test public void dynamicForeach2Test() { SqlSession session = Util.getSqlSessionFactory().openSession(); BlogMapper blogMapper = session.getMapper(BlogMapper.class); int[] ids = new int[] {1,3,6,9}; List<Blog> blogs = blogMapper.dynamicForeach2Test(ids); for (Blog blog : blogs) System.out.println(blog); session.close(); }
3.本身把參數封裝成Map的類型
<select id="dynamicForeach3Test" resultType="Blog"> select * from t_blog where title like "%"#{title}"%" and id in <foreach collection="ids" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>
上述collection的值爲ids,是傳入的參數Map的key,對應的Mapper代碼:
public List<Blog> dynamicForeach3Test(Map<String, Object> params);
對應測試代碼:
@Test public void dynamicForeach3Test() { SqlSession session = Util.getSqlSessionFactory().openSession(); BlogMapper blogMapper = session.getMapper(BlogMapper.class); final List<Integer> ids = new ArrayList<Integer>(); ids.add(1); ids.add(2); ids.add(3); ids.add(6); ids.add(7); ids.add(9); Map<String, Object> params = new HashMap<String, Object>(); params.put("ids", ids); params.put("title", "中國"); List<Blog> blogs = blogMapper.dynamicForeach3Test(params); for (Blog blog : blogs) System.out.println(blog); session.close(); }