mybatis + mysql json精准查询

原创
03/30 19:58
阅读数 316

参数:

{
	"productName": "华为MatePad Pro",
	"managerAttrs": "{\"code\":\"OKs\"}",
	"technicalAttrs": "{\"code\":\"OK\"}",
	"simplePageInfo": {
		"pageNum": "1",
		"pageSize": "10"
	}
}

代码:

        List<Map<String, String>> managers = new ArrayList<>();

        //.......

        if (StringUtils.isNotBlank(queryProductVo.getManagerAttrs())){
            JSONObject jsonManager = JSONObject.parseObject(queryProductVo.getManagerAttrs());
            Map<String, Object> mapManager = jsonManager;
            for (Map.Entry<String, Object> m : mapManager.entrySet()) {
                Map<String, String> m1 = new HashMap<>();
                m1.put("k", m.getKey());
                m1.put("v", String.valueOf(m.getValue()));
                managers.add(m1);
            }
            productMap.put("managerList", managers);
        }
        Page<Map<String, String>> productList = productMapper.queryProductList(productMap);

实体类(部分)字段:

  @ApiModelProperty("技术属性")
  private String technicalAttrs;

  @ApiModelProperty("管理属性")
  private String managerAttrs;

  private List<Map<String, String>> technicalAttrList;

  private List<Map<String, String>> managerList;

Mapper:

 <if test="technicalAttrList!=null and technicalAttrList!=''">
      <foreach collection="technicalAttrList" item="item" index="index" open=" " separator=" " close=" ">
            AND TECHNICAL_ATTRS->'$.${item.k}' = #{item.v}
      </foreach>
</if>

 <if test="managerList!=null and managerList!=''">
      <foreach collection="managerList" item="item" index="index" open=" " separator=" " close=" ">
           AND MANAGER_ATTRS->'$.${item.k}' = #{item.v}
      </foreach>
 </if>

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部