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());
        }
相关推荐
我登哥MVP12 小时前
Spring Boot 从“会用”到“精通”:Model-Map原理
java·spring boot·后端·spring·servlet·maven·mybatis
Full Stack Developme12 小时前
MyBatis-Plus 分页使用详解
mybatis
落木萧萧82513 小时前
MyBatisGX 批量操作:比 MyBatis-Plus 和 MyBatis-Flex 更好用、更快
mybatis·orm
Jabes.yang14 小时前
Java电商订单系统面试全流程解析:接口设计、数据库、微服务与分布式事务实战
java·微服务·mybatis·分布式事务·电商·订单系统·接口设计
cheems952715 小时前
[开发日记]Spring Boot + MyBatis-Plus 抽奖系统开发复盘:从奖品创建、活动校验到前端圈选人员失效的一次完整排障
前端·spring boot·mybatis
就叫_这个吧1 天前
Java+MySQL+Mybatis+Junit4实现学生信息管理系统
java·mysql·mybatis
噢,我明白了1 天前
MyBatis-Plus 中IPage的分页查询
java·mybatis
basketball6161 天前
Redis基础:3. Redis 持久化(重要)
redis·bootstrap·mybatis
Knight_AL2 天前
MyBatis 报错:Parameter ‘xxx‘ not found 的原因与解决方案
java·tomcat·mybatis
我登哥MVP2 天前
Spring Boot 从“会用”到“精通”:Converter 原理
java·spring boot·servlet·maven·mybatis·converter