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());
        }
相关推荐
Amagi.1 小时前
Redis的内存淘汰策略
数据库·redis·mybatis
执键行天涯6 小时前
【经验帖】JAVA中同方法,两次调用Mybatis,一次更新,一次查询,同一事务,第一次修改对第二次的可见性如何
java·数据库·mybatis
工业甲酰苯胺7 小时前
Spring Boot 整合 MyBatis 的详细步骤(两种方式)
spring boot·后端·mybatis
ggdpzhk8 小时前
Mybatis 快速入门(maven)
oracle·maven·mybatis
Java小白笔记18 小时前
关于使用Mybatis-Plus 自动填充功能失效问题
spring boot·后端·mybatis
计算机学姐1 天前
基于SpringBoot+Vue的篮球馆会员信息管理系统
java·vue.js·spring boot·后端·mysql·spring·mybatis
程序员大金1 天前
基于SpringBoot+Vue+MySQL的智能物流管理系统
java·javascript·vue.js·spring boot·后端·mysql·mybatis
惜.己1 天前
MyBatis中一对多关系的两种处理方法
java·开发语言·后端·sql·mysql·mybatis·idea
终末圆1 天前
MyBatis动态SQL中的`if`标签使用【后端 19】
java·数据结构·数据库·sql·算法·spring·mybatis
飞翔的佩奇1 天前
Java项目: 基于SpringBoot+mybatis+maven医院管理系统(含源码+数据库+任务书+开题报告+毕业论文)
java·数据库·spring boot·毕业设计·maven·mybatis·医院管理系统