mybatis-plus 不用QueryWrapper怎么实现自定义sql的in查询

mybatis-plus 不用QueryWrapper怎么实现自定义sql in查询

@Select("SELECT s.id submit_id,s.create_time,s.submit_content,s.rule_id,s.is_overtime,s.staff_id,t.staff_name,t.staff_avatar " +
        "FROM application.report_submit s LEFT JOIN application.enterprise_staff t on t.staff_id = s.staff_id " +
        "where s.id in (${coll})")
List<Map<String,Object>> testGetList(@Param(Constants.COLLECTION) List<Integer> ids);

这个生成的sql 后面 是这样where s.id in ([20, 21, 22])
或者

@Select("SELECT s.id submit_id,s.create_time,s.submit_content,s.rule_id,s.is_overtime,s.staff_id,t.staff_name,t.staff_avatar " +
        "FROM application.report_submit s LEFT JOIN application.enterprise_staff t on t.staff_id = s.staff_id " +
        "where s.id in " +
       "<foreach collection=#{ids} item=id index=index" +
        " open=\"(\" close=\")\" separator=\",\">" +
        "   #{id}" +
        "</foreach>")
List<Map<String,Object>> testGetList(@Param("ids") List<Integer> ids);

这个就报错 id找不到 #{id}

因为wrapper好像只能传一个 所以 有些条件得这样写 或者说 可不可以传多个wrapper进查询
@Param(Constants.WRAPPER) Wrapper wrapper

querywrapper 直接in


        new QueryWrapper<>()
                .in()

img

FIND_IN_SET(str,strlist)
str 要查询的字符串
strlist 字段名 参数以”,”分隔 如 (1,2,6,8)

mybaitsplus的wrapper也可以自定义sql的。wrapper.last()

plus直接in就行了。很简单就完事
其次,foreach的collection取值就是list和array之一,而不是参数名。
然后这种sql应该写xml了

Wrapper本身就可以用in方法


  List<Integer> idlist = new ArrayList<>();
                idlist.add(1);
                idlist.add(2);
                queryWrapper.in("id",idlist);

还有你的forerch 写错了,这种复杂的SQL最好使用xml,参考下

img

注意方法参数是ids

接口定义:
@Select("SELECT s.id submit_id,s.create_time,s.submit_content,s.rule_id,s.is_overtime,s.staff_id,t.staff_name,t.staff_avatar " +
        "FROM application.report_submit s LEFT JOIN application.enterprise_staff t on t.staff_id = s.staff_id " +
        "where ${ew.customSqlSegment})")
List<Map<String,Object>> testGetList(@Param(Constants.WRAPPER) QueryWrapper<TNotice> queryWrapper);

调用:
QueryWrapper<TNotice> queryWrapper = new QueryWrapper<>();
queryWrapper.in("id", ids);