转:http://www.cnblogs.com/winkey4986/p/3915151.html

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设置为90,checkState设置为80

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

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

Mybatis中mapper-xml

这里,首先介绍实体类。

public classWrongTitle {

    //manipulatetable of tblsupertitleresult

    private String titleId;

    private String titleIdNew;

    private String result;

    private String checkState;

    private String isCollect;

    private String times;

    private String wrongDate;

    private String wrongNum;

    private String collectDate;

    private String userHhCode;

    private String correctDate;

    private String tid;// teacher who will review this wrong title

    private String 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">

             (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("applicationContext.xml");

DatacenterDAO dao = context.getBean(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+"行数据");

}

运行结果截图:

希望能帮助到大家~。~

================

亲测可用,但是不知道效率到底如何。

Mybatis批量更新详解的更多相关文章

  1. MyBatis Mapper XML 详解

    MyBatis Mapper XML 详解 MyBatis 真正的力量是在映射语句中.这里是奇迹发生的地方.对于所有的力量,SQL 映射的 XML 文件是相当的简单.当然如果你将它们和对等功能的 JD ...

  2. 深入浅出mybatis之启动详解

    深入浅出mybatis之启动详解 MyBatis功能丰富,但使用起来非常简单明了,今天我们来追踪一下它的启动过程. 目录 如何启动MyBatis 如何使用MyBatis MyBatis启动过程 如何启 ...

  3. Mybatis案例超详解(上)

    Mybatis案例超详解(上) 前言: 本来是想像之前一样继续跟新Mybatis,但由于种种原因,迟迟没有更新,快开学了,学了一个暑假,博客也更新了不少,我觉得我得缓缓,先整合一些案例练练,等我再成熟 ...

  4. mybatis代码生成器配置文件详解

    mybatis代码生成器配置文件详解 更多详见 http://generator.sturgeon.mopaas.com/index.html http://generator.sturgeon.mo ...

  5. mybatis批量更新报错badsql

    mybatis批量更新时语法写的都对,但是报错,需要在连接上面加上allowMultiQueries=true 示例:jdbc:MySQL://192.168.1.236:3306/test?useU ...

  6. mybatis批量更新update-设置多个字段值 报错 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near

    mybatis批量更新update-设置多个字段值 2016年08月01日 12:49:26 姚一号 阅读数:29539 标签: mysql mybatis批量更新批量更新allowMultiQuer ...

  7. MyBatis核心配置文件详解

    ------------------------siwuxie095                                     MyBatis 核心配置文件详解         1.核心 ...

  8. Mybatis批量更新<转>

    Mybatis批量更新 批量操作就不进行赘述了.减少服务器与数据库之间的交互.网上有很多关于批量插入还有批量删除的帖子.但是批量更新却没有详细的解决方案. 实现目标 这里主要讲的是1张table中.根 ...

  9. 《深入理解mybatis原理2》 Mybatis初始化机制详解

    <深入理解mybatis原理> Mybatis初始化机制详解 对于任何框架而言,在使用前都要进行一系列的初始化,MyBatis也不例外.本章将通过以下几点详细介绍MyBatis的初始化过程 ...

随机推荐

  1. 213. String Compression【LintCode java】

    Description Implement a method to perform basic string compression using the counts of repeated char ...

  2. Matlab 图象操作函数讲解

    h = imrect;pos = getPosition(h); 这个函数用来获取图象上特定区域的坐标,其中pos的返回值中有四个参数[xmin,ymin,width,height],特定区域的左上角 ...

  3. 【Machine Learning】如何处理机器学习中的非均衡数据集?

    在机器学习中,我们常常会遇到不均衡的数据集.比如癌症数据集中,癌症样本的数量可能远少于非癌症样本的数量:在银行的信用数据集中,按期还款的客户数量可能远大于违约客户的样本数量.   比如非常有名的德国信 ...

  4. ffmpeg实现mjpeg摄像头的采集-预览-拍照

    摄像头输出是mjpeg格式的,需要实现在线预览功能,然后实现拍照功能 1.可以设置采集图像的分辨率,预览分辨率为640*480,可以自定义 2.ctrl+\ 拍照,ctrl+c 退出 void tes ...

  5. 六: Image Viewer 离线镜像查看器

    参考:http://hadoop.apache.org/docs/r2.6.3/hadoop-project-dist/hadoop-hdfs/HdfsImageViewer.html   离线镜像查 ...

  6. SGU 438 The Glorious Karlutka River =)(最大流)

    Description A group of Mtourists are walking along the Karlutka river. They want to cross the river, ...

  7. 自测之Lesson7:设备文件操作

    题目:请编写一个输入密码(不回显)的程序,要求通过设置终端来完成. 完成代码: #include <stdio.h> #include <unistd.h> #include ...

  8. Linux上安装MySQL - 12条命令搞定MySql

    从零开始安装mysql数据库 : 按照该顺序执行 :  a. 查看是否安装有mysql:yum list installed mysql*, 如果有先卸载掉, 然后在进行安装; b. 安装mysql客 ...

  9. Alpha冲刺——第三天

    Alpha第三天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...

  10. java — 线程池

    线程池的作用       线程池作用就是限制系统中执行线程的数量.     根据系统的环境情况,可以自动或手动设置线程数量,达到运行的最佳效果:少了浪费了系统资源,多了造成系统拥挤效率不高.用线程池控 ...