mybatis批量更新(对象集合)
Mybatis批量更新
批量操作就不进⾏赘述了。减少服务器与数据库之间的交互。⽹上有很多关于批量插⼊还有批量删除的帖⼦。但是批量更新却没有详细的解决⽅案。
实现⽬标
这⾥主要讲的是1张table中。根据不同的id值,来update不同的property。
数据表:1张。Tblsupertitleresult。错题结果统计。
表结构:
表中每⼀条数据必须通过两个字段来确定:userHhCode+titleId
需要批量更新的字段是:correctDate,result,checkState。
1批量更新的sql语句
我⽤的数据库是mysql。其他数据库的sql语句也都⼤同⼩异。
⽤mybatis的mapper-xml进⾏组装sql之前需要写出批量操作的sql语句。
Sql:
update tblsupertitleresult set result =case
when (userHhCode=2001 and titleId=1)then  90
when (userHhCode=2001 and titleId=2)then  70
end
,checkState = case
when (userHhCode=2001 and titleId=1)then  80
when (userHhCode=2001 andtitleId=2)then  120
end
where (userHhCode=2001 and titleId=1) or(userHhCode=2001 and titleId=2)
关于这个批量更新的sql语句做⼀个简单的解释。
要更新userHhCode=2001,titleId=1和userHhCode=2001 ,titleId=2的两条数据。
当userHhCode=2001,titleId=1时,将result,checkState设置为80
当userHhCode=2001,titleId=2时,将result设置为80,checkState设置为120.
这是mysql语句。运⾏没有问题。接下来就是mybatis的mapper-xml
Mybatis中mapper-xml
这⾥,⾸先介绍实体类。
public classWrongTitle {
//manipulatetable of tblsupertitleresult
privateString titleId;
privateString titleIdNew;
privateString result;
privateString checkState;
privateString isCollect;
privateString times;
privateString wrongDate;
privateString wrongNum;
privateString collectDate;
privateString userHhCode;
privateString correctDate;
privateString tid;// teacher who will review this wrong title
privateString paperTitleId;
getter和set⽅法省略。
好了现在开始介绍mybatis⾥⾯的⼏个标签。由于⼀些原因,mybatis的技术⽂档和⽤户指南所介绍得并不详细。
<foreach>标签:foreach元素的属性主要有item,index,collection,open,separator,close。item表⽰集合中每⼀个元素进⾏迭代时的别名,
index指定⼀个名字,⽤于表⽰在迭代过程中,每次迭代到的位置,
open表⽰该语句以什么开始,
separator表⽰在每次进⾏迭代之间以什么符号作为分隔符,
close表⽰以什么结束,
在使⽤foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况下,该属性的值是不⼀样的,主要有⼀下3种情况:
1.如果传⼊的是单参数且参数类型是⼀个List的时候,collection属性值为list;
2.如果传⼊的是单参数且参数类型是⼀个array数组的时候,collection的属性值为array;
3.如果传⼊的参数是多个的时候,我们就需要把它们封装成⼀个Map了,当然单参数也可以封装成map,实际上如果你在传⼊参数的时候,在MyBatis⾥⾯也是会把它封装成⼀个Map的,map的key就是参数名,所以这个时候collection属性值就是传⼊的List或array对象在⾃⼰封装的map⾥⾯的key;
关于以上三种collection的⽤法。百度上有很多帖⼦。这⾥不进⾏赘述。
<trim>标签:有四个属性:
Prefix:指的是<trim></trim>所包含的部分(body)以什么开头。
prefixOverrides:指<trim>中如果有内容时可忽略(body)前的匹配字符。
suffix:指的是<trim></trim>所包含的部分(body)以什么结尾。suffixOverrides:指<trim>中如果有内容时可忽略(body)后的匹配字符。
接下来直接上:
Mapper-xml
<update id="batchUpdate">
update tblsupertitleresult
<trim prefix="set" suffixOverrides=",">
<trim prefix="checkState =case" suffix="end,">
<foreach collection="list" item="i"  index="index">
<if test="i.checkState!=null">
when (userHhCode=#{i.userHhCode} and titleId=#{i.titleId}) then #{i.checkState} </if>
</foreach>
</trim>
<trim prefix=" correctDate =case"  suffix="end,">
<foreach collection="list" item="i"  index="index">
<if test="i.correctDate!=null">
when (userHhCode=#{i.userHhCode} and titleId=#{i.titleId}) then #{i.correctDate} </if>
</foreach>
</trim>
<trim prefix="result =case"  suffix="end,">
<foreach collection="list"item="i" index="index">
<if test="i.result!=null">
when (userHhCode=#{i.userHhCode} and titleId=#{i.titleId}) then #{i.result} </if>
</foreach>
</trim>
</trim>
where
<foreach collection="list" separator="or"item="i" index="index">批量更新sql语句
(userHhCode =#{i.userHhCode} andtitleId=#{i.titleId})
</foreach>
</update>
接下来就是dao:
public interface DatacenterDAO{
// batch update super title_result_view
public intbatchUpdate(List<WrongTitle> list );
Test类
public classTestBatch {
/
**
* @param args
*/
public static voidmain(String[] args) {
ApplicationContext  context = newClassPathXmlApplicationContext("l");      DatacenterDAO dao = Bean(DatacenterDAO.class);
ArrayList<WrongTitle> list = newArrayList<WrongTitle>();
WrongTitle t1=new WrongTitle();
WrongTitle t2=new WrongTitle();
WrongTitle t3=new WrongTitle();
WrongTitle t4=new WrongTitle();
t1.setTitleId(3+"");
t2.setTitleId(4+"");
t3.setTitleId(5+"");
t4.setTitleId(6+"");
t1.setUserHhCode(2001+"");
t2.setUserHhCode(2001+"");
t3.setUserHhCode(2001+"");
t4.setUserHhCode(2001+"");
t1.setCheckState(5+"");
t2.setCheckState(6+"");
t3.setCheckState(7+"");
t4.setCheckState(8+"");
t1.setResult(10+"");
t2.setResult(12+"");
t3.setResult(14+"");
t4.setResult(16+"");
list.add(t1);
list.add(t2);
list.add(t3);
list.add(t4);
int i=dao.batchUpdate(list);
System.out.println("操作了"+i+"⾏数据"); }
运⾏结果截图:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。