springboot2.0+mybatis的批量更新mysql数据库

版权声明:不卑不亢,不骄不躁---好男人就是我,我就是曾哥. https://blog.csdn.net/weixin_42884584/article/details/82427079

我的场景是根据多个id更新某个字段:

相当于sql语句:

update tb set name="zhangsan" where id in (1,2,3...)

①mapper.java文件和mapper.xml中分别添加代码:

int batchUpdateByPrimaryKey(Map<String,Object> map);
<!--批量修改,只能能修改相同字段-->
<update id="batchUpdateByPrimaryKey" parameterType="Map">
  update systeminfo
  set SystemLevelID = #{systemLevel,jdbcType=VARCHAR}
  where SystemID in
  <foreach collection="systemIDList" item="params" open="(" separator="," close=")">
    #{params}
  </foreach>
</update>

service中代码:

@Override
public CommonResult batchChangeSystemLevel(String systemInfos) {
	JSONObject jsonObject = JSON.parseObject(systemInfos);
	String systemIDs = jsonObject.getString("systemIDs");
	String systemLevel = jsonObject.getString("systemLevel");

	Map<String, Object> map = new HashMap<>();
	map.put("systemLevel",systemLevel);
	List<String> systemIDList= Arrays.asList(systemIDs.split(","));
	map.put("systemIDList",systemIDList);

	systemInfoMapper.batchUpdateByPrimaryKey(map);

	return CommonResult.ok();
}

controller中代码

public CommonResult batchChangeSystemLevel(@RequestBody String systemInfos) {
	CommonResult result = systemInfoService.batchChangeSystemLevel(systemInfos);

	return result;
}

猜你喜欢

转载自blog.csdn.net/weixin_42884584/article/details/82427079