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());
        }
相关推荐
java—大象10 天前
基于java SSM的房屋租赁系统设计和实现
java·开发语言·数据库·spring boot·layui·mybatis
Mutig_s10 天前
Spring Boot动态数据源切换:优雅实现多数据源管理
java·数据库·spring boot·后端·mybatis
编程乐学(Arfan开发工程师)10 天前
73、单元测试-断言机制
服务器·数据库·servlet·单元测试·sqlite·log4j·mybatis
小时候的阳光10 天前
MyBatis 的一级缓存导致的数据一致性问题分析
缓存·mybatis·事务·隔离级别
烙印60110 天前
MyBatis原理剖析(三)--加载配置文件
服务器·tomcat·mybatis
demon755200310 天前
Mybatis-Plus支持多种数据库
mybatis·多数据库
能工智人小辰11 天前
二刷苍穹外卖 day03
java·开发语言·mybatis
2401_8260976211 天前
JavaEE-Mybatis初阶
java·java-ee·mybatis
剽悍一小兔11 天前
一个小BUG引发的对Mybatis-Plus的模糊查询的思考
bug·mybatis