{"id":888,"date":"2020-04-30T14:34:30","date_gmt":"2020-04-30T06:34:30","guid":{"rendered":"http:\/\/toradora.xyz\/?p=888"},"modified":"2020-05-28T15:57:54","modified_gmt":"2020-05-28T07:57:54","slug":"%e5%9f%ba%e4%ba%8ejpa%e7%9a%84%e5%b1%9e%e6%80%a7%e8%bd%ac%e6%8d%a2%e5%99%a8attributeconverter","status":"publish","type":"post","link":"https:\/\/diuut.com\/?p=888","title":{"rendered":"\u57fa\u4e8eJPA\u7684\u5c5e\u6027\u8f6c\u6362\u5668AttributeConverter"},"content":{"rendered":"\n<p>\u6570\u636e\u5e93\u5b58\u653e\u7684\u6570\u636e\u7c7b\u578b\u6709\u9650\uff0c\u4e14\u4f7f\u7528\u4e5f\u4e0d\u65b9\u4fbf\uff0c\u56e0\u6b64\u9700\u8981\u5b9e\u73b0\u76f4\u63a5\u4ece\u6620\u5c04\u7684\u5b9e\u4f53\u7c7b\u4e2d\u62ff\u5230\u6bd4\u5982JSONObject\u7b49\u683c\u5f0f\u7684\u6570\u636e\uff0c\u53cd\u4e4b\uff0c\u5b58\u53d6\u7684\u65f6\u5019\u6211\u4eec\u4e5f\u6309\u7167\u5bf9\u5e94\u7684\u683c\u5f0f\u8fdb\u884c\u5b58\u53d6\u8f6c\u6362\u3002<br> \u8981\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\u9700\u8981\u5b9e\u73b0AttributeConverter\u8fd9\u4e2a\u63a5\u53e3<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n&lt;pre class=&quot;wp-block-syntaxhighlighter-code&quot;&gt;\nimport com.miracle.dson.Dson;\nimport org.springframework.stereotype.Component;\n\nimport javax.persistence.AttributeConverter;\n\n\/**\n * @Author Diuut\n * @Date 2020\/4\/24  14:03\n *\/\n\/\/\u5b9e\u4f53\u5c5e\u6027\u7c7b\u578b\u8f6c\u6362\u5668\n@Component\npublic class DsonMapConverter implements AttributeConverter&amp;amp;amp;lt;Dson, String&amp;amp;gt; {\n    @Override\n    public String convertToDatabaseColumn(Dson attribute) {\n        \/\/\u653e\u5230\u6570\u636e\u5e93\u4e2d\u7684\u5904\u7406\u65b9\u5f0f\n        return attribute.asString();\n    }\n\n    @Override\n    public Dson convertToEntityAttribute(String dbData) {\n       \/\/\u4ece\u6570\u636e\u5e93\u4e2d\u53d6\u51fa\u6765\u7684\u5904\u7406\u65b9\u5f0f\n        return Dson.fromMap(dbData);\n    }\n}\n&lt;\/pre&gt;\n<\/pre><\/div>\n\n\n<p>\u4e4b\u540e\u5c31\u53ef\u4ee5\u6309\u7167\u81ea\u5b9a\u4e49\u683c\u5f0f\u76f4\u63a5\u4f7f\u7528\u8be5\u5b9e\u4f53\u7c7b\u5b57\u6bb5\u3002<\/p>\n\n\n\n<p>\u8bb0\u7740\u5728\u8f6c\u6362\u7684\u5b57\u6bb5\u4e0a\u52a0\u4e0a   @Convert(converter = DsonMapConverter.class)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6570\u636e\u5e93\u5b58\u653e\u7684\u6570\u636e\u7c7b\u578b\u6709\u9650\uff0c\u4e14\u4f7f\u7528\u4e5f\u4e0d\u65b9\u4fbf\uff0c\u56e0\u6b64\u9700\u8981\u5b9e\u73b0\u76f4\u63a5\u4ece\u6620\u5c04\u7684\u5b9e\u4f53\u7c7b\u4e2d\u62ff\u5230\u6bd4\u5982JSONObject\u7b49\u683c\u5f0f\u7684<span class=\"more-button\"><a href=\"https:\/\/diuut.com\/?p=888\" class=\"more-link\">view all . . .<span class=\"screen-reader-text\">\u57fa\u4e8eJPA\u7684\u5c5e\u6027\u8f6c\u6362\u5668AttributeConverter<\/span><\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":657,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[7],"_links":{"self":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/888"}],"collection":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=888"}],"version-history":[{"count":2,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/888\/revisions"}],"predecessor-version":[{"id":996,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/888\/revisions\/996"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/media\/657"}],"wp:attachment":[{"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=888"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}