{"id":6850,"date":"2024-11-14T09:43:53","date_gmt":"2024-11-14T01:43:53","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6850\/"},"modified":"2024-11-14T09:43:53","modified_gmt":"2024-11-14T01:43:53","slug":"springmvc-%e8%bf%9e%e6%8e%a5-mysql-%e6%97%b6%ef%bc%8c%e5%a6%82%e4%bd%95%e8%be%93%e5%87%ba%e5%b8%b8%e8%a7%81%e9%94%99%e8%af%af%e4%bf%a1%e6%81%af%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6850\/","title":{"rendered":"SpringMVC \u8fde\u63a5 MySQL \u65f6\uff0c\u5982\u4f55\u8f93\u51fa\u5e38\u89c1\u9519\u8bef\u4fe1\u606f\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/172999513655694.jpg\" class=\"aligncenter\" title=\"SpringMVC \u8fde\u63a5 MySQL \u65f6\uff0c\u5982\u4f55\u8f93\u51fa\u5e38\u89c1\u9519\u8bef\u4fe1\u606f\uff1f\u63d2\u56fe\" alt=\"SpringMVC \u8fde\u63a5 MySQL \u65f6\uff0c\u5982\u4f55\u8f93\u51fa\u5e38\u89c1\u9519\u8bef\u4fe1\u606f\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>springmvc \u8fde\u63a5  \u4e2d\u7684\u5e38\u89c1\u9519\u8bef<\/strong><\/p>\n<p>\u5728 springmvc \u8fde\u63a5 mysql \u65f6\uff0c\u7528\u6237\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u5c06 mysql \u9519\u8bef\u5730\u5199\u6210\u4e86 mysq\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c \u53ef\u80fd\u4e00\u76f4\u505c\u7559\u5728\u542f\u52a8\u9636\u6bb5\uff0c\u800c\u4e0d\u4f1a\u663e\u793a\u4efb\u4f55\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5982\u4f55\u8f93\u51fa\u9519\u8bef\u4fe1\u606f<\/strong><\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u8fde\u63a5\u8d85\u65f6\u65f6\u95f4\u6765\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\u3002\u5177\u4f53\u505a\u6cd5\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>\u5728\u6570\u636e\u6e90\u914d\u7f6e\u4e2d\u8bbe\u7f6e validationquery \u9009\u9879\uff0c\u8868\u793a\u7528\u4e8e\u6d4b\u8bd5\u8fde\u63a5\u7684 sql \u67e5\u8be2\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<pre>&lt;bean id=\"dataSource\" class=\"org.springframework.jdbc.datasource.DriverManagerDataSource\"&gt;\n    &lt;property name=\"driverClassName\" value=\"com.mysql.jdbc.Driver\" \/&gt;\n    &lt;property name=\"url\" value=\"jdbc:mysql:\/\/localhost:3306\/test\" \/&gt;\n    &lt;property name=\"username\" value=\"root\" \/&gt;\n    &lt;property name=\"password\" value=\"password\" \/&gt;\n    &lt;!-- \u8bbe\u7f6e\u8fde\u63a5\u8d85\u65f6\u65f6\u95f4 --&gt;\n    &lt;property name=\"testOnBorrow\" value=\"true\" \/&gt;\n    &lt;property name=\"validationQuery\" value=\"SELECT 1\" \/&gt;\n    &lt;property name=\"timeBetweenEvictionRunsMillis\" value=\"5000\" \/&gt;\n&lt;\/bean&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li>\u8bbe\u7f6e testonborrow \u4e3a true\uff0c\u8868\u793a\u5728\u6bcf\u6b21\u501f\u7528\u8fde\u63a5\u65f6\u90fd\u8fdb\u884c\u9a8c\u8bc1\u3002<\/li>\n<li>\u8bbe\u7f6e validationquery \u4e3a select 1\uff0c\u8868\u793a\u4f7f\u7528 sql \u67e5\u8be2\u6765\u6d4b\u8bd5\u8fde\u63a5\u3002<\/li>\n<li>\u8bbe\u7f6e timebetweenevictionrunsmillis \u4e3a 5000 \u6beb\u79d2\uff0c\u8868\u793a\u6bcf 5 \u79d2\u68c0\u67e5\u4e00\u6b21\u8fde\u63a5\u7684\u6709\u6548\u6027\u3002<\/li>\n<\/ul>\n<p>\u5728\u4e0a\u8ff0\u914d\u7f6e\u4e0b\uff0c\u5982\u679c\u5728 5 \u79d2\u5185\u65e0\u6cd5\u83b7\u53d6\u5230\u8fde\u63a5\uff0c\u5c06\u4f1a\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\uff0c\u4f8b\u5982\u6570\u636e\u5e93\u4e0d\u5b58\u5728\u3002<\/p>\n<p><strong>\u5176\u4ed6\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u9664\u4e86\u8bbe\u7f6e\u8fde\u63a5\u8d85\u65f6\u65f6\u95f4\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528 init-method \u6216 @postconstruct \u6ce8\u89e3\uff1a\u5728 spring bean \u521d\u59cb\u5316\u65f6\u6267\u884c\u4e00\u6bb5\u4ee3\u7801\uff0c\u7528\u4e8e\u6d4b\u8bd5\u8fde\u63a5\u3002<\/li>\n<li>\u5b9e\u73b0 initializingbean \u63a5\u53e3\uff1a\u5728 spring bean \u521d\u59cb\u5316\u65f6\u8c03\u7528 afterpropertiesset \u65b9\u6cd5\uff0c\u7528\u4e8e\u6d4b\u8bd5\u8fde\u63a5\u3002<\/li>\n<li>\u4f7f\u7528\u963f\u91cc\u5df4\u5df4\u7684 druid \u6570\u636e\u6e90\uff1adruid \u6570\u636e\u6e90\u5728\u542f\u52a8\u65f6\u4f1a\u81ea\u52a8\u6d4b\u8bd5\u8fde\u63a5\u7684\u6709\u6548\u6027\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fSpringMVC \u8fde\u63a5 MySQL \u65f6\uff0c\u5982\u4f55\u8f93\u51fa\u5e38\u89c1\u9519\u8bef\u4fe1\u606f\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>springmvc \u8fde\u63a5 \u4e2d\u7684\u5e38\u89c1\u9519\u8bef \u5728 springmvc \u8fde\u63a5 mysql \u65f6\uff0c\u7528\u6237\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u5c06 mysql \u9519\u8bef\u5730\u5199\u6210\u4e86 mysq\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c \u53ef\u80fd\u4e00\u76f4\u505c\u7559\u5728\u542f\u52a8\u9636\u6bb5\uff0c\u800c\u4e0d\u4f1a\u663e\u793a\u4efb\u4f55\u9519\u8bef\u4fe1\u606f\u3002 \u5982\u4f55\u8f93\u51fa\u9519\u8bef\u4fe1\u606f \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u8fde\u63a5\u8d85\u65f6\u65f6\u95f4\u6765\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\u3002\u5177\u4f53\u505a\u6cd5\u5982\u4e0b\uff1a \u5728\u6570\u636e\u6e90\u914d\u7f6e\u4e2d\u8bbe\u7f6e validationquery \u9009\u9879\uff0c\u8868\u793a\u7528\u4e8e\u6d4b\u8bd5\u8fde\u63a5\u7684 sql \u67e5\u8be2\u3002\u4f8b\u5982\uff1a &lt;bean id=&#8221;dataSource&#8221; class=&#8221;org.springframework.jdbc.datasource.DriverManagerDataSource&#8221;&gt; &lt;property name=&#8221;driverClassName&#8221; value=&#8221;com.mysql.jdbc.Driver&#8221; \/&gt; &lt;property name=&#8221;url&#8221; value=&#8221;jdbc:mysql:\/\/localhost:3306\/test&#8221; \/&gt; &lt;property name=&#8221;username&#8221; value=&#8221;root&#8221; \/&gt; &lt;property name=&#8221;password&#8221; value=&#8221;password&#8221; \/&gt; &lt;!&#8211; \u8bbe\u7f6e\u8fde\u63a5\u8d85\u65f6\u65f6\u95f4 &#8211;&gt; &lt;property name=&#8221;testOnBorrow&#8221; value=&#8221;true&#8221; \/&gt; &lt;property name=&#8221;validationQuery&#8221; value=&#8221;SELECT 1&#8243; \/&gt; &lt;property name=&#8221;timeBetweenEvictionRunsMillis&#8221; value=&#8221;5000&#8243; \/&gt; &lt;\/bean&gt; [&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-6850","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6850","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=6850"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6850\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}