MyBatis 批量修改(update)数据表某字段

  • studentMapper.xml 文件内容
<update id="updateName">
       update student set grade = #{newGrade}, course = #{newCourse}
       where stuID in (
       <foreach collection="idList" item="item" index="index" open="" close="" separator=",">
           #{item}
       </foreach>
       )
   </update>
  • studentMapper 接口 interface(继承了baseMapper)文件内容,idList 需是 list 类型,不能是逗号分割的 String
	int updateInfo(@Param("idList") List idList, @Param("newGrade") String newGrade, @Param("newCourse") String newCourse);
  • studentService 接口 interface 文件内容
	boolean updateInfo(List idList, String newGrade, String newCourse);
  • studentServiceImpl java文件内容
@Override
	public boolean updateInfo(List idList, String newGrade, String newCourse) {
		int result = baseMapper.updateInfo(idList, newGrade, newCourse);
		return result > 0;
	}
  • studentcontroller java 文件内容
	@PostMapping("/updateInfo")
	@ApiOperationSupport
	@Transactional(rollbackFor = Exception.class)
	@ApiOperation(value = "批量更改学生信息", notes = "传入需要修改的学生id,grade,course")
	public R<Object> updateInfo(@RequestBody Map<String,Object> map) {
		if (!ObjectUtils.isEmpty(map.get("idList"))) {
			// 更新的执行结果(true:成功,false:失败)
			boolean result = false;
			// 将接收到的学生ids 转为 list
			List idList= Arrays.asList(String.valueOf(map.get("idList")).split(","));
			result = studentService.updateInfo(idList, String.valueOf(map.get("newGrade")), String.valueOf(map.get("newCourse")));
			if (!result) {
				throw new ServiceException("更新失败!");
			}
		}
		return R.status(true);
	}
  • 前端传参 js文件
export const updateInfo= (param) => {
  return request({
    url: '/api/collage/student/updateInfo',
    method: 'post',
    data: param
  })
}
  • 前端 vue 文件传参的定义
var param = {
		idList:this.stuIds,
        newGrade:this.grade,
        newCourse:this.course,
      }
上一篇:浅谈 synchronized


下一篇:VUE 实现拖拽排序(前后端)