MyBatis怎么根据条件批量修改字段

发布时间:2023-02-22 16:28:36 作者:iii
来源:亿速云 阅读:107

本篇内容介绍了“MyBatis怎么根据条件批量修改字段”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

MyBatis根据条件批量修改字段

背景:

给学生改作业,只要是对的都批量进行数据库的修改

代码以及注释

@RestController
@RequestMapping("/work")
public class WorkController {
    @Autowired
    private WorkService workService;
    
    @PutMapping("/examine")
    public HttpResponse examine(Integer[] id) {
        Integer total = workService.examine(id);
        return HttpResponse.ok("共批改[ "+total+" ]条道题","");
    }
}
@Service
public class WorkService {
    @Autowired
    private WorkMapper workMapper;
    
    public Integer examine(Integer[] id) {
        return workMapper.examine(id);
    }
}
@Mapper
public interface WorkMapper {
    Integer examine(@Param("id")Integer[] id);
}
<!--根据id来批量修改WORK表里面的isEnable字段-->
<update id="examine">
    UPDATE WORK SET isEnable=TRUE WHERE id IN
    <foreach collection="id" item="id" separator="," open="(" close=")">
        #{id}
    </foreach>
</update>
public class HttpResponse {
    private Integer status;
    private String message;
    private Object object;
    
    public static HttpResponse ok(String message) {
        return new HttpResponse(200, message, null);
    }
    
    public static HttpResponse ok(Object object) {
        return new HttpResponse(200, null, object);
    }
    
    public static HttpResponse ok(String message,Object object) {
        return new HttpResponse(200, message, object);
    }
    
    public static HttpResponse error(Integer status, String message) {
        return new HttpResponse(500, message, null);
    }
    
    public static HttpResponse error(String message) {
        return new HttpResponse(500, message, null);
    }
    
    public static HttpResponse error(String message,Object object) {
        return new HttpResponse(500, message, object);
    }
    
    
    protected HttpResponse() {
        super();
    }
    
    private HttpResponse(Integer status, String message, Object object) {
        super();
        this.status = status;
        this.message = message;
        this.object = object;
    }
    public Integer getStatus() {
        return status;
    }
    public void setStatus(Integer status) {
        this.status = status;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public Object getObject() {
        return object;
    }
    public void setObject(Object object) {
        this.object = object;
    }
}

重点:

如果mapper没加@Param("id")注解会报错找不到参数"id"

至此MyBatis根据id批量修改数据库的某字段需求完成!

MyBatis多条件批量修改

简单记录下

想要修改一张表,是联合主键,也就是where后两个以上条件才能唯一确定一条数据。

如果有唯一键,那么foreach中 用in就可以解决。

现在没办法用in,不然 A in (#{})and B in (#{})感觉成笛卡尔了。

简单点就是要实现执行多条语句。对update做循环。传入参数为 List,ATest类中字段A,B为联合主键,修改C的值

<update id="update" parameterType="java.util.List">
        <foreach collection="list"  item="val" separator=";" open="begin" close=";end;">
            update table_ABC
            <set>
                <if test="val.C!= null">C = #{val.C},</if>
            </set>
            where A=#{val.A} and B= #{val.B}
        </foreach>

    </update>

执行出来效果为:

begin 
update table_ABC set C = '2' where A = '1' and B= '1';
update table_ABC set C = '2' where A = '1' and B= '2';
update table_ABC set C = '4' where A = '3' and B= '2';
end;

这里使用的数据库为oracle,oracle执行认为begin和end之前为一条语句

当然,也可以用or 的形式拼接,不过还没测试。

网上查有的说是mysql数据库 mybatis一次执行一条语句。支持多条,可以MySQL连接数据库时,添加语句:“allowMultiQueries=true”

作用:

1.可以在sql语句后携带分号,实现多语句执行。

2.可以执行批处理,同时发出多个SQL语句。

“MyBatis怎么根据条件批量修改字段”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

推荐阅读:
  1. 怎么在IDEA中使用SpringBoot+MyBatis+MySql实现一个动态登录与注册功能
  2. <include>标签在mybatis中的作用是什么

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mybatis

上一篇:python如何实现断点调试

下一篇:Java怎么实现手写线程池并测试

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》