更新單條記錄
1
|
UPDATE course SET name = 'course1' WHERE id = 'id1';
|
更新多條記錄的同一個字段爲同一個值java
1
|
UPDATE course SET name = 'course1' WHERE id in ('id1', 'id2', 'id3);
|
更新多條記錄爲多個字段爲不一樣的值
比較普通的寫法,是經過循環,依次執行update語句。sql
Mybatis寫法以下:數組
1
2
3
4
5
6
7
8
9
|
<update id="updateBatch" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="" close="" separator=";">
update course
<set>
name=${item.name}
</set>
where id = ${item.id}
</foreach>
</update>
|
一條記錄update一次,性能比較差,容易形成阻塞。session
MySQL沒有提供直接的方法來實現批量更新,但可使用case when語法來實現這個功能。app
1
2
3
4
5
6
7
8
9
10
11
12
|
UPDATE course
SET name = CASE id
WHEN 1 THEN 'name1'
WHEN 2 THEN 'name2'
WHEN 3 THEN 'name3'
END,
title = CASE id
WHEN 1 THEN 'New Title 1'
WHEN 2 THEN 'New Title 2'
WHEN 3 THEN 'New Title 3'
END
WHERE id IN (1,2,3)
|
這條sql的意思是,若是id爲1,則name的值爲name1,title的值爲New Title1;依此類推。ide
在Mybatis中的配置則以下:post
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
<update id="updateBatch" parameterType="list">
update course
<trim prefix="set" suffixOverrides=",">
<trim prefix="peopleId =case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.peopleId!=null">
when id=#{i.id} then #{i.peopleId}
</if>
</foreach>
</trim>
<trim prefix=" roadgridid =case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.roadgridid!=null">
when id=#{i.id} then #{i.roadgridid}
</if>
</foreach>
</trim>
<trim prefix="type =case" suffix="end," >
<foreach collection="list" item="i" index="index">
<if test="i.type!=null">
when id=#{i.id} then #{i.type}
</if>
</foreach>
</trim>
<trim prefix="unitsid =case" suffix="end," >
<foreach collection="list" item="i" index="index">
<if test="i.unitsid!=null">
when id=#{i.id} then #{i.unitsid}
</if>
</foreach>
</trim>
</trim>
where
<foreach collection="list" separator="or" item="i" index="index" >
id=#{i.id}
</foreach>
</update>
|
注:性能
MyBatis的foreach語句詳解
foreach的主要用在構建in條件中,它能夠在SQL語句中進行迭代一個集合。foreach元素的屬性主要有 item,index,collection,open,separator,close。item表示集合中每個元素進行迭代時的別名,index指 定一個名字,用於表示在迭代過程當中,每次迭代到的位置,open表示該語句以什麼開始,separator表示在每次進行迭代之間以什麼符號做爲分隔 符,close表示以什麼結束,在使用foreach的時候最關鍵的也是最容易出錯的就是collection屬性,該屬性是必須指定的,可是在不一樣狀況 下,該屬性的值是不同的,主要有一下3種狀況:
- 若是傳入的是單參數且參數類型是一個List的時候,collection屬性值爲list
- 若是傳入的是單參數且參數類型是一個array數組的時候,collection的屬性值爲array
- 若是傳入的參數是多個的時候,咱們就須要把它們封裝成一個Map了,固然單參數也能夠封裝成map,實際上若是你在傳入參數的時候,在breast裏面也是會把它封裝成一個Map的,map的key就是參數名,因此這個時候collection屬性值就是傳入的List或array對象在本身封裝的map裏面的key
下面分別來看看上述三種狀況的示例代碼:
1.單參數List的類型: <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是這樣的 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(); }