mybatis-plus注解实现数据的批量操作

时间:2025-03-20 13:13:59

1.批量删除

@Delete({"<script>",
            "delete from li_print_goods_connection where printer_id = #{printerId} and goods_id in ",
            "<foreach collection='list' item='goodsId' open='(' separator=',' close=')'>",
            "#{goodsId}",
            "</foreach>",
            "</script>"})
Integer deletePrinterBindAll(String printerId, List<String> list);

2.批量查询

@Select({"<script>" +
            "select goods_name,id from li_goods where id in " +
            "<foreach item='goodsId' index='index' collection='list' open='(' separator=',' close=')'>" +
            "#{goodsId}",
            "</foreach>",
            "</script>"})
List<Goods> getGoodsList(List<String> goodsIds);

同理,更新、插入只需要将注解更换为update和insert即可。

注意:

  1. 参数如果没有@Param指定,collection的值为list,不能为其他,用单引号即可,不用#{}包含
  2. item是循环集合中的单个值引用,与<foreach></foreach>标签内的值对应,标签内的参数需要用#{}包含

借鉴(32条消息) mybaits-plus批量添加、修改、删除(注解)_slowly_jin的博客-****博客_mybatis-plus批量修改