准备工作:实体类
@Data
public class Lu implements Serializable {
private static final long serialVersionUID = -99119486096716759L;
private Integer id;
private String name;
/**
* 上衣尺码
*/
private String topSize;
/**
* 裤子尺码
*/
private String pantsSize;
/**
* 鞋码
*/
private String shoeSize;
}
一、新增
- controller层
@PostMapping("/batch")
public Integer insertBatch(@RequestBody List<Lu> lus){
return luService.insertBatch(lus);
}
- service层
Integer insertBatch(List<Lu> lus);
- mapper层
Integer insertBatch(@Param("lus") List<Lu> lus);
<!-- 批量新增-->
<insert id="insertBatch" parameterType="java.util.List">
insert into lu
(name,top_size,pants_size,shoe_size)
values
<foreach collection="lus" item="lu" separator=",">
(#{lu.name},#{lu.topSize},#{lu.pantsSize},#{lu.shoeSize})
</foreach>
</insert>
传入参数:
[
{
"name": "张三",
"topSize": "111111111",
"pantsSize": "22222222",
"shoeSize": "33333333"
},
{
"name": "李斯",
"topSize": "111111111",
"pantsSize": "22222222",
"shoeSize": "33333333"
},
{
"name": "王五",
"topSize": "111111111",
"pantsSize": "22222222",
"shoeSize": "33333333"
}
]
二、删除
- controller层
@DeleteMapping
public Integer deleteByIds(@RequestParam List<Long> ids){
return luService.deleteByIds(ids);
}
- service层
Integer deleteByIds(List<Long> ids);
- mapper层
Integer deleteByIds(List<Long> ids);
<!-- 批量删除-->
<delete id="deleteByIds">
delete from lu where id in
<foreach collection="list" item="ids" open="(" close=")" separator=",">
#{ids}
</foreach>
</delete>
三、修改
- controller层
@PutMapping("/batch")
public Integer updateBatch(@RequestBody List<Lu> lus){
return luService.updateBatch(lus);
}
- service层
Integer updateBatch(List<Lu> lus);
- mapper层
Integer updateBatch(@Param("lus") List<Lu> lus);
<!-- 批量修改-->
<update id="updateBatch">
update lu
<set>
shoe_size =
<foreach collection="lus" item="lu" separator=" " open="case id" close="end" index="index">
when #{lu.id} then #{lu.shoeSize}
</foreach>
,pants_size =
<foreach collection="lus" item="lu" separator=" " open="case id" close="end" index="index">
when #{lu.id} then #{lu.pantsSize}
</foreach>
</set>
<where>
id in
<foreach collection="lus" index="index" item="lu" separator="," open="(" close=")">
#{lu.id}
</foreach>
</where>
</update>
四、查询
- controller层
@GetMapping("/find")
public List<Lu> findByIds(@RequestParam List<Long> ids) {
return luService.findByIds(ids);
}
- service层
List<Lu> findByIds(List<Long> ids);
- mapper层
List<Lu> findByIds(List<Long> ids);
<!-- 批量查询-->
<select id="findByIds" resultType="com.hsl.entity.Lu">
select id,name,top_size,pants_size,shoe_size from lu
<where>
id in
<foreach collection="list" item="ids" open="(" close=")" separator=",">
#{ids}
</foreach>
</where>
</select>