mybatis 组装map里面的集合当作入参
- 入参
{
"1": ["二轮车"],
"9": ["城郊道路", "十字路口"]
}
业务需要查询同一个key里包含多个参数的情况,因为值是字符串所以只能分开查询。
2. 接收
入参我是用Map<String,String[]>来接收的。
3. mapper.xml
select
t1.*
from
json_table t1
where
t1.is_head = false
<if test="content!=null">
<foreach collection="content.entrySet()" item="value" index="key">
<foreach collection="value" item="val">
and (t1.s_json :: json ->> #{key})::text LIKE concat('%',#{val},'%')
</foreach>
</foreach>
</if>
我的值是用jsonb类型存入的,第二层foreach的collection就是第一层的value。
4. 最终SQL
效果如下:
select
t1.*
from
json_table t1
where
t1.is_head = false
and (t1.s_json:: json ->> '1')::text like concat('%','二轮车', '%')
and (t1.s_json:: json ->> '9')::text like concat('%','城郊道路', '%')
and (t1.s_json:: json ->> '9')::text like concat('%','十字路口', '%')