一、在修改行数据 只修改一个参数 并且修改的结果都一样时
1、sql语句为:
#也就是说 参数是多个 但是条件确是多个
update t_learn_mysql
set `password` = '123456'
where id BETWEEN 2 and 5 ;
2、在mybatis-plus中:
//可以通过 **mapper.update()修改
//new 一个新的对象 把需要修改的参数传递进去 别的参数为空
User user = new User();
user.setPassword("123456");
// 在修改的时候 只需要 保证条件正确即可
usermapper.update(user,new UpdateWrapper<User>().between("id",2,5));
二、当需要修改的 有多个结果 多个条件的时候
可以通过 case when then 的组合来完成数的批量修改
注:在项目中 sql语句可以长一些 但是应该避免重复的连接数据库
1、sql语句为:
update t_learn_mysql
set
account =
case id
when 1 then 'xiuer'
when 2 then 'aimafan'
end
where
id in(1,2)
;
2、mybatis 或者mybatis-plus连接, 只修改一个参数 ,并且条件不同 值也不同 的时候
@Update({"<script>"+
" update t_learn_mysql " +
"<trim prefix ='set' prefixOverrides=',' > " +
"<trim prefix ='account = case' suffix='end'>" +
"<foreach collection ='list' item ='user' index = 'index'> " +
"when id = #{user.id} then #{user.account} " +
"</foreach>" +
"</trim> " +
"</trim> " +
"where id in " +
"<foreach collection ='list' item ='user' index ='index' separator=',' open='(' close=')' > " +
"#{user.id} " +
"</foreach>" +
" </script>" })
int updateBatn(@Param("list")List<User> list);
3、mybatis 或者mybatis-plus连接 ,修改多个参数 条件不同,值也不同的时候
@Update({"<script>"+
" update t_learn_mysql " +
"<trim prefix ='set' prefixOverrides=',' > " +
"<trim prefix ='account = (case' suffix='end),'>" +
"<foreach collection ='list' item ='user' index = 'index'> " +
"when id = #{user.id} then #{user.account} " +
"</foreach>" +
"</trim> " +
"<trim prefix ='password= (case' suffix='end)'>" +
"<foreach collection ='list' item ='user' index = 'index'> " +
"when id = #{user.id} then #{user.password} " +
"</foreach>" +
"</trim> " +
"</trim> " +
"where id in " +
"<foreach collection ='list' item ='user' index ='index' separator=',' open='(' close=')' > " +
"#{user.id} " +
"</foreach>" +
" </script>" })
int updateBatn(@Param("list")List<User> list);