{"id":10006355,"date":"2022-08-31T11:10:35","date_gmt":"2022-08-31T03:10:35","guid":{"rendered":"https:\/\/ityet.com\/?p=10006355"},"modified":"2022-08-31T11:10:35","modified_gmt":"2022-08-31T03:10:35","slug":"%e7%bb%99shardingsphere%e6%8f%90%e4%ba%86%e4%b8%aapr%ef%bc%8c%e4%b8%8d%e7%9f%a5%e9%81%93%e6%98%af%e4%b8%8d%e6%98%af%e5%ab%8c%e5%bc%83%e6%88%91","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006355","title":{"rendered":"\u7ed9ShardingSphere\u63d0\u4e86\u4e2aPR\uff0c\u4e0d\u77e5\u9053\u662f\u4e0d\u662f\u5acc\u5f03\u6211"},"content":{"rendered":"<p>\u8bf4\u6765\u60ed\u6127\uff0c\u5e72\u4e86 10 \u6765\u5e74\u7a0b\u5e8f\u5458\uff0c\u8fd8\u6ca1\u6709\u7ed9\u5f00\u6e90\u505a\u8fc7\u4efb\u4f55\u8d21\u732e\uff0c\u4ee5\u524d\u53ea\u77e5\u9053\u560e\u560e\u5199\uff0c\u51fa\u4e86\u95ee\u9898\u560e\u560e\u6539\uff0c\u4ece\u6765\u6ca1\u60f3\u8fc7\u63d0\u4e2a PR \u53bb\u4fee\u590d\u4ed6\uff0c\u6700\u8fd1\u78b0\u5230\u4e2a\u95ee\u9898\uff0c\u53d1\u73b0\u633a\u7b80\u5355\u7684\uff0c\u5c31\u968f\u624b\u63d0\u4e86\u4e2a PR \u8fc7\u53bb\u3002<\/p>\n<h2>\u95ee\u9898<\/h2>\n<p>\u95ee\u9898\u633a\u7b80\u5355\u7684\uff0c\u5c31\u662f\u5728\u4f7f\u7528 mybatis \u548c ShardingSphere \u7684\u65f6\u5019\uff0c\u6709\u4eba\u5728 model \u7c7b\u4f7f\u7528\u4e86 <code>OffsetDateTime<\/code> \u8fd9\u4e2a\u65f6\u95f4\u7c7b\u578b\uff0c\u53d1\u73b0\u4f1a\u62a5\u9519\u3002<\/p>\n<pre><code class=\"java\">Caused by: java.lang.ClassCastException: class java.sql.Timestamp cannot be cast to class java.time.OffsetDateTime (java.sql.Timestamp is in module java.sql of loader 'platform'; java.time.OffsetDateTime is in module java.base of loader 'bootstrap')\n    at org.apache.ibatis.type.OffsetDateTimeTypeHandler.getNullableResult(OffsetDateTimeTypeHandler.java:38)\n    at org.apache.ibatis.type.OffsetDateTimeTypeHandler.getNullableResult(OffsetDateTimeTypeHandler.java:28)\n    at org.apache.ibatis.type.BaseTypeHandler.getResult(BaseTypeHandler.java:85)\n    ... 99 more<\/code><\/pre>\n<p>\u8fd9\u5c31\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u7c7b\u578b\u8f6c\u6362\u7684\u5f02\u5e38\uff0c\u4e8e\u662f\u8ddf\u7740\u6e90\u7801\u770b\u4e86\u4e0b\uff0c\u5148\u770b\u5230<code>BaseTypeHandler#getResult<\/code>\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u5b9e\u9645\u4e0a\u5c31\u662f\u6839\u636e\u5217\u540d\u8fd4\u56de\u67e5\u8be2\u7ed3\u679c\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042399298\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u6839\u636e\u8c03\u7528\u5173\u7cfb\uff0c\u627e\u5230\u4e86<code>OffsetDateTimeTypeHandler<\/code>\u5b9e\u73b0\u7c7b\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042399299\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u53d1\u73b0\u6700\u7ec8\u4f1a\u8c03\u7528<code>rs.getObject()<\/code>\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u90a3\u4e48\u5176\u5b9e\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u6700\u7ec8\u8d70\u5230\u7531 ShardingSphere \u5b9e\u73b0\u7684 <code>getObject<\/code>\u65b9\u6cd5\u4e2d\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042399300\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u770b\u5230\u8fd9\u91cc\u7684\u65f6\u5019\u5176\u5b9e\u5df2\u7ecf\u660e\u767d\u4e86\u4e3a\u5565\u4f1a\u62a5\u9519\u4e86\uff0cShardingsphere \u53ea\u5224\u65ad\u4e86\u51e0\u4e2a<code>LocalDateTime<\/code>\u7b49\u7c7b\u578b\uff0c\u5bf9\u4e8e\u8fd9\u4e2a\u6bd4\u8f83\u7279\u6b8a\u7684\u65f6\u95f4\u7c7b\u578b\u6ca1\u6709\u5904\u7406\uff0c\u6700\u7ec8\u4f1a\u8f6c\u6362\u6210 Timestamp \uff0c\u7136\u540e\u5f3a\u8f6c\u5c31\u62a5\u9519\u4e86\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042399301\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u6700\u540e\u8c03\u7528\u5230<code>ResultSetUtil#convertTimestampValue<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u770b\u5230\u786e\u5b9e\u662f\u8fd9\u6837\u54c8\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042399302\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u90a3\u5982\u679c\u4fee\u6539\u6e90\u7801\u7684\u8bdd\u5176\u5b9e\u5f88\u7b80\u5355\u4e86\uff0c<code>getObject<\/code>\u5224\u65ad\u591a\u52a0\u4e00\u4e2a\uff0c<code>convertTimestampValue<\/code>\u518d\u52a0\u4e00\u4e2a\uff0c\u5c31\u8fd9\u6837\u5f88\u7b80\u5355\u554a\u3002<\/p>\n<pre><code class=\"java\">@Override\n    public &lt;T&gt; T getObject(final int columnIndex, final Class&lt;T&gt; type) throws SQLException {\n        if (BigInteger.class.equals(type)) {\n            return (T) BigInteger.valueOf(getLong(columnIndex));\n        } else if (Blob.class.equals(type)) {\n            return (T) getBlob(columnIndex);\n        } else if (Clob.class.equals(type)) {\n            return (T) getClob(columnIndex);\n        } else if (LocalDateTime.class.equals(type) || LocalDate.class.equals(type) || LocalTime.class.equals(type) || OffsetDateTime.class.equals(type)) {\n            return (T) ResultSetUtil.convertValue(mergeResultSet.getValue(columnIndex, Timestamp.class), type);\n        } else {\n            return (T) ResultSetUtil.convertValue(mergeResultSet.getValue(columnIndex, type), type);\n        }\n    }\n    \nprivate static Object convertTimestampValue(final Object value, final Class&lt;?&gt; convertType) {\n        Timestamp timestamp = (Timestamp) value;\n        if (LocalDateTime.class.equals(convertType)) {\n            return timestamp.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();\n        }\n        if (LocalDate.class.equals(convertType)) {\n            return timestamp.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();\n        }\n        if (LocalTime.class.equals(convertType)) {\n            return timestamp.toInstant().atZone(ZoneId.systemDefault()).toLocalTime();\n        }\n        if (OffsetDateTime.class.equals(convertType)) {\n            return timestamp.toInstant().atZone(ZoneId.systemDefault()).toOffsetDateTime();\n        }\n        return value;\n}<\/code><\/pre>\n<h2>\u4fee\u590d<\/h2>\n<p>\u6700\u5f00\u59cb\u6211\u5176\u5b9e\u5e76\u4e0d\u60f3\u6539\u6e90\u7801\uff0c\u6211\u5728\u60f3\u5176\u4ed6\u7684\u5b9e\u73b0\u65b9\u6848\uff0c\u641c\u7d22\u540e\u53d1\u73b0\u5f15\u5165\u4e00\u4e2a\u5305\u5c31\u53ef\u4ee5\u89e3\u51b3\uff0c\u4e5f\u5c31\u662f mybatis \u7684 JSR310 \u89c4\u8303\u3002<\/p>\n<pre><code class=\"java\">&lt;dependency&gt;\n  &lt;groupId&gt;org.mybatis&lt;\/groupId&gt;\n  &lt;artifactId&gt;mybatis-typehandlers-jsr310&lt;\/artifactId&gt;\n  &lt;version&gt;1.0.1&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\u4ed6\u4e3a\u4ec0\u4e48\u80fd\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff1f\u6211\u770b\u4e86\u4e0b\u4ed6\u7684\u5305\u91cc\u9762\u7684\u4ee3\u7801\uff0c\u8fd9\u4e0d\u5c31\u662f\u52a0\u4e86\u4e2a TypeHandler \u81ea\u5df1\u5904\u7406\u4e86\u561b\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042399303\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u518d\u53bb\u770b\u4e86\u4e0b <code>OffsetDateTimeTypeHandler<\/code>\u7684\u5b9e\u73b0\uff0c\u5176\u5b9e\u5c31\u662f\u81ea\u5df1\u5c31\u89e3\u51b3\u4e86\uff0c\u76f4\u63a5\u7ed9\u8fd4\u56de<code>OffsetDateTime<\/code> \uff0c\u6839\u672c\u4e0d\u4f1a\u8d70\u5230 ShardingSphere \u7684\u903b\u8f91\u91cc\u9762\u53bb\uff0c\u8fd9\u4e5f\u5c31\u662f\u4ed6\u80fd\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u539f\u56e0\u4e86\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042399304\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u5f53\u7136\uff0c\u5982\u679c\u4e0d\u60f3\u90a3\u4e48\u9ebb\u70e6\u5f15\u5165\u4e00\u4e2a\u5305\uff0c\u4e5f\u53ef\u4ee5\u5355\u72ec\u628a\u4ed6\u62ce\u51fa\u6765\u81ea\u5df1\u53bb\u6307\u5b9a\u4e00\u4e0b\uff0c\u8fd9\u4e2a\u5f88\u7b80\u5355\uff0c\u5c31\u4e0d\u591a\u8bf4\u4e86\u3002<\/p>\n<h2>\u63d0PR<\/h2>\n<p>\u4e8e\u662f\u6211\u60f3\uff0c\u8fd9\u4e8b\u60c5\u8fd9\u4e48\u7b80\u5355\uff0c\u6211\u4e0d\u5982\u63d0\u4e2a PR \u7ed9\u5b98\u65b9\u5427\uff0c\u8fd9\u91cc\u6559\u4e0b\u5927\u5bb6\u600e\u4e48\u63d0 PR \u3002<\/p>\n<p>\u56e0\u4e3a\u4e0d\u662f\u54b1\u4eec\u7684\u9879\u76ee\uff0c\u662f\u6ca1\u6cd5 push \u4ee3\u7801\u7684\uff0c\u6240\u4ee5\u8fdb\u5165\u5230\u9879\u76ee\uff0c\u7136\u540e<code>fork<\/code>\uff0cfork \u597d\u4e86\u4ee5\u540e\uff0c\u76f4\u63a5\u628a\u9879\u76ee clone \u4e0b\u6765\uff0c\u7136\u540e\u6267\u884c\u547d\u4ee4\u3002<\/p>\n<pre><code class=\"java\">git remote add upstream https:\/\/github.com\/apache\/shardingsphere.git<\/code><\/pre>\n<p>\u901a\u8fc7\u547d\u4ee4\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u6210\u529f\u4e86\uff0c\u8fd9\u6837\u5c31 OK\u4e86\uff0c\u7136\u540e\u6b63\u5e38\u62c9\u5206\u652f\u5199\u4ee3\u7801\u5427\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042399305\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u5199\u5b8c\u4e4b\u540e\uff0c\u6b63\u5e38\u53bb\u6211\u4eec\u7684\u9879\u76ee\u754c\u9762\u63d0\u4ea4 PR\uff0c\u7136\u540e\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042399306\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h2>\u9ebb\u70e6<\/h2>\n<p>\u5f53\u7136\uff0c\u8fc7\u7a0b\u5e76\u6ca1\u6709\u8fd9\u4e48\u987a\u5229\uff0c\u867d\u7136\u8bf4\u53ea\u662f\u5f88\u7b80\u5355\u7684\u4fee\u6539\u3002<\/p>\n<p>\u9996\u5148\uff0c\u8fd9\u4e2a\u6821\u9a8c\u5c31\u7ed9\u6211\u63d0\u793a\u9519\u8bef\u4e86\uff0c\u7b2c\u4e00\u70b9\u53eb\u6211\u4e0d\u8981\u7528 <code>*<\/code>\u53f7\u53bb\u5f15\u7528\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042399307\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u8fd9\u4e2a\u5176\u5b9e\u662f <code>IDEA<\/code>\u7684\u9505\uff0c\u5982\u679c\u5f15\u7528\u540c\u4e00\u4e2a\u5305\u4e0b\u7c7b\u8fc7\u591a\u7684\u8bdd\uff0c\u4f1a\u81ea\u52a8\u5e2e\u6211\u4eec\u8f6c\u6210\u661f\u53f7\uff0c\u8fd9\u4e2a\u6211\u4eec\u53ef\u4ee5\u5728<code>Editor-Code Style-Java<\/code>\uff0c\u7136\u540e\u627e\u5230 Imports \u4e0b\u7684\u8fd9\u4e24\u4e2a\u9009\u9879\uff0c\u628a\u4ed6\u4eec\u90fd\u6539\u6210 99 \u5c31\u53ef\u4ee5\u4e86\uff0c\u9632\u6b62\u4ed6\u81ea\u52a8\u7ed9\u6211\u4eec\u6539\u6210\u661f\u53f7\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042399308\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u7684\u6bd4\u5982 if \u540e\u9762\u6ca1\u8ddf\u7a7a\u683c\u4e4b\u7c7b\u7684\uff0c\u8fd9\u662f\u6211\u5fd8\u8bb0\u683c\u5f0f\u5316\u4e86\uff01<\/p>\n<p>\u7136\u540e\u5927\u4f6c\u56de\u590d\u89c9\u5f97\u770b\u4e0d\u4e0b\u53bb\uff0c\u8fd9\u4ee3\u7801\u592a\u6076\u5fc3\u4e86\uff0c\u8bf4\u6211\u4eec\u662f\u4e0d\u662f\u53ef\u4ee5\u7528<code>java.time.temporal.TemporalAccessor<\/code>\u6765\u5224\u65ad\uff0c\u4e0d\u7136\u8fd9\u4e48\u591a\u65f6\u95f4\u7c7b\u578b\uff0c\u641e\u4e2a\u6bdb\u7ebf\u5462\u3002<\/p>\n<p>\u7136\u540e\u6211\u5c31\u7ffb\u8bd1\u4e86\u4e00\u6bb5\u82f1\u6587\uff0c\u6211\u4e5f\u4e0d\u77e5\u9053\u5927\u4f6c\u770b\u6ca1\u770b\u61c2\uff0c\u6211\u544a\u8bc9\u4ed6\uff0c\u8fd9\u4e2a\u4e0d\u597d\u6574\u554a\uff0c\u4f60\u770b\u8fd9\u4e2a\u63a5\u53e3\u554a\uff0c\u5f88\u591a\u4e71\u4e03\u516b\u7cdf\u7684\u7c7b\u5b9e\u73b0\u4e86\u4ed6\uff0c\u5b9e\u9645\u4e0a\u6211\u89c9\u5f97\u6211\u4eec\u8986\u76d6\u5e38\u7528\u7684\u4e00\u4e9b\u5c31\u884c\u4e86\uff0c\u5176\u4ed6\u7684\u7279\u6b8a\u65f6\u95f4\u7c7b\u578b\u8ba9\u4ed6\u4eec\u81ea\u5df1\u7528 TypeHandler \u5904\u7406\u5427\u3002<\/p>\n<p>\u5927\u4f6c\u8bf4\uff0c\u55ef\u5f53\u7136\uff0c\u6ca1\u529e\u6cd5\u5224\u65ad\u8fd9\u4e2a\u63a5\u53e3\u90a3\u6211\u4eec\u4e5f\u6ca1\u8f99\u4e86\uff0c\u6211\u8bf4\u90a3\u4e0d\u53ef\u5c31\u662f\u561b\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042399309\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u5176\u5b9e\uff0c\u8fd8\u6709\u5f88\u591a\u65f6\u95f4\u7c7b\u578b\u4ed6\u90fd\u4f1a\u62a5\u9519\u7684\uff0c\u6700\u597d\u7684\u529e\u6cd5\u8fd9\u4e2a\u90fd\u62bd\u8c61\u51fa\u6765\u548cMybatis\u5355\u72ec\u7528\u5b9e\u73b0\u7c7b\uff0c\u4e0d\u8fc7\u90a3\u6837\u7684\u8bdd\u5c31\u5f97\u5927\u5de5\u4f5c\u4e86\uff0c\u6211\u592a\u61d2\u4e86\uff0c\u5c31\u8fd9\u6837\u3002<\/p>\n<p>\u3002\u3002\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042399296\">https:\/\/segmentfault.com\/a\/1190000042399296<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8bf4\u6765\u60ed\u6127\uff0c\u5e72\u4e86 10 \u6765\u5e74\u7a0b\u5e8f\u5458\uff0c\u8fd8\u6ca1\u6709\u7ed9\u5f00\u6e90\u505a\u8fc7\u4efb\u4f55\u8d21\u732e\uff0c\u4ee5\u524d\u53ea\u77e5\u9053\u560e\u560e\u5199\uff0c\u51fa\u4e86\u95ee\u9898\u560e\u560e\u6539\uff0c\u4ece\u6765\u6ca1\u60f3\u8fc7\u63d0\u4e2a  [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006355","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006355","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006355"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006355\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}