mybaits批量查询、新增、删除、修改

该文详细介绍了如何利用MyBatis框架进行批量操作,包括新增实体对象、按ID列表删除记录、更新指定属性以及按ID列表查询数据。示例中展示了Controller、Service和Mapper层的代码实现,涉及到List类型的参数处理及对应的XML映射文件配置。
摘要由CSDN通过智能技术生成


准备工作:实体类

@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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr.han、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值