加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

mybatis的批量更新实例

发布时间:2020-12-14 18:05:32 所属栏目:大数据 来源:网络整理
导读:近来批量添加,删除,更新用的比较多,单一的删除和更新,操作无法满足企业某些业务的需求,故通过以下示例分享知识: 今天通过更新的例子来说明 演示环境为jdk8,maven环境,ssm框架 请准备好环境,数据表可直接使用 一、准备数据表 CREATE TABLE `user` ( `us

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

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

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

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

一、准备数据表

CREATE TABLE `user` (
  `user_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户ID',`username` varchar(20) DEFAULT NULL COMMENT '用户名'20) DEFAULT NULL COMMENT '电话'int(4) DEFAULT NULL COMMENT '等级'50) DEFAULT NULL COMMENT '邮箱'int(2) DEFAULT '0' COMMENT '登录标识'
package cn.blog.entity;

import java.util.Date;
 java.util.List;
/**
 * 用户实体
 * @author youcong
 *
 */
public class User {
     用户ID*/
    private Integer userId;

     用户名 String username;

     电话 String phone;

     密码 String password;

     等级 Integer level;

     用户创建时间 String createTime;

     性别 String sex;
    
    
     * 邮箱
      String email;
    
    

     * 登录标识
     * @return
      Integer logo;
    
    
    
    public Integer getLogo() {
        return logo;
    }

    void setLogo(Integer logo) {
        this.logo = logo;
    }

 String getEmail() {
         email;
    }

     setEmail(String email) {
        this.email = Integer getUserId() {
         userId;
    }

     setUserId(Integer userId) {
        this.userId = String getUsername() {
         username;
    }

     setUsername(String username) {
        this.username = username == null ? null : username.trim();
    }

     String getPhone() {
         phone;
    }

     setPhone(String phone) {
        this.phone = phone ==  : phone.trim();
    }

     String getPassword() {
         password;
    }

     setPassword(String password) {
        this.password = password ==  : password.trim();
    }

     Integer getLevel() {
         level;
    }

     setLevel(Integer level) {
        this.level = level;
    }


     String getCreateTime() {
         createTime;
    }

     setCreateTime(String createTime) {
        this.createTime = String getSex() {
         sex;
    }

     setSex(String sex) {
        this.sex = sex;
    }
    

}

三、编写对应的Mapper和xml

 cn.blog.mapper;

 java.util.List;

 org.apache.ibatis.annotations.Param;

 cn.blog.entity.User;


 * 用户接口
 *  挑战者
 *
 interface UserMapper {
   

    
    
     * 批量修改
     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单元测试

 cn.blog.test;

 java.util.ArrayList;
 org.junit.Test;
 org.junit.runner.RunWith;
 org.springframework.beans.factory.annotation.Autowired;
 org.springframework.test.context.ContextConfiguration;
 org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

 cn.blog.entity.User;
 cn.blog.mapper.UserMapper;

@RunWith(SpringJUnit4ClassRunner.)
@ContextConfiguration("classpath:application-config.xml")
 BlogTest {

    
    @Autowired
     UserMapper userMapper;
    
    @Test
    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);
        }
        
    
    }


}

?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读