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());
        }
相关推荐
新world10 小时前
mybatis-plus从入门到入土(三):持久层接口之IService
mybatis
苦学编程的谢18 小时前
MyBatis_3
java·开发语言·后端·mybatis
guojl19 小时前
MyBatis最佳实践
后端·微服务·mybatis
小白的代码日记21 小时前
MyBatis-Plus 通用 Service
mybatis
Java初学者小白1 天前
秋招Day18 - MyBatis - 基础
java·数据库·mybatis
经典19922 天前
Spring Boot 遇上 MyBatis-Plus:高效开发的奇妙之旅
java·spring boot·mybatis
艺杯羹2 天前
MyBatis 之缓存机制核心解析
java·后端·spring·mybatis
鹦鹉0073 天前
mybatis多对一一对多的关联及拼接操作以及缓存处理
缓存·mybatis
心月狐的流火号3 天前
Mybatis 分页插件 PageHelper SQL异常拼接问题深度分析
java·mybatis
南清的coding日记3 天前
苍穹外卖DAY11
java·开发语言·spring boot·spring·mvc·mybatis