sql in关键字使用长度超过1000问题处理

sql in关键字使用长度超过1000问题处理

in方法,选定特定自己需要查询的某些值。在这个两面有两个坑,一是目标字符中不能包含特定字符,如[']。二是,当超过1000个字符后,不能直接使用in(‘a’,‘b’,‘c’)等。下面将介绍解决上面两个问题的方法。

原始写法

@Select(" <script>SELECT a.* FROM \"basic\" where delete_mark=0 and id in   <foreach item=\"item\" index=\"index\" separator="," collection=\"list\"  open=\"(\"  close=\")\">  #{item} </foreach> </script> ")
    List<Basic> getLists(@Param("list") List<String> list);

当使用in语法时,后面的数据大于1000条时,可以将数据进行拆分
写法:

@Select(" <script>SELECT a.* FROM \"basic\" where delete_mark=0 and id in   <foreach item=\"item\" index=\"index\" collection=\"list\"  open=\"(\"  close=\")\">\n" +
            " <if  test=\"index !=0\"> <choose> <when test=\"index % 1000 ==999\">  ) or id  in (</when>  <otherwise> ,</otherwise> </choose> </if>  #{item} </foreach> </script> ")
    List<Basic> getLists(@Param("list") List<String> list);

建议在项目中用到in 的时候统一用这种方式实现,避免开发时数据量小,in 里面元素没有超过1000 导致存在隐藏bug

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

princeAladdin

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

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

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

打赏作者

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

抵扣说明:

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

余额充值