Python 官方文档:入门教程 => 点击学习
目录mybatis根据条件批量修改字段代码以及注释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批量修改数据库的某字段需求完成!
想要修改一张表,是联合主键,也就是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语句。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: MyBatis根据条件批量修改字段的方式
本文链接: https://www.lsjlt.com/news/197177.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0