MyBatis根据条件批量修改字段

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

1)代码以及注释

conttoller

@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

@Service
public class WorkService {
	@Autowired
	private WorkMapper workMapper;
	
	public Integer examine(Integer[] id) {
		return workMapper.examine(id);
	}
}

mapper

@Mapper
public interface WorkMapper {
	Integer examine(@Param("id")Integer[] id);
}

xml

<!--根据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>

工具类HttpResponse(此需求中无关紧要的类)

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;
	}
}

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

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

生活不易,祝愿每个看到这句话的人天黑有灯,下雨有伞,你的付出不被辜负生活不易,祝愿每个看到这句话的人天黑有灯,下雨有伞,你的付出不被辜负

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值