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批量更新报错badsql

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

  2. 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 ...

  3. Mybatis批量更新详解

    转:http://www.cnblogs.com/winkey4986/p/3915151.html Mybatis批量更新 批量操作就不进行赘述了.减少服务器与数据库之间的交互.网上有很多关于批量插 ...

  4. mybatis批量更新两种方式:1.修改值全部一样 2.修改每条记录值不一样

    Mybatis批量更新数据 mybatis批量更新两种方式:1.修改值全部一样 2.修改每条记录值不一样 mybatis批量更新两种方式:1.修改值全部一样 2.修改每条记录值不一样 mybatis批 ...

  5. mybatis批量更新策略

    我们知道循环中操作db会导致连接数满,严重影响数据库性能.所以在对db进行DQL与DML时,根据业务逻辑尽量批量操作,这里我们介绍下使用mybatis批量更新mysql的两种方式. 方式一: < ...

  6. Mybatis批量更新数据库与批量插入数据库(以oracle为例)

    一.批量更新 1.普通写法(一条记录update一次,性能比较差,容易造成阻塞.不建议使用) <update id="updateBatch" parameterType=& ...

  7. mybatis 批量更新 Parameter '__frch_item_0' not found. Available parameters are [list]

    一次在做批量更新数据的时候报错 Parameter '__frch_item_0' not found. Available parameters are [list] 记过反复查找,最后才发现是一个 ...

  8. MyBatis批量更新

    逐条更新 这种方式显然是最简单,也最不容易出错的,即便出错也只是影响到当条出错的数据,而且可以对每条数据都比较可控. 代码 updateBatch(List<MyData> datas){ ...

  9. mybatis批量更新报错 org.mybatis.spring.MyBatisSystemException

    具体报错信息: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.Bin ...

随机推荐

  1. 【笔记】让360浏览器用chrome 内核渲染你的网页

    学校的项目还处在测试阶段 有一个痛点就是有一些页面在360浏览器中默认以ie 内核渲染 这样很不好 以为部分页面因技术方面的不足导致并不能很好地兼容ie 浏览器,于是在网上找了一下答案 可真还有解决方 ...

  2. Open DJ备份与恢复方案

    最近接手了一个Cognos项目,第三方用户认证采用的是和Open DJ集成.本人之前很多采用的是cjap ,当然这和cjap相比起来简单的多了,最起码你不必具有Java的基础知识就可以完全驾驭了! 一 ...

  3. springboot中generator相关配置文件

    generator.properties # jdbc jdbc.driverClass = com.mysql.jdbc.Driver jdbc.url = jdbc:mysql://localho ...

  4. 创建sdcard.img时,提示permission dennid

    解决方法: mksdcard -l sdcard 100M E:\sdcard\sdcard.img 换一个盘试试,比如:mksdcard -l sdcard 100M F:\sdcard\sdcar ...

  5. Js中/g \s 什么意思

    Js中/g \s 什么意思 js里elm.value.replace(/[\s ]+/g, ''),是什么意思 比如/[\s]是什么意思 elm是表单吧.将elm表单的值中的空白字符替换 replac ...

  6. PotPlayer 进度条显示缩略图

      PotPlayer设置鼠标放在播放器进度条任意位置显示缩略图 迁移时间--2017年8月9日15:41:27Author:Marydon 右键-->选项(F5)-->点击左侧面板上的“ ...

  7. DBCP( 二) DataBase Connection Pool 的使用

    使用DBCP必须用的三个包: commons-dbcp-1.2.1.jar, commons-pool-1.2.jar, commons-collections-3.1.jar. 配置参数. Java ...

  8. activeMQ Jms Demo

    概述 ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经 ...

  9. Python-线程的生命周期

    线程的生命周期 所谓的xx生命周期,其实就是某对象的包含产生和销毁的一张状态图.线程的生命周期如下图所示: 各状态的说明如下: New新建.新创建的线程经过初始化后,进入Runnable状态. Run ...

  10. 【转载】安装mysql8.0.11以及修改root密码、连接navicat for mysql。

    1.1. 下载: 官网下载zip包,我下载的是64位的: 下载地址:https://dev.mysql.com/downloads/mysql/ 下载zip的包: 下载后解压:(解压在哪个盘都可以的) ...