近来批量添加,删除,更新用的比较多,单一的删除和更新,操作无法满足企业某些业务的需求,故通过以下示例分享知识:

今天通过更新的例子来说明

演示环境为jdk8,maven环境,ssm框架

请准备好环境,数据表可直接使用

一、准备数据表

CREATE TABLE `user` (
`user_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户ID',
`username` varchar(20) DEFAULT NULL COMMENT '用户名',
`sex` varbinary(20) DEFAULT NULL COMMENT '性别',
`phone` varchar(20) DEFAULT NULL COMMENT '电话',
`password` varchar(20) DEFAULT NULL COMMENT '密码',
`level` int(4) DEFAULT NULL COMMENT '等级',
`create_time` datetime DEFAULT NULL COMMENT '用户创建时间',
`email` varchar(50) DEFAULT NULL COMMENT '邮箱',
`logo` int(2) DEFAULT '0' COMMENT '登录标识',
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;

二、准备JavaBean

package cn.blog.entity;

import java.util.Date;
import java.util.List;
/**
* 用户实体
* @author youcong
*
*/
public class User {
/** 用户ID*/
private Integer userId; /** 用户名*/
private String username; /** 电话*/
private String phone; /** 密码*/
private String password; /** 等级*/
private Integer level; /** 用户创建时间*/
private String createTime; /** 性别*/
private String sex; /**
* 邮箱
*/
private String email; /**
* 登录标识
* @return
*/
private Integer logo; public Integer getLogo() {
return logo;
} public void setLogo(Integer logo) {
this.logo = logo;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
} public Integer getUserId() {
return userId;
} public void setUserId(Integer userId) {
this.userId = userId;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username == null ? null : username.trim();
} public String getPhone() {
return phone;
} public void setPhone(String phone) {
this.phone = phone == null ? null : phone.trim();
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password == null ? null : password.trim();
} public Integer getLevel() {
return level;
} public void setLevel(Integer level) {
this.level = level;
} public String getCreateTime() {
return createTime;
} public void setCreateTime(String createTime) {
this.createTime = createTime;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} }

三、编写对应的Mapper和xml

package cn.blog.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import cn.blog.entity.User;

/**
* 用户接口
* @author 挑战者
*
*/
public interface UserMapper { /**
* 批量修改
*/
public void udpateUserLogoStatu(@Param ("users") List<User> users); }
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.blog.mapper.UserMapper" > <resultMap id="BaseResultMap" type="User" >
<id column="user_id" property="userId" jdbcType="INTEGER" />
<result column="username" property="username" jdbcType="VARCHAR" />
<result column="phone" property="phone" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="level" property="level" jdbcType="INTEGER" />
<result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
<result column="email" property="email" jdbcType="VARCHAR" />
<result column="logo" property="logo" jdbcType="INTEGER"/> </resultMap> <update id="udpateUserLogoStatu" parameterType="java.util.List"> <foreach collection="users" item="user" index="index" separator=";">
update `user`
<set>
logo = 1
</set>
where logo = #{user.logo}
</foreach> </update> </mapper>

 四、junit单元测试

package cn.blog.test;

import java.util.ArrayList;
import java.util.List; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import cn.blog.entity.User;
import cn.blog.mapper.UserMapper; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:application-config.xml")
public class BlogTest { @Autowired
private UserMapper userMapper; @Test
public void testName() throws Exception { int logo[] = new int[] {0};
for (int i = 0; i < logo.length; i++) {
User user = new User();
user.setLogo(logo[i]);
List<User> users = new ArrayList<User>();
users.add(user);
userMapper.udpateUserLogoStatu(users);
} } }

mybatis的批量更新实例的更多相关文章

  1. mybatis执行批量更新update

    Mybatis的批量插入这里有http://ljhzzyx.blog.163.com/blog/static/38380312201353536375/.目前想批量更新,如果update的值是相同的话 ...

  2. mybatis 的批量更新操作sql

    转: mybatis 的批量更新操作sql 2018年07月23日 10:38:19 海力布 阅读数:1689   版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.cs ...

  3. 170829、mybatis使用oracle和mybatis中批量更新

    一.mybatis执行批量更新batch update 的方法(mysql数据库) 1.数据库连接必须配置:&allowMultiQueries=true(切记一定要加上这个属性,否则会有问题 ...

  4. 【mybatis】mybatis进行批量更新,报错:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right

    使用mybatis进行批量更新操作: 报错如下: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an erro ...

  5. mysql批量update更新,mybatis中批量更新操作

    在日常开发中,有时候会遇到批量更新操作,这时候最普通的写法就是循环遍历,然后一条一条地进行update操作.但是不管是在服务端进行遍历,还是在sql代码中进行遍历,都很耗费资源,而且性能比较差,容易造 ...

  6. mybatis中批量更新的问题

    问题:使用mybatis在执批量更新操作时,一直报错执行失败 解决方法: 首先打印了SQL语句,发现SQL语句拿出来执行没问题,也可以批量执行.SQL没问题,应该是配置的问题. 在网上查询和很多资料, ...

  7. Mybatis之批量更新操作

    更新单条记录 UPDATE course SET name = 'course1' WHERE id = 'id1'; 更新多条记录的同一个字段为同一个值 UPDATE course SET name ...

  8. mybatis 实现批量更新

    更新单条记录 1 UPDATE course SET name = 'course1' WHERE id = 'id1'; 更新多条记录的同一个字段为同一个值 1 UPDATE course SET  ...

  9. SpringBoot+Mybatis+Druid批量更新 multi-statement not allow异常

      本文链接:https://blog.csdn.net/weixin_43947588/article/details/90109325 注:该文是本博主记录学习之用,没有太多详细的讲解,敬请谅解! ...

随机推荐

  1. SuperMap 三维产品资料一览表

    转自:http://blog.csdn.net/supermapsupport/article/details/68924713 如何能快速地开发项目中的三维功能呢?本文为您提供全方位的三维资料,为您 ...

  2. python自动化开发-6-面向对象编程

    面向对象编程 面向对象的特性 封装:把客观事物封装成抽象的类,并且类可以把自己的数据和方法只让可信的类或者对象操作,对不可信的进行信息隐藏.  继承:面向对象编程 (OOP) 语言的一个主要功能就是“ ...

  3. 移植marvell poncat3 demo板的总结

    1.arm cpu自带的bootrom, 已经初始化好串口.SPI等外设,只要硬件设置好从SPI FLASH启动,则cpu自带的bootrom会自动打印出一些信息,显示出从SPI FLASH启动,驱动 ...

  4. Linux 学习笔记之超详细基础linux命令 Part 7

    Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 6----------------- ...

  5. 遇到npm报错read ECONNRESET怎么办

    遇到npm 像弱智一样报错怎么办 read ECONNRESET This is most likely not a problem with npm itselft 'proxy' config i ...

  6. ER模型试题

    M公司为了便于开展和管理各项业务活动,提高公司的知名度和影响力,拟构建一个基于网络的会议策划系统. [需求分析结果] 该系统的部分功能及初步需求分析的结果如下 : (1)M公司旗下有业务部.策划部和其 ...

  7. React Refs

    React Refs React 支持一种非常特殊的属性 Ref ,你可以用来绑定到 render() 输出的任何组件上. 这个特殊的属性允许你引用 render() 返回的相应的支撑实例( back ...

  8. 关于Natively Compiled Stored Procedures的优化

    Interpreted Transact-SQL stored procedures are compiled at first execution, in contrast to natively ...

  9. macOS 下NFS 文件系统挂载

    主要有两种方式: 使用:resvport选项, mount 挂载命令时. 使用:insecure选项, exportfs 文件配置时. sudo mount -o resvport IP:Addr b ...

  10. [SequenceFile_4] SequenceFile 配置压缩

    0. 说明 SequenceFile 配置压缩编解码器 && 压缩类型的选型 1. SequenceFile 配置压缩编解码器 package hadoop.compression; ...