mysql存json数据时的查询办法

很多时候mysql的一列当中存的是json格式的数据,这时候如果要查询某个key对应的值的时候要如何查询呢,这里记录一种查询方法:

json列的值:
{“InventoryMainTypeCode”: 1, “InventoryMainTypeName”: “GOOD”}

现在要查询InventoryMainTypeCode为xxx或者InventoryMainTypeName为xxx的数据:(这里以springboot+mybatis为例子)

代码:
请求类:

Query query = new Query();
@Data
public class Query implements Serializable {
   

    
    private Map<String, Object> featureMap = new HashMap<>();

    public void setFeature(String feature) {
   
        if (StringUtils.isNotBlank(feature)) {
   
            featureMap = JSON.parseObject(feature, Map.class);
        }
    }

    public void addFeature(String key, Object value) {
   
        if (StringUtils.isBlank(key)) {
   
            return;
        }
        this.featureMap.put(key, value);
    }
    public void addFeatureMap(Map<String, Object> featureMaps) {
   
        if (MapUtils.isNotEmpty(featureMaps)){
   
            this.featureMap.putAll(featureMaps);
        }
    }
}

在代码中将key添加到Feature中:

query.addFeature(DicConst.InventoryMainTypeCode.name(), 1);
List<ResultDTO> dTOS = dictionaryManager.queryDicByParam(query);

mapper文件:

 List<CnbDictionaryDO> queryDicByParam(CnbDictionaryQuery query);

xml:

  <select id="queryDicByParam" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from
        <include refid="Table_Name"/>
        <where>
            <if test="id != null">
                and id = #{id}
            </if>
         
            <if test="featureMap != null and featureMap.size > 0">
                <foreach collection="featureMap" index="key" item="value">
                    <if test="value != null">
                        <![CDATA[and feature->'$.${key}' = #{value}]]>
                    </if>
                </foreach>
            </if>
        </where>
    </select>

相关推荐

  1. mysqljson数据查询办法

    2023-12-09 09:04:02       60 阅读
  2. mybatis查询修改mysqljson字段

    2023-12-09 09:04:02       48 阅读
  3. mysql json查询去除值引号

    2023-12-09 09:04:02       31 阅读
  4. mysql实现json数据解析

    2023-12-09 09:04:02       28 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-09 09:04:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-09 09:04:02       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-09 09:04:02       82 阅读
  4. Python语言-面向对象

    2023-12-09 09:04:02       91 阅读

热门阅读

  1. 2312d,d语言来绑定C++和rust

    2023-12-09 09:04:02       58 阅读
  2. 微服务和无服务器架构时代的持续测试

    2023-12-09 09:04:02       66 阅读
  3. centos7.9 安装sersync+rsync 服务器数据实时同步

    2023-12-09 09:04:02       64 阅读
  4. vue 批量下载文件,不走后端接口的方法

    2023-12-09 09:04:02       65 阅读
  5. nginx 的概念、高并发处理及详细参数配置

    2023-12-09 09:04:02       59 阅读
  6. Axios

    Axios

    2023-12-09 09:04:02      52 阅读
  7. es6 相关面试总结

    2023-12-09 09:04:02       60 阅读
  8. elasticsearch中LessThen遇到的坑

    2023-12-09 09:04:02       35 阅读
  9. python写数据进es中

    2023-12-09 09:04:02       56 阅读