-
在mapper层定义查询方法
注@Param参数是绑定xml里面的collection参数
ListqueryByIds(@Param("idList") List idList);
- xml文件中写SQL语句
批量更新操作select * from test_table id in #{item}
首先假设项目已经连接好数据库,定义好了实体类 且实体类名为MybatisDTO;
| id | 主键 |
|---|---|
| address | 地址 |
实现需求为通过id批量更新地址信息。
Class MybatisDTO{
Integer id;
String address;
}
接口Controller
@ApiOperation("批量更新")
@PutMapping(value = "/batchUpdate")
public ResponseEntity batchUpdate(@PathVariable("organizationId") Long tenantId,
String defaultAddress,
@RequestBody List mybatisDTOS){
mybatisService.batchUpdate(mybatisDTOS,defaultAddress);
return ResponseEntity.ok(HttpStatus.OK);
}
service层
public interface BatchUpdateService{
void batchUpdate(List mybatisDTOS, String defaultAddress);
}
public class BatchUpdateServiceImpl extends BatchUpdateService{
@Autowird
private BatchUpdateMapper batchUpdateMapper;
public void batchUpdate(List mybatisDTOS,String defaultAddress){
batchUpdateMapper.batchUpdate(mybatisDTOS,defaultAddress);
}
}
mapper层
public interface BatchUpdateMapper{
void batchUpdate(List mybatisDTOS,String defaultAddress);
}
XML文件
update hcbm_sign_archive where #{defaultAddress} when #{a.id} then #{a.archiveAddress} id=#{a.id}
//这种方式一直报错 未解决 item.archiveAddress}--> item.id};-->



