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

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

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

Mybatis  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中实现:

 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());
        }

相关推荐

  1. mysql 字段组合查询,删除重复

    2024-03-23 16:56:02       13 阅读
  2. mybatis-plus循环处理条件的 or 查询

    2024-03-23 16:56:02       27 阅读
  3. oracle/达梦字段排序,其中字段含有空

    2024-03-23 16:56:02       14 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-23 16:56:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-23 16:56:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-23 16:56:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-23 16:56:02       18 阅读

热门阅读

  1. 日本大带宽服务器优缺点分析

    2024-03-23 16:56:02       20 阅读
  2. mysql 锁的知识点简述

    2024-03-23 16:56:02       17 阅读
  3. 复试专业前沿问题问答合集14——自然语言处理

    2024-03-23 16:56:02       17 阅读
  4. 某笔试题记录

    2024-03-23 16:56:02       19 阅读