{"id":216,"date":"2024-11-30T21:26:21","date_gmt":"2024-11-30T13:26:21","guid":{"rendered":"https:\/\/zhoujibin.com\/?p=216"},"modified":"2025-02-28T15:10:49","modified_gmt":"2025-02-28T07:10:49","slug":"mybatis-plus%e4%b8%8b%e5%ae%9e%e7%8e%b0%e5%af%b9mysql%e7%9a%84json%e5%ad%97%e6%ae%b5%e7%b1%bb%e5%9e%8b%e7%9a%84%e8%8c%83%e5%9b%b4%e6%9f%a5%e8%af%a2","status":"publish","type":"post","link":"https:\/\/zhoujibin.com\/?p=216","title":{"rendered":"MySQL\u4e0b\u5b9e\u73b0JSON\u6570\u636e\u7c7b\u578b\u7684\u8303\u56f4\u67e5\u8be2"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">\u4e1a\u52a1\u573a\u666f<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>MySQL\u4e2d\u9700\u8981\u5bf9\u4e00\u6761\u6570\u636e\u4e2d\u67d0\u4e2a\u5b57\u6bb5\u505a\u7279\u6b8a\u7684\u6269\u5c55\u5904\u7406\uff0c\u6bd4\u5982\uff1a\u4e3a\u7528\u6237\u8bbe\u7f6e\u67d0\u4e9b\u7279\u5b9a\u7684\u6807\u7b7e\uff08\u6807\u7b7e\u5305\u542b\u6392\u5e8f\u3001\u4ee3\u7801\u3001\u540d\u5b57\uff09<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">\u65b9\u6848\u5bf9\u6bd4<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">\u591a\u5b57\u6bb5\u5b58\u50a8(\u4e0d\u63a8\u8350)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5728\u4e3b\u8868\u4e2d\u65b0\u589etag_sort\u3001tage_code\u3001tag_name\uff0c\u591a\u4e2a\u6807\u7b7e\u4f7f\u7528\u7279\u6b8a\u5b57\u7b26\u5206\u5272\u4e14\u5b58\u50a8\u987a\u5e8f\u9700\u8981\u4e00\u4e00\u5bf9\u5e94\uff0c\u793a\u4f8b\uff1a\ntag_sort=1,2,3\ntage_code=tall,rich,cool\ntag_name=\u9ad8,\u5bcc,\u5e05<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b50\u8868(\u63a8\u8350)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\u65b0\u5efatag\u8868\uff0c\u5b58\u50a8\u5173\u8054\u7528\u6237\u7684id\u3001\u6392\u5e8f\u3001\u4ee3\u7801\u3001\u540d\u5b57\u3002<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u6269\u5c55\u5b57\u6bb5\u4f7f\u7528json\u7c7b\u578b\u5b58\u50a8(\u63a8\u8350)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\u76f4\u63a5\u4f7f\u7528tag\u5b57\u6bb5\u5b58\u50a8json\u6570\u636e\uff0c\u793a\u4f8b\uff1a\ntag=&#91;\n{\"tag_sort\":1,\"tage_code\":\"tall\",\"tag_name\":\"\u9ad8\"},\n{\"tag_sort\":2,\"tage_code\":\"rich\",\"tag_name\":\"\u5bcc\"},\n{\"tag_sort\":3,\"tage_code\":\"cool\",\"tag_name\":\"\u5e05\"}\n]<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">\u51c6\u5907\u5de5\u4f5c<\/h1>\n\n\n\n<p>MySQL \u7248\u672c &gt;= 8.0<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u65b0\u5efa\u6d4b\u8bd5\u8868\nCREATE TABLE `demo`.`user`  (\n  `id` bigint(32) NOT NULL COMMENT '\u4e3b\u952eid',\n  `name` varchar(50) NULL COMMENT '\u540d\u79f0',\n  `tag` json NULL COMMENT '\u6807\u7b7e',\n  PRIMARY KEY (`id`)\n);<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">\u6848\u4f8b<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">\u4f7f\u7528\u6269\u5c55\u5b57\u6bb5\u65b9\u5f0f<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u7248\u672c<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>mybatis 3.5.1\nmybatis-plus 3.5.2<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u67e5\u8be2\u6240\u6709\u5305\u542b\u9ad8\u3001\u5bcc\u3001\u5e05\u4e09\u4e2a\u6807\u7b7e\u7684\u7528\u6237<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">SQL\u5b9e\u73b0<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT u.*\nFROM user u,\nJSON_TABLE(\n    u.tag,\n    '$&#91;*]' \n    COLUMNS(\n        tag_code VARCHAR(255) PATH '$.tag_code',\n        tag_sort_no INT PATH '$.tag_sort_no'\n    )\n) AS u\nWHERE u.tag_code IN (1, 2, 3)\norder by id<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">mybatis-plus\u5b9e\u73b0<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>       \/\/ \u4ee3\u7801\u793a\u4f8b\n       List&lt;String&gt; tagCodes = dto.getTagCodes();\n        List&lt;UserEntity&gt; existConfigs = new LambdaQueryChainWrapper&lt;&gt;(this.userMapper)\n                .and(wrapper -&gt; {\n                    Iterator&lt;String&gt; it = tagCodes.iterator();\n                    while (it.hasNext()) {\n                        wrapper.apply(\"JSON_CONTAINS(tag, JSON_OBJECT('tag_code',{0}))\", it.next());\n                        wrapper.or();\n                    }\n                })\n                .orderByAsc(UserEntity::getId)\n                .list();<\/code><\/pre>\n\n\n\n<p>\u66f4\u591a\u4f7f\u7528\u8bed\u6cd5\u53c2\u8003\uff1a<a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.4\/en\/json-function-reference.html\">MySQL-JSON\u51fd\u6570\u624b\u518c<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e1a\u52a1\u573a\u666f \u65b9\u6848\u5bf9\u6bd4 \u591a\u5b57\u6bb5\u5b58\u50a8(\u4e0d\u63a8\u8350) \u5b50\u8868(\u63a8\u8350) \u6269\u5c55\u5b57\u6bb5\u4f7f\u7528json\u7c7b\u578b\u5b58\u50a8(\u63a8\u8350) \u51c6\u5907\u5de5\u4f5c MyS [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[53,22],"class_list":["post-216","post","type-post","status-publish","format-standard","hentry","category-java","tag-mybatis-plus-2","tag-mysql"],"_links":{"self":[{"href":"https:\/\/zhoujibin.com\/index.php?rest_route=\/wp\/v2\/posts\/216"}],"collection":[{"href":"https:\/\/zhoujibin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhoujibin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhoujibin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhoujibin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=216"}],"version-history":[{"count":3,"href":"https:\/\/zhoujibin.com\/index.php?rest_route=\/wp\/v2\/posts\/216\/revisions"}],"predecessor-version":[{"id":221,"href":"https:\/\/zhoujibin.com\/index.php?rest_route=\/wp\/v2\/posts\/216\/revisions\/221"}],"wp:attachment":[{"href":"https:\/\/zhoujibin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhoujibin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhoujibin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}