mysql字段多个值,mybatis/mybatis-plus匹配查询

mysql中有一个字段是字符串类型的,category字段值有多个用逗号分割的,例如:娱乐,时尚美妆,美食 。现在想实现这么一个功能, 前端传参 字符串,美食,娱乐。现在想在mybatis的xml中实现,查询,能查到category只要包含美食或者娱乐的数据行。

数据如下示例,category字段中有多个值,country字段中只有一个值

Mybatis xml 查询如下:

XML 复制代码
 <where>
           
            <if test="sex!= null and sex!=''">
                and sex= #{sex}
            </if>
            <if test="country != null and country != ''">
                AND country IN
                <foreach item="item" index="index" collection="country.split(',')" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="category != null and category != ''">
                and
                <foreach item="item" index="index" collection="category.split(',')" open="(" separator="OR" close=")">
                    FIND_IN_SET(#{item}, category) > 0
                </foreach>
            </if>
        </where>

Mybatis-plus中实现:

java 复制代码
 QueryWrapper<对象> lqw = new QueryWrapper<>();
            if (StringUtils.isNotEmpty(paramObj.getCountry())) {
                String[] split = paramObj.getCountry().split(",");
                List<String> countryList = Arrays.asList(split);
                lqw.in("country", countryList);
            }

           
        if (StringUtils.isNotEmpty(paramObj.getCategory())) {
            String[] split = paramObj.getCategory().split(",");

            StringBuilder sb = new StringBuilder();
            sb.append("(");
            for(int i = 0; i< split.length; i++){
                String catgory = split[i];
                if(i == split.length -1){
                    sb.append("FIND_IN_SET('" + catgory + "', category) > 0").append(")");
                }else{
                    sb.append("FIND_IN_SET('" + catgory + "', category) > 0").append(" or ");
                }

            }

            lqw.apply(sb.toString());
        }
相关推荐
雁凡彡9 小时前
mybatis-plus中sqlserver 查询数组中指定位置前的数据
数据库·sqlserver·mybatis
alonewolf_999 小时前
Spring整合Mybatis底层源码深度解析:从@MapperScan到SQL执行的完整流程
spring·mybatis
小肖爱笑不爱笑11 小时前
JDBC Mybatis
数据库·mybatis
while(1){yan}12 小时前
图书管理系统(超详细版)
spring boot·spring·java-ee·tomcat·log4j·maven·mybatis
林shir15 小时前
3.6-Web后端基础(java操作数据库)
spring·mybatis
super_lzb1 天前
mybatis拦截器ParameterHandler详解
java·数据库·spring boot·spring·mybatis
CodeAmaz1 天前
MyBatis 分页插件实现原理(Interceptor 机制 + SQL 改写)
mybatis·分页插件
CodeAmaz2 天前
MyBatis 如何实现“面向接口”查询
mybatis·面向接口
此剑之势丶愈斩愈烈2 天前
mybatis-plus乐观锁
开发语言·python·mybatis
雨中飘荡的记忆2 天前
MyBatis数据源模块详解
mybatis