{"id":200824,"date":"2025-05-06T09:25:28","date_gmt":"2025-05-06T01:25:28","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200824\/"},"modified":"2025-05-06T09:25:28","modified_gmt":"2025-05-06T01:25:28","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-spring-boot-%e9%a1%b9%e7%9b%ae%e4%b8%ad%e6%a0%b9%e6%8d%ae%e7%94%a8%e6%88%b7%e6%97%b6%e5%8c%ba%e6%98%be%e7%a4%ba-mysql-datetime-%e5%80%bc%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200824\/","title":{"rendered":"\u5982\u4f55\u5728 Spring Boot \u9879\u76ee\u4e2d\u6839\u636e\u7528\u6237\u65f6\u533a\u663e\u793a MySQL datetime \u503c\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u5728 Spring Boot \u9879\u76ee\u4e2d\u6839\u636e\u7528\u6237\u65f6\u533a\u663e\u793a MySQL datetime \u503c\uff1f<\/h1>\n<p>\u54c8\u55bd\uff01\u4eca\u5929\u5fc3\u8840\u6765\u6f6e\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86\uff0c\u60f3\u5fc5\u5927\u5bb6\u5e94\u8be5\u5bf9\u90fd\u4e0d\u964c\u751f\u5427\uff0c\u90a3\u4e48\u9605\u8bfb\u672c\u6587\u5c31\u90fd\u4e0d\u4f1a\u5f88\u56f0\u96be\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230\uff0c\u82e5\u662f\u4f60\u6b63\u5728\u5b66\u4e60\uff0c\u5343\u4e07\u522b\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u4f60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241116\/17317163106737e4d6e67b7.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u5728 spring boot \u9879\u76ee\u4e2d\u6307\u5b9a mysql <\/strong>datetime<strong> \u503c\u7684\u65f6\u533a<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u9700\u8981\u5728 spring boot \u9879\u76ee\u4e2d\u67e5\u8be2 mysql \u6570\u636e\u5e93\u4e2d\u7684 <strong>datetime<\/strong> \u503c\uff0c\u4f46\u5e0c\u671b\u6839\u636e\u7528\u6237\u7684\u65f6\u533a\u663e\u793a\u6570\u636e\u3002\u4f46\u662f\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6570\u636e\u4f1a\u8f6c\u6362\u4e3a\u5e94\u7528\u670d\u52a1\u5668\u7684\u65f6\u533a\u3002\u5982\u4f55\u6307\u5b9a\u663e\u793a\u65f6\u533a\u5e76\u89e3\u51b3\u6b64\u95ee\u9898\uff1f<\/p>\n<p><strong>\u56de\u7b54\uff1a<\/strong><\/p>\n<p>\u60f3\u8981\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u9700\u8981\u5c06\u4ee5\u4e0b\u6b65\u9aa4\u7ec4\u5408\u4f7f\u7528\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 jackson2objectmapperbuilder<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u4f7f\u7528 jackson2objectmapperbuilder\uff0c\u8fd9\u662f\u4e00\u79cd\u53ef\u6269\u5c55\u7684\u914d\u7f6e\u5668\uff0c\u53ef\u4ee5\u914d\u7f6e\u7528\u4e8e\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u7684 jackson \u5bf9\u8c61\u6620\u5c04\u5668\u3002\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684 jackson2objectmapperbuilder bean\uff0c\u53ef\u4ee5\u8986\u76d6 spring boot \u7684\u9ed8\u8ba4\u5e8f\u5217\u5316\u884c\u4e3a\u3002<\/p>\n<pre>@bean\n@primary\npublic jackson2objectmapperbuilder jackson2objectmapperbuilder() {\n    return new jackson2objectmapperbuilder().serializerbytype(date.class, new dateserializer());\n}<\/pre>\n<p><strong>2. \u81ea\u5b9a\u4e49 stdserializer<\/strong><\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684 stdserializer\uff0c\u5b83\u5c06\u8986\u76d6 date \u5bf9\u8c61\u7684\u9ed8\u8ba4\u5e8f\u5217\u5316\u884c\u4e3a\u3002\u5728 serialize \u65b9\u6cd5\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u4fee\u6539\u65f6\u533a\u3002<\/p>\n<pre>public class DateSerializer extends StdSerializer&lt;Date&gt; {\n    protected DateSerializer() {\n        super(Date.class);\n    }\n\n    @Override\n    public void serialize(Date value, JsonGenerator gen, SerializerProvider provider) throws IOException {\n        \/\/ \u5728\u6b64\u5904\u5904\u7406\u65f6\u533a\n        gen.writeString(value.toString() + \"\u8fd9\u662f\u4e00\u4e2a\u6d4b\u8bd5\");\n    }\n}<\/pre>\n<p><strong>3. \u786e\u5b9a\u5ba2\u6237\u7aef\u65f6\u533a<\/strong><\/p>\n<p>\u8fd8\u9700\u8981\u786e\u5b9a\u5ba2\u6237\u7aef\u7684\u65f6\u533a\u3002\u6b64\u4fe1\u606f\u53ef\u4ee5\u4ece\u8bf7\u6c42\u5934\u3001http cookie \u6216\u4f1a\u8bdd\u4e2d\u83b7\u53d6\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u5e94\u7528\u7a0b\u5e8f\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n<p><strong>4. \u8f6c\u6362\u65f6\u533a<\/strong><\/p>\n<p>\u4e00\u65e6\u786e\u5b9a\u4e86\u5ba2\u6237\u7aef\u65f6\u533a\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 java datetimeformatter \u6216 calendar \u7c7b\u5c06\u65e5\u671f\u503c\u8f6c\u6362\u4e3a\u6240\u9700\u7684\u65f6\u533a\u3002<\/p>\n<p><strong>5. \u8fd4\u56de\u65f6\u533a\u8f6c\u6362\u540e\u7684\u6570\u636e<\/strong><\/p>\n<p>\u6700\u540e\uff0c\u5728\u63a7\u5236\u5668\u4e2d\u8fd4\u56de\u65f6\u533a\u8f6c\u6362\u540e\u7684\u65e5\u671f\u503c\u3002<\/p>\n<p>\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u6307\u5b9a datetime \u503c\u7684\u65f6\u533a\uff0c\u5e76\u6839\u636e\u7528\u6237\u7684\u672c\u5730\u65f6\u533a\u663e\u793a\u6570\u636e\u3002<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u5728 Spring Boot \u9879\u76ee\u4e2d\u6839\u636e\u7528\u6237\u65f6\u533a\u663e\u793a MySQL datetime \u503c\uff1f\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6570\u636e\u5e93\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5728 Spring Boot &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-200824","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200824","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=200824"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200824\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200824"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200824"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200824"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}