背景
项目中有个需求,根据多个整型id查询对应的信息
理论上最终sql应该是如下所示:
select userName form t_user where userId in(1,2,3,4,5,6);
但前端传来的参数
Integer[] userIdArr
想着就把userIdArr转成1,2,3,4,5,6就行了
/**
* [1,2,3] to 1,2,3
*
* @return
*/
public static String intArrToString(Integer[] arr) {
return StringUtils.join(arr, ",");
}
然后将字符串形式的userArrIds作为参数传给mappe,于是写成动态sql如下
@Select("select userName from t_user where userId in (#{userArrIds})")
List<String> getUserNameByUserIds(String userArrIds);
按照预想的,它应该生成如下sql语句
select userName form t_user where userId in(1,2,3,4,5,6);
自测一下,发现无论userIdArr传多少个,都只能查出第一个,郁闷了半天

在项目中遇到根据多个整型ID查询信息的需求,尝试使用MyBatisPlus的动态SQL`QueryWrapper`及`@Select`配合`script`标签,遇到将Integer数组转换为字符串导致的查询问题。总结了从数组到字符串转换错误、使用QueryWrapper的限制,到最后采用`@Select`和`script`避免二次转换的优雅解决方案。
最低0.47元/天 解锁文章
775

被折叠的 条评论
为什么被折叠?



