分享

Mybatis批量更新

 jjls14 2015-01-19

Mybatis批量更新

批量操作就不进行赘述了。减少服务器与数据库之间的交互。网上有很多关于批量插入还有批量删除的帖子。但是批量更新却没有详细的解决方案。


实现目标

这里主要讲的是1张table中。根据不同的id值,来update不同的property。

数据表:1张。Tblsupertitleresult。错题结果统计。

表结构:

表中每一条数据必须通过两个字段来确定:userHhCode+titleId

需要批量更新的字段是:correctDate,result,checkState。


批量更新的sql语句

 我用的数据库是mysql。其他数据库的sql语句也都大同小异。

用mybatis的mapper-xml进行组装sql之前需要写出批量操作的sql语句。

Sql:

  1. update tblsupertitleresult set result =case   
  2.   
  3. when (userHhCode=2001 and titleId=1)then  90  
  4.    
  5. when (userHhCode=2001 and titleId=2)then  70  
  6.    
  7. end  
  8.    
  9. ,checkState = case    
  10.   
  11. when (userHhCode=2001 and titleId=1)then  80  
  12.    
  13. when (userHhCode=2001 andtitleId=2)then  120  
  14.    
  15. end  
  16.    
  17. 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设置为90,checkState设置为80

当userHhCode=2001,titleId=2时,将result设置为80,checkState设置为120.

这是mysql语句。运行没有问题。接下来就是mybatis的mapper-xml


Mybatis中mapper-xml

这里,首先介绍实体类。

  1. public classWrongTitle {  
  2.    
  3.     //manipulatetable of tblsupertitleresult   
  4.   
  5.     privateString titleId;  
  6.    
  7.     privateString titleIdNew;  
  8.    
  9.     privateString result;  
  10.    
  11.     privateString checkState;  
  12.    
  13.     privateString isCollect;  
  14.    
  15.     privateString times;  
  16.    
  17.     privateString wrongDate;  
  18.    
  19.     privateString wrongNum;  
  20.    
  21.     privateString collectDate;  
  22.    
  23.     privateString userHhCode;  
  24.    
  25.     privateString correctDate;  
  26.    
  27.     privateString tid;// teacher who will review this wrong title  
  28.    
  29.     privateString paperTitleId;  
  30.    
  31. 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

  1. <update id="batchUpdate">   
  2.             update tblsupertitleresult   
  3.             <trim prefix="set" suffixOverrides=",">  
  4.             <trim prefix="checkState =case" suffix="end,">  
  5.                 <foreach collection="list" item="i"  index="index">  
  6.                         <if test="i.checkState!=null">  
  7.                          when (userHhCode=#{i.userHhCode} and titleId=#{i.titleId}) then #{i.checkState}  
  8.                         </if>  
  9.                 </foreach>  
  10.              </trim>  
  11.              <trim prefix=" correctDate =case"  suffix="end,">  
  12.                 <foreach collection="list" item="i"  index="index">  
  13.                         <if test="i.correctDate!=null">  
  14.                          when (userHhCode=#{i.userHhCode} and titleId=#{i.titleId}) then #{i.correctDate}  
  15.                         </if>  
  16.                 </foreach>  
  17.              </trim>   
  18.              <trim prefix="result =case"  suffix="end,"  >  
  19.                 <foreach collection="list"item="i" index="index">   
  20.                         <if test="i.result!=null">  
  21.                          when (userHhCode=#{i.userHhCode} and titleId=#{i.titleId}) then #{i.result}  
  22.                         </if>  
  23.                 </foreach>  
  24.              </trim>   
  25.              </trim>   
  26.             where   
  27.             <foreach collection="list" separator="or"item="i" index="index" >  
  28.              (userHhCode =#{i.userHhCode} andtitleId=#{i.titleId})  
  29.          </foreach>  
  30. </update>  

接下来就是dao:
  1. public interface DatacenterDAO{  
  2.    
  3. // batch update super title_result_view  
  4.    
  5.        public intbatchUpdate(List<WrongTitle> list );  
  6.    
  7. Test类  
  8.    
  9. public classTestBatch {  
  10.     /** 
  11.   
  12.     * @param args 
  13.   
  14.     */  
  15.    
  16.     public static voidmain(String[] args) {  
  17.    
  18.        ApplicationContext  context = newClassPathXmlApplicationContext("applicationContext.xml");  
  19.    
  20.        DatacenterDAO dao = context.getBean(DatacenterDAO.class);  
  21.    
  22.        ArrayList<WrongTitle> list = newArrayList<WrongTitle>();  
  23.    
  24.        WrongTitle t1=new WrongTitle();  
  25.    
  26.        WrongTitle t2=new WrongTitle();  
  27.    
  28.        WrongTitle t3=new WrongTitle();  
  29.    
  30.        WrongTitle t4=new WrongTitle();  
  31.    
  32.        t1.setTitleId(3+"");  
  33.    
  34.        t2.setTitleId(4+"");  
  35.    
  36.        t3.setTitleId(5+"");  
  37.    
  38.        t4.setTitleId(6+"");  
  39.    
  40.        t1.setUserHhCode(2001+"");  
  41.    
  42.        t2.setUserHhCode(2001+"");  
  43.    
  44.        t3.setUserHhCode(2001+"");  
  45.    
  46.        t4.setUserHhCode(2001+"");  
  47.    
  48.        t1.setCheckState(5+"");  
  49.    
  50.        t2.setCheckState(6+"");  
  51.    
  52.        t3.setCheckState(7+"");  
  53.    
  54.        t4.setCheckState(8+"");  
  55.    
  56.        t1.setResult(10+"");  
  57.    
  58.        t2.setResult(12+"");  
  59.    
  60.        t3.setResult(14+"");  
  61.    
  62.        t4.setResult(16+"");  
  63.    
  64.        list.add(t1);  
  65.    
  66.        list.add(t2);  
  67.    
  68.        list.add(t3);  
  69.    
  70.        list.add(t4);  
  71.    
  72.        int i=dao.batchUpdate(list);  
  73.    
  74.        System.out.println("操作了"+i+"行数据");  
  75.    
  76. }  


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多