{"id":25623,"date":"2024-11-24T15:49:11","date_gmt":"2024-11-24T07:49:11","guid":{"rendered":"https:\/\/fwq.ai\/blog\/25623\/"},"modified":"2024-11-24T15:49:11","modified_gmt":"2024-11-24T07:49:11","slug":"mybatis-%e6%9f%a5%e8%af%a2-int-%e7%b1%bb%e5%9e%8b%e6%95%b0%e6%8d%ae%e8%bf%94%e5%9b%9e-null-%e6%97%b6%e5%a6%82%e4%bd%95%e5%a4%84%e7%90%86%ef%bc%9f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/25623\/","title":{"rendered":"MyBatis \u67e5\u8be2 int \u7c7b\u578b\u6570\u636e\u8fd4\u56de null \u65f6\u5982\u4f55\u5904\u7406\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173064115397576.jpg\" class=\"aligncenter\" title=\"MyBatis \u67e5\u8be2 int \u7c7b\u578b\u6570\u636e\u8fd4\u56de null \u65f6\u5982\u4f55\u5904\u7406\uff1f\u63d2\u56fe\" alt=\"MyBatis \u67e5\u8be2 int \u7c7b\u578b\u6570\u636e\u8fd4\u56de null \u65f6\u5982\u4f55\u5904\u7406\uff1f\u63d2\u56fe\" \/><\/p>\n<p>\u5f53\u4f7f\u7528 mybatis \u67e5\u8be2 sql \u671f\u671b\u7740\u8fd4\u56de int \u7c7b\u578b\u7684\u6570\u636e\u65f6\uff0c\u5374\u5b9e\u9645\u8fd4\u56de null\uff0c\u4f1a\u5f15\u53d1\u5f02\u5e38\u3002\u8fd9\u662f\u56e0\u4e3a mybatis \u4e0d\u5141\u8bb8\u539f\u59cb\u7c7b\u578b\u7684\u65b9\u6cd5\u8fd4\u56de null \u503c\u3002\u9488\u5bf9\u6b64\u95ee\u9898\uff0c\u6709\u4e24\u79cd\u5e38\u89c1\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<p><strong>1. \u4fee\u6539\u8fd4\u56de\u7c7b\u578b<\/strong><\/p>\n<p>\u4fee\u6539 @select \u6ce8\u89e3\u4e2d\u7684\u65b9\u6cd5\u8fd4\u56de\u7c7b\u578b\u4e3a integer \u4ee3\u66ff int\u3002integer \u662f int \u7684\u5305\u88c5\u7c7b\u578b\uff0c\u5b83\u53ef\u4ee5\u5904\u7406 null \u503c\u3002\u4fee\u6539\u540e\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>@select(\"select max(id) from user\")\ninteger maxid();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e1a\u52a1\u5c42\u5224\u65ad\u8fd4\u56de\u503c\u662f\u5426\u4e3a null\uff0c\u5982\u679c\u662f\uff0c\u5219\u8fd4\u56de 0 \u6216\u5176\u4ed6\u9884\u671f\u7684\u9ed8\u8ba4\u503c\u3002<\/p>\n<p><strong>2. \u4f7f\u7528\u6570\u636e\u5e93\u51fd\u6570<\/strong><\/p>\n<p>\u5728 sql \u4e2d\u4f7f\u7528\u6570\u636e\u5e93\u51fd\u6570\uff08\u5982  \u7684 coalesce \u6216 ifnull\uff09\u6765\u5904\u7406 null \u503c\u3002\u4fee\u6539\u540e\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>@Select(\"SELECT COALESCE(max(id), 0) FROM user\")\nint maxId();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>coalesce \u51fd\u6570\u5c06\u8fd4\u56de\u7b2c\u4e00\u4e2a\u975e null \u503c\uff0c\u5982\u679c\u6ca1\u6709\u975e null \u503c\uff0c\u5219\u8fd4\u56de\u6307\u5b9a\u7684\u9ed8\u8ba4\u503c\uff08\u672c\u4f8b\u4e2d\u4e3a 0\uff09\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fMyBatis \u67e5\u8be2 int \u7c7b\u578b\u6570\u636e\u8fd4\u56de null \u65f6\u5982\u4f55\u5904\u7406\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u4f7f\u7528 mybatis \u67e5\u8be2 sql \u671f\u671b\u7740\u8fd4\u56de int \u7c7b\u578b\u7684\u6570\u636e\u65f6\uff0c\u5374\u5b9e\u9645\u8fd4\u56de null\uff0c\u4f1a\u5f15\u53d1\u5f02\u5e38\u3002\u8fd9\u662f\u56e0\u4e3a mybatis \u4e0d\u5141\u8bb8\u539f\u59cb\u7c7b\u578b\u7684\u65b9\u6cd5\u8fd4\u56de null \u503c\u3002\u9488\u5bf9\u6b64\u95ee\u9898\uff0c\u6709\u4e24\u79cd\u5e38\u89c1\u89e3\u51b3\u65b9\u6848\uff1a 1. \u4fee\u6539\u8fd4\u56de\u7c7b\u578b \u4fee\u6539 @select \u6ce8\u89e3\u4e2d\u7684\u65b9\u6cd5\u8fd4\u56de\u7c7b\u578b\u4e3a integer \u4ee3\u66ff int\u3002integer \u662f int \u7684\u5305\u88c5\u7c7b\u578b\uff0c\u5b83\u53ef\u4ee5\u5904\u7406 null \u503c\u3002\u4fee\u6539\u540e\u7684\u4ee3\u7801\u5982\u4e0b\uff1a @select(&#8220;select max(id) from user&#8221;) integer maxid(); \u767b\u5f55\u540e\u590d\u5236 \u5728\u4e1a\u52a1\u5c42\u5224\u65ad\u8fd4\u56de\u503c\u662f\u5426\u4e3a null\uff0c\u5982\u679c\u662f\uff0c\u5219\u8fd4\u56de 0 \u6216\u5176\u4ed6\u9884\u671f\u7684\u9ed8\u8ba4\u503c\u3002 2. \u4f7f\u7528\u6570\u636e\u5e93\u51fd\u6570 \u5728 sql \u4e2d\u4f7f\u7528\u6570\u636e\u5e93\u51fd\u6570\uff08\u5982 \u7684 coalesce \u6216 ifnull\uff09\u6765\u5904\u7406 null \u503c\u3002\u4fee\u6539\u540e\u7684\u4ee3\u7801\u5982\u4e0b\uff1a @Select(&#8220;SELECT COALESCE(max(id), 0) FROM user&#8221;) int maxId(); \u767b\u5f55\u540e\u590d\u5236 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-25623","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25623","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=25623"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25623\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=25623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=25623"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=25623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}