update a single record
| 1 |
UPDATE Course SET name = ' COURSE1 ' WHERE id = ' ID1 '; |
Update multiple records with the same field as one value
| 1 |
UPDATE Course SET name = ' course1 ' WHERE ID in (' id1 ', ' id2 ', ' id3); |
update multiple records to a different value for multiple fields
The more common writing is to execute the UPDATE statement in sequence by looping.
MyBatis is written as follows:
| 1 2 3 4 5 6 7 8 9 |
<update id= "UpdateBatch" parametertype= "java.util.List" > <foreach collection= "List" item= "Item" index= "IND Ex "open=" "close=" "separator="; > Update course <set> name=${item.name} </set> where id = ${ite M.id} </foreach> </update> |
A record update once, poor performance, easy to cause congestion.
MySQL does not provide a straightforward way to implement bulk updates, but you can use case-when syntax to implement this functionality.
| 1 2 3 4 5 6 7 8 9 10 11-12 |
UPDATE Course SET name = case ID if 1 THEN ' name1 ' when 2 THEN ' name2 ' when 3 THEN ' Name3 ' End, title = case ID when 1 THEN ' new Title 1 ' then 2 THEN ' new Title 2 ' When 3 THEN ' Ne W Title 3 ' End WHERE ID in (1,2,3) |
This SQL means that if the ID is 1, the value of name Name1,title is new Title1, and so on.
The configuration in MyBatis is as follows:
| 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 |
<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} &NBsp; </if> </foreach> </trim> <trim prefix= " roadgridid =case" suffix= "End," > <foreach collection= "List" item= "I" index= "index" > <if test= "I.roadgridid!=null" > &nbsP; 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}&NBSP;THEN&NBSP;#{I.UNITSID} </if> </foreach > </trim> </trim> where <foreach collection= "List" separator= "or" item= "I" index= "index" > id=#{i.id} </foreach> </update> |
Note:
MyBatis's foreach statementThe main use of foreach is in the build in condition, which can iterate over a collection in an SQL statement. The properties of a foreach element are mainly item,index,collection,open,separator,close. Item represents the alias for each element of the collection when it is iterated, and index specifies a name that represents where each iteration is in the iteration, open indicates what the statement starts with, and separator represents what symbol to use as a separator between iterations, Close indicates what ends with foreach, and the most critical and error-prone is the collection property, which must be specified, but in different cases the value of the property is not the same, mainly for 3 things: If a single parameter is passed in and the parameter type is a list, the value of the collection property is list if the argument type is an array array, the collection property value is array if the argument passed is multiple, We need to encapsulate them into a map, of course, single parameters can also be encapsulated into a map, in fact, if you are passing in the parameters, in the breast will be encapsulated into a map, the map key is the parameter name, So this time the collection attribute value is the incoming list or array object, below the key in its encapsulated map, to see sample code for each of these three cases: 1. Type of single parameter 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>
The value of the above collection is list, and the corresponding Mapper is this
Public list<blog> dynamicforeachtest (list<integer> IDs);
Test code:
@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. The type of the single parameter array 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>
The above collection is array, corresponding to the mapper code:
Public list<blog> dynamicforeach2test (int[] IDs);
The corresponding test code:
@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. Encapsulate the parameters into the type of 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>
The value of the above collection is IDs, which is the key of the incoming parameter map, corresponding to the mapper code:
Public list<blog> dynamicforeach3test (map<string, object> params);
Corresponding Test code:
@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", "China");