• 记录 mybatis plus QuerWapper使用 FIND_IN_SET


    1.需求:

        一条记录属于多个分类时,使用 MySQL 自带函数 find_in_set 实现多值匹配查询

    例如:

         查询楼盘名称为name=xxx,查询出楼盘ID为:1,2,3
        方案表存的是楼盘ID信息; real_estate_ids ={1,2,5,8,9} 这条记录里面,怎么在方案表查出楼盘为xxx的数据呢?

    2.分析:

     (1)表名 (crm_programme)

     

      (2).表字段楼盘ID(real_estate_ids)

    3.MySQL 提供的 find_in_set 函数,进行数据的查询
        find_in_set 用法:
        find_in_set(参数一,参数二)

     参数说明:

        参数一:想要查询的分类值,如 1
        参数二:方案表中的分类所存储的字段,如 real_estate_ids

    方案表(crm_programme) 楼盘ID,有多个,以英文 , 相隔,如 1,2,5,8,9


    # 查询 real_estate_ids = 1 or real_estate_ids = 3 的记录

    第一种方法:直接使用自定义查询语句,使用Mysql中的find_in_set 函数。

          SELECT * FROM `crm_programme` where find_in_set(1,real_estate_ids) or find_in_set(3,real_estate_ids);

    第二种方法:利用mybatis-plus的lambda().apply 拼接SQL,代码如下
      List  realIds = crmProgrammeService.getIdsByRealEstateNames(name);
      if (!CollectionUtils.isEmpty(realIds)){
                    if (realIds.size() == 1){
                        queryWrapper.like("real_estate_ids", realIds.get(0));
                    } else {
                        for (int i = 0; i < realIds.size(); i++) {
                            if (i == 0) {
                                queryWrapper.apply(null != realIds.get(i), "(find_in_set({0},real_estate_ids)", realIds.get(i));
                            } else {
                                if (i == realIds.size() - 1) {
                                    queryWrapper.or().apply(null != realIds.get(i), "find_in_set({0},real_estate_ids))", realIds.get(i));
                                } else {
                                    queryWrapper.or().apply(null != realIds.get(i), "find_in_set({0},real_estate_ids)", realIds.get(i));
                                }
                            }
                        }
                    }
     } else {
         queryWrapper.isNull("real_estate_ids");
     }

  • 相关阅读:
    Python将图片转换为ASCII字符画
    积分商城运营成功的7个关键要素
    JavaScript混淆加密
    Spring Cloud之BootstrapApplicationListener巧妙构建双容器
    leetcode 205. 同构字符串
    【SpringMVC】拦截器&JSR303的使用
    TLS/SSL(九) TLS1.2与TLS1.3中的ECDH协议
    Javaweb之javascript的详细解析
    php实战案例记录(22)smarty模版引擎数组循环的方式
    ​“债务飙升!美国一天内增加2750亿美元,金融震荡的前奏已拉开帷幕!”
  • 原文地址:https://blog.csdn.net/qq_34410726/article/details/126561485