JSON格式的字段


mysql中定義 json 格式的字段, 在 Spring 中定義 字段:  

private JSON tags;
POSTMAN中 可以傳:   "tags":["aaa"]   或者  "tags":{"qq":"111"}  。
 
如果 mysql 中的 tags 定義json, 存儲的是 json數組,例如: ["test"]  ,則 在 spring 中可以定義字段:
private JSON tags;  或者  private JSONArray tags;

查詢 tags 中是否包含某個 字符串:
    <select id="queryAllPage" resultMap="TtProductContentMap">
        select <include refid="resultColumn"/>
        from lesmart_cms.tt_product_content
        <where>
            <if test="productMode != null and productMode !=''">and product_mode = #{productMode}</if>
            <if test="terminalType != null and terminalType !=''">and terminal_type = #{terminalType}</if>
            <if test="keyWord != null and keyWord !=''">
                and (
                    title like concat('%',#{keyWord},'%')
                    or JSON_LENGTH(tags) = 0
                    or  JSON_CONTAINS(tags, JSON_ARRAY(#{keyWord}))
                )
            </if>
        </where>
    </select>

查詢出的結果:

 

 

 

 SQL

SELECT * FROM `member` m WHERE JSON_CONTAINS(tag, '["VIP"]')
SELECT * FROM `member` m WHERE JSON_CONTAINS(tag, JSON_ARRAY('VIP'))

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM