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());
        }
相关推荐
敖正炀9 小时前
MyBatis 性能调优:批处理、流式查询与 SQL 优化
mybatis
敖正炀9 小时前
初始化流程的完整串联:从 XML 到 SqlSessionFactory
mybatis
2301_771717219 小时前
Spring Boot 自动配置核心注解
java·spring boot·mybatis
MegaDataFlowers10 小时前
使用MyBatisX快速生成CRUD
mybatis
敖正炀11 小时前
插件开发与拦截链——分页、脱敏、多租户实战
mybatis
敖正炀11 小时前
MyBatis 架构全解:SqlSession、Executor 与 StatementHandler
mybatis
敖正炀11 小时前
一级/二级缓存深度:生命周期、脏读与生产最佳实践
mybatis
空中海14 小时前
MyBatis 基础认知、配置体系与核心映射
mybatis
空中海14 小时前
05 MyBatis 架构设计、渐进式综合项目与专家题库
mybatis
空中海17 小时前
03 MyBatis Spring Boot 集成、事务、测试与工程化体系
spring boot·后端·mybatis