{"id":200893,"date":"2025-05-06T10:37:17","date_gmt":"2025-05-06T02:37:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200893\/"},"modified":"2025-05-06T10:37:17","modified_gmt":"2025-05-06T02:37:17","slug":"mybatis-%e6%b5%8b%e8%af%95%e7%b1%bb%e4%b8%ad%e8%b0%83%e7%94%a8%e6%8e%a5%e5%8f%a3%e6%96%b9%e6%b3%95%e6%8a%a5%e9%94%99%ef%bc%9a%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3non-static-method-list-c","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200893\/","title":{"rendered":"Mybatis \u6d4b\u8bd5\u7c7b\u4e2d\u8c03\u7528\u63a5\u53e3\u65b9\u6cd5\u62a5\u9519\uff1a\u5982\u4f55\u89e3\u51b3\u201cNon-static method &#8216;list()&#8217; cannot be referenced from a static context\u201d\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Mybatis \u6d4b\u8bd5\u7c7b\u4e2d\u8c03\u7528\u63a5\u53e3\u65b9\u6cd5\u62a5\u9519\uff1a\u5982\u4f55\u89e3\u51b3\u201cNon-static method &#8216;list()&#8217; cannot be referenced from a static context\u201d\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300aMybatis \u6d4b\u8bd5\u7c7b\u4e2d\u8c03\u7528\u63a5\u53e3\u65b9\u6cd5\u62a5\u9519\uff1a\u5982\u4f55\u89e3\u51b3\u201cNon-static method &#8216;list()&#8217; cannot be referenced from a static context\u201d\uff1f\u300b\uff0c\u8986\u76d6\u4e86\u6570\u636e\u5e93\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241119\/1731999294673c363eaeb72.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5728 idea \u7684 mybatis \u6d4b\u8bd5\u7c7b\u4e2d\u8c03\u7528\u63a5\u53e3\u65b9\u6cd5\u65f6\u62a5\u9519<\/strong><\/p>\n<p>\u5728\u7f16\u5199 mybatis \u6d4b\u8bd5\u7c7b\u65f6\uff0c\u8c03\u7528\u7528\u6237\u6620\u5c04\u5668\u63a5\u53e3 usermapper \u4e2d\u7684 list \u65b9\u6cd5\u5374\u9047\u5230\u4e86\u62a5\u9519\uff1a&#8221;non-static method &#8216;list()&#8217; cannot be referenced from a static context&#8221;\u3002<\/p>\n<p><strong>\u539f\u56e0\u5206\u6790<\/strong><\/p>\n<p>\u51fa\u73b0\u6b64\u62a5\u9519\u7684\u539f\u56e0\u662f\uff0c\u5728\u6d4b\u8bd5\u7c7b\u4e2d\uff0c\u7f16\u8bd1\u5668\u65e0\u6cd5\u8bc6\u522b\u63a5\u53e3\u53d8\u91cf usermapper\u3002\u5728 java \u4e2d\uff0c\u63a5\u53e3\u53d8\u91cf\u5fc5\u987b\u5148\u88ab\u5b9e\u4f8b\u5316\u624d\u80fd\u4f7f\u7528\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6cd5<\/strong><\/p>\n<p>\u8981\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u9700\u8981\u5728\u6d4b\u8bd5\u7c7b\u4e2d\u5b9e\u4f8b\u5316 usermapper \u63a5\u53e3\u3002\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\uff1a<\/p>\n<ol>\n<li>\n<p>\u5728\u6d4b\u8bd5\u7c7b\u4e2d\u521b\u5efa usermapper \u63a5\u53e3\u7684\u5b9e\u4f8b\uff1a<\/p>\n<pre>private usermapper usermapper; \/\/ \u58f0\u660e\u4e00\u4e2a usermapper \u5b9e\u4f8b\u53d8\u91cf<\/pre>\n<\/li>\n<li>\n<p>\u5728\u6d4b\u8bd5\u65b9\u6cd5\u4e2d\u6ce8\u5165 usermapper \u5b9e\u4f8b\uff1a<\/p>\n<pre>@autowired\npublic void setusermapper(usermapper usermapper) {\n this.usermapper = usermapper;\n}<\/pre>\n<\/li>\n<li>\n<p>\u5728\u6d4b\u8bd5\u65b9\u6cd5\u4e2d\u8c03\u7528 list \u65b9\u6cd5\uff1a<\/p>\n<pre>List&lt;User&gt; users = userMapper.list(); \/\/ \u8c03\u7528 list \u65b9\u6cd5<\/pre>\n<\/li>\n<\/ol>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\u5b9e\u4f8b\u5316 usermapper \u63a5\u53e3\uff0c\u7f16\u8bd1\u5668\u5c31\u53ef\u4ee5\u8bc6\u522b\u63a5\u53e3\u53d8\u91cf\uff0c\u4ece\u800c\u89e3\u51b3\u62a5\u9519\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aMybatis \u6d4b\u8bd5\u7c7b\u4e2d\u8c03\u7528\u63a5\u53e3\u65b9\u6cd5\u62a5\u9519\uff1a\u5982\u4f55\u89e3\u51b3\u201cNon-static method &#8216;list()&#8217; cannot be referenced from a static context\u201d\uff1f\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6570\u636e\u5e93\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>Mybatis \u6d4b\u8bd5\u7c7b\u4e2d\u8c03\u7528\u63a5\u53e3&#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-200893","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200893","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=200893"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200893\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}