mybatis的批量更新例項
阿新 • • 發佈:2018-12-29
近來批量新增,刪除,更新用的比較多,單一的刪除和更新,操作無法滿足企業某些業務的需求,故通過以下示例分享知識:
今天通過更新的例子來說明
演示環境為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; /** 電話*/ privateString 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); } } }