﻿{"id":161,"date":"2016-04-07T15:00:40","date_gmt":"2016-04-07T07:00:40","guid":{"rendered":"http:\/\/www.chenweikang.top\/?p=161"},"modified":"2016-04-07T15:30:51","modified_gmt":"2016-04-07T07:30:51","slug":"spring4springmvcmybatis%e6%95%b4%e5%90%88%e6%a1%88%e4%be%8b","status":"publish","type":"post","link":"https:\/\/www.chenweikang.top\/?p=161","title":{"rendered":"Spring4+Springmvc+Mybatis\u6574\u5408\u6848\u4f8b"},"content":{"rendered":"<h2>\u6574\u5408\u6b65\u9aa4<\/h2>\n<blockquote><p>1. \u51c6\u5907\u5de5\u4f5c \u5bfc\u5165\u76f8\u5173jar\u5305<\/p>\n<p>2. \u7f16\u5199\u914d\u7f6e\u6587\u4ef6\uff0c\u5728src\u4e0b\u6dfb\u52a0\u4e00\u4e2a \u6e90\u6587\u4ef6\u5939 conf \u5b58\u653e\u914d\u7f6e\u6587\u4ef6<\/p>\n<p>3. \u7f16\u5199\u7c7b\u6d4b\u8bd5<br \/>\n\u5305\u5bf9\u5e94\u7684\u6a21\u5757:<\/p>\n<ul>\n<li>controller---\u63a7\u5236\u5668<\/li>\n<li>service--\u4e1a\u52a1\u903b\u8f91<\/li>\n<li>dao ----\u6570\u636e\u5c42<\/li>\n<li>entities ----\u5b9e\u4f53\u7c7b<\/li>\n<li>mapper -----mybatis\u6620\u5c04\u6587\u4ef6<\/li>\n<\/ul>\n<\/blockquote>\n<h2>\u4e00\u3001\u5bfc\u5165jar\u5305<\/h2>\n<ul>\n<li>aopalliance-1.0.jar<\/li>\n<li>aspectjweaver.jar<\/li>\n<li>com.mysql.jdbc.Driver.jar<\/li>\n<li>commons-fileupload-1.3.1.jar<\/li>\n<li>commons-lang-2.4.jar<\/li>\n<li>commons-logging-1.1.3.jar<\/li>\n<li>commons-pool-1.6.jar<\/li>\n<li>jstl-impl.jar<\/li>\n<li>mybatis-3.3.1.jar<\/li>\n<li>mybatis-spring-1.2.3.jar<\/li>\n<li>spring-aop-4.2.4.RELEASE.jar<\/li>\n<li>spring-aspects-4.2.4.RELEASE.jar<\/li>\n<li>spring-beans-4.2.4.RELEASE.jar<\/li>\n<li>spring-context-4.2.4.RELEASE.jar<\/li>\n<li>spring-core-4.2.4.RELEASE.jar<\/li>\n<li>spring-expression-4.2.4.RELEASE.jar<\/li>\n<li>spring-instrument-4.2.4.RELEASE.jar<\/li>\n<li>spring-instrument-tomcat-4.2.4.RELEASE.jar<\/li>\n<li>spring-jdbc-4.2.4.RELEASE.jar<\/li>\n<li>spring-jms-4.2.4.RELEASE.jar<\/li>\n<li>spring-oxm-4.2.4.RELEASE.jar<\/li>\n<li>spring-test-4.2.4.RELEASE.jar<\/li>\n<li>spring-tx-4.2.4.RELEASE.jar<\/li>\n<li>spring-web-4.2.4.RELEASE.jar<\/li>\n<li>spring-webmvc-4.2.4.RELEASE.jar<\/li>\n<li>spring-webmvc-portlet-4.2.4.RELEASE.jar<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2>\u4e8c\u3001\u00a0\u7f16\u5199\u914d\u7f6e\u6587\u4ef6<\/h2>\n<ul>\n<li>\u7f16\u5199 web.xml<\/li>\n<\/ul>\n<blockquote><p>&lt;!-- \u914d\u7f6espring4 \u52a0\u8f7d\u914d\u7f6e\u6587\u4ef6--&gt;<\/p>\n<p>&lt;context-param&gt;<\/p>\n<p style=\"padding-left: 60px;\">&lt;param-name&gt;contextConfigLocation&lt;\/param-name&gt;<\/p>\n<p style=\"padding-left: 60px;\">&lt;param-value&gt;classpath:applicationContext.xml&lt;\/param-value&gt;<\/p>\n<p>&lt;\/context-param&gt;<br \/>\n&lt;!-- spring\u7684\u76d1\u542c\u5668 --&gt;<\/p>\n<p style=\"padding-left: 60px;\">&lt;listener&gt;<\/p>\n<p style=\"padding-left: 60px;\">&lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;\/listener-class&gt;<\/p>\n<p style=\"padding-left: 30px;\">&lt;\/listener&gt;<\/p>\n<p>&lt;!-- \u914d\u7f6espringmvc --&gt;<br \/>\n&lt;servlet&gt;<\/p>\n<p style=\"padding-left: 30px;\">&lt;servlet-name&gt;springmvc&lt;\/servlet-name&gt;<\/p>\n<p style=\"padding-left: 30px;\">&lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;\/servlet-class&gt;<\/p>\n<p style=\"padding-left: 30px;\">&lt;init-param&gt;<\/p>\n<p style=\"padding-left: 60px;\">&lt;param-name&gt;contextConfigLocation&lt;\/param-name&gt;<\/p>\n<p style=\"padding-left: 60px;\">&lt;param-value&gt;classpath:mvc.xml&lt;\/param-value&gt;<\/p>\n<p style=\"padding-left: 30px;\">&lt;\/init-param&gt;<\/p>\n<p>&lt;\/servlet&gt;<br \/>\n&lt;servlet-mapping&gt;<\/p>\n<p style=\"padding-left: 30px;\">&lt;servlet-name&gt;springmvc&lt;\/servlet-name&gt;<\/p>\n<p style=\"padding-left: 30px;\">&lt;url-pattern&gt;*.do&lt;\/url-pattern&gt;<\/p>\n<p>&lt;\/servlet-mapping&gt;<\/p><\/blockquote>\n<ul>\n<li>\u7f16\u5199\u6570\u636e\u6e90\u914d\u7f6e\u6587\u4ef6 db.properties (\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f)<\/li>\n<\/ul>\n<blockquote><p>\u00a0jdbc.user=root<\/p>\n<p>jdbc.password=199432<\/p>\n<p>jdbc.driverClass=com.mysql.jdbc.Driver<\/p>\n<p>jdbc.jdbcUrl=jdbc\\:mysql\\:\/\/\/cns_db<\/p>\n<p>&nbsp;<\/p><\/blockquote>\n<ul>\n<li>\u7f16\u5199 \u00a0mybatis\u914d\u7f6e\u6587\u4ef6 mybatis.cfg.xml<\/li>\n<\/ul>\n<blockquote><p>&lt;!-- \u914d\u7f6e\u5b9e\u4f53\u5bf9\u8c61\u7684\u5305 --&gt;<br \/>\n&lt;typeAliases&gt;<br \/>\n&lt;package name=\"com.cns.entities\"\/&gt;<br \/>\n&lt;\/typeAliases&gt;<\/p><\/blockquote>\n<ul>\n<li>\u7f16\u5199spring\u914d\u7f6e\u6587\u4ef6 applicationContext.xml<\/li>\n<\/ul>\n<blockquote><p>&lt;!-- \u914d\u7f6e\u6570\u636e\u6e90 --&gt;<br \/>\n&lt;context:property-placeholder location=\"classpath:db.properties\"\/&gt;<br \/>\n&lt;bean id=\"dataSource\"<br \/>\nclass=\"org.springframework.jdbc.datasource.DriverManagerDataSource\"&gt;<br \/>\n&lt;property name=\"username\" value=\"${jdbc.user}\" \/&gt;<br \/>\n&lt;property name=\"password\" value=\"${jdbc.password}\" \/&gt;<br \/>\n&lt;property name=\"url\" value=\"${jdbc.jdbcUrl}\" \/&gt;<br \/>\n&lt;property name=\"driverClassName\" value=\"${jdbc.driverClass}\" \/&gt;<br \/>\n&lt;\/bean&gt;<br \/>\n&lt;!-- \u914d\u7f6eSessionFactory --&gt;<br \/>\n&lt;bean id=\"sqlSessionFactoryBean\" class=\"org.mybatis.spring.SqlSessionFactoryBean\"&gt;<br \/>\n&lt;property name=\"dataSource\" ref=\"dataSource\"\/&gt;<br \/>\n&lt;!-- \u5c06myBatis\u914d\u7f6e\u52a0\u8f7d\u5230sessionFactory --&gt;<br \/>\n&lt;property name=\"mapperLocations\" value=\"classpath:com\/cns\/mapper\/*.xml\" \/&gt;<br \/>\n&lt;property name=\"configLocation\" value=\"classpath:mybatis.cfg.xml\" \/&gt;<br \/>\n&lt;\/bean&gt;<\/p>\n<p>&lt;!-- \u81ea\u52a8\u4e3a\u6240\u6709Dao\u6ce8\u5165 sessionFactory --&gt;<br \/>\n&lt;bean class=\"org.mybatis.spring.mapper.MapperScannerConfigurer\"&gt;<br \/>\n&lt;!-- \u63a5\u53e3\u6240\u5728\u7684\u5305 --&gt;<br \/>\n&lt;!-- \u6ce8\u5165sessionFactory --&gt;<br \/>\n&lt;property name=\"basePackage\" value=\"com.cns.dao\" \/&gt;<br \/>\n&lt;property name=\"sqlSessionFactoryBeanName\" value=\"sqlSessionFactoryBean\" \/&gt;<br \/>\n&lt;\/bean&gt;<\/p>\n<p>&lt;!-- \u914d\u7f6e\u4e8b\u52a1\u7ba1\u7406\u5668 --&gt;<br \/>\n&lt;bean id=\"txManager\"<br \/>\nclass=\"org.springframework.jdbc.datasource.DataSourceTransactionManager\"&gt;<br \/>\n&lt;property name=\"dataSource\" ref=\"dataSource\" \/&gt;<br \/>\n&lt;\/bean&gt;<br \/>\n&lt;tx:advice id=\"txAdvice\" transaction-manager=\"txManager\"&gt;<br \/>\n&lt;tx:attributes&gt;<br \/>\n&lt;tx:method name=\"save\" propagation=\"REQUIRED\"\/&gt;<br \/>\n&lt;tx:method name=\"get\" read-only=\"true\"\/&gt;<br \/>\n&lt;tx:method name=\"*\" propagation=\"REQUIRED\"\/&gt;<br \/>\n&lt;\/tx:attributes&gt;<br \/>\n&lt;\/tx:advice&gt;<\/p>\n<p>&lt;!-- \u914d\u7f6eAOP --&gt;<br \/>\n&lt;aop:config&gt;<br \/>\n&lt;aop:pointcut expression=\"execution (* com.cns.services.impl.*.*(..))\" id=\"pointcut\"\/&gt;<br \/>\n&lt;aop:advisor advice-ref=\"txAdvice\" pointcut-ref=\"pointcut\"\/&gt;<br \/>\n&lt;\/aop:config&gt;<\/p>\n<p>&lt;!-- \u6ce8\u89e3\u626b\u63cf --&gt;<br \/>\n&lt;context:component-scan base-package=\"com.cns.dao\" \/&gt;<br \/>\n&lt;context:component-scan base-package=\"com.cns.service\" \/&gt;<\/p><\/blockquote>\n<ul>\n<li>\u7f16\u5199springmvc\u914d\u7f6e\u6587\u4ef6 mvc.xml<\/li>\n<\/ul>\n<blockquote><p>&lt;!-- \u6ce8\u89e3\u626b\u63cf --&gt;<br \/>\n&lt;context:component-scan base-package=\"com.cns.controller\" \/&gt;<\/p><\/blockquote>\n<h2>\u4e09\u3001\u7f16\u5199\u6d4b\u8bd5\u7c7b<\/h2>\n<ul>\n<li>UserController---\u63a7\u5236\u5668<\/li>\n<\/ul>\n<blockquote><p>&nbsp;<\/p>\n<p>@Controller<br \/>\n@RequestMapping(\"\/user\")<br \/>\npublic class UserController {<\/p>\n<p>@Resource<\/p>\n<p>private UserService userService;<\/p>\n<p style=\"padding-left: 30px;\">@RequestMapping(\"\/list\")<\/p>\n<p style=\"padding-left: 30px;\">public String list(HttpServletRequest request){<\/p>\n<p style=\"padding-left: 60px;\">List&lt;User&gt; list= userService.findAll(); \u00a0\/\/\u67e5\u8be2\u6240\u6709\u7528\u6237<\/p>\n<p style=\"padding-left: 60px;\">System.out.println(\"list...\");<\/p>\n<p style=\"padding-left: 60px;\">request.setAttribute(\"userList\", list); \u00a0\/\/\u5b58\u653e\u5927request<\/p>\n<p style=\"padding-left: 60px;\">return \"success.jsp\"; \/\/\u8fd4\u56de\u9875\u9762\u4e3aseccess.jsp<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<\/blockquote>\n<ul>\n<li>UserService--\u4e1a\u52a1\u903b\u8f91 + UserServiceImpl \u5b9e\u73b0\u63a5\u53e3<\/li>\n<\/ul>\n<blockquote><p>public interface UserService {<\/p>\n<p style=\"padding-left: 30px;\">public List&lt;User&gt; findAll();<\/p>\n<p>}<\/p><\/blockquote>\n<blockquote><p>@Service(\"userService\")<\/p>\n<p>public class UserServiceImpl implements UserService{<\/p>\n<p style=\"padding-left: 30px;\">@Resource<\/p>\n<p style=\"padding-left: 30px;\">private UserDao userDao; \u00a0\/\/\u6ce8\u89e3\u6ce8\u5165dao<\/p>\n<p style=\"padding-left: 30px;\">@Override<\/p>\n<p style=\"padding-left: 30px;\">public List&lt;User&gt; findAll() {<\/p>\n<p style=\"padding-left: 60px;\">return userDao.findAll(); \/\/\u8c03\u7528\u67e5\u8be2\u65b9\u6cd5<\/p>\n<p style=\"padding-left: 30px;\">}<\/p>\n<p>}<\/p><\/blockquote>\n<ul>\n<li>UserDao ----\u6570\u636e\u5c42\uff08\u4f7f\u7528\u63a5\u53e3\uff09<\/li>\n<\/ul>\n<blockquote><p>public interface UserDao {<\/p>\n<p style=\"padding-left: 30px;\">public List&lt;User&gt; findAll();<\/p>\n<p>}<\/p><\/blockquote>\n<ul>\n<li>User ----\u5b9e\u4f53\u7c7b (\u6570\u636e\u8868 test \u00a0\u5b57\u6bb5 id\uff0cusername )<\/li>\n<\/ul>\n<blockquote><p>\/\/\u5b9e\u4f53\u7c7b\u6d4b\u8bd5\u5b57\u6bb5 id username<\/p>\n<p>public class User {<\/p>\n<p style=\"padding-left: 30px;\">private Integer id;<\/p>\n<p style=\"padding-left: 30px;\">private String username;<\/p>\n<p style=\"padding-left: 30px;\">\/\/\u7701\u7565getter\/setter<\/p>\n<p style=\"padding-left: 30px;\">....<\/p>\n<p>}<\/p><\/blockquote>\n<ul>\n<li>UserMapper .xml-----mybatis\u6620\u5c04\u6587\u4ef6<\/li>\n<\/ul>\n<blockquote><p>\/\/\u6620\u5c04\u5bf9\u5e94\u7684dao<\/p>\n<p>&lt;mapper namespace=\"com.cns.dao.UserDao\"&gt;<\/p>\n<p style=\"padding-left: 30px;\">\/\/\u5b9a\u4e49\u7ed3\u679c\u96c6<\/p>\n<p style=\"padding-left: 30px;\">&lt;resultMap type=\"User\" id=\"userResult\"&gt;<\/p>\n<p style=\"padding-left: 60px;\">&lt;id property=\"id\" column=\"id\" \/&gt;<\/p>\n<p style=\"padding-left: 60px;\">&lt;result property=\"username\" column=\"username\" \/&gt;<\/p>\n<p style=\"padding-left: 30px;\">&lt;\/resultMap&gt;<\/p>\n<p style=\"padding-left: 30px;\">\/\/id \u5bf9\u5e94dao\u4e2d\u7684\u65b9\u6cd5 resultMap \u5b9a\u4e49\u7ed3\u679c\u96c6<\/p>\n<p style=\"padding-left: 30px;\">&lt;select id=\"findAll\" resultMap=\"userResult\"&gt;<\/p>\n<p style=\"padding-left: 60px;\">select * from test \/\/\u67e5\u8be2test\u8868\u4e2d\u6240\u6709\u6570\u636e<\/p>\n<p style=\"padding-left: 30px;\">&lt;\/select&gt;<\/p>\n<p>&lt;\/mapper&gt;<\/p><\/blockquote>\n<ul>\n<li>\u8fd4\u56de\u7684success.jsp \u4f7f\u7528jstl\u6807\u7b7e\u6253\u5370\u6570\u636e<\/li>\n<\/ul>\n<blockquote><p>&lt;body&gt;<\/p>\n<p style=\"padding-left: 30px;\">&lt;h2&gt;success.jsp&lt;\/h2&gt;<\/p>\n<p style=\"padding-left: 30px;\">&lt;c:forEach items=\"${userList }\" var=\"user\"&gt;<\/p>\n<p style=\"padding-left: 60px;\">${user.id } --- ${user.username } &lt;br \/&gt;<\/p>\n<p style=\"padding-left: 30px;\">&lt;\/c:forEach&gt;<\/p>\n<p>&lt;\/body&gt;<\/p><\/blockquote>\n<h2>\u6210\u529f\u8fd0\u884c\u7ed3\u679c<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-165\" src=\"http:\/\/www.chenweikang.top\/wp-content\/uploads\/2016\/04\/QQ\u622a\u56fe20160407152431-e1460013924555.png\" alt=\"QQ\u622a\u56fe20160407152431\" width=\"640\" height=\"327\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>\u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1a<a href=\"https:\/\/www.chenweikang.top\">\u5de6\u624b\u4ee3\u7801\u53f3\u624b\u8bd7<\/a> &raquo; <a href=\"https:\/\/www.chenweikang.top\/?p=161\">Spring4+Springmvc+Mybatis\u6574\u5408\u6848\u4f8b<\/a><\/p><div class=\"__youshang\">\r\n            <div id=\"__youshang_popup\" class=\"wechat popup\" style=\"display: none;\">\r\n                <div class=\"head\">~\u8c22\u8c22\u6253\u8d4f~<\/div>\r\n                <div class=\"qrcode\"><div class=\"qrcode-li wechat\" ><img src=\"https:\/\/www.chenweikang.top\/wp-content\/uploads\/2019\/07\/wexin.png\" \/><\/div><div class=\"qrcode-li alipay\" style=\"display:none;\"><img src=\"https:\/\/www.chenweikang.top\/wp-content\/uploads\/2019\/07\/ali-pay.png\" \/><\/div><div class=\"qrcode-li hongbao\" style=\"display:none;\"><img src=\"https:\/\/www.chenweikang.top\/wp-content\/uploads\/2019\/07\/ali-hongbao.png\" \/><\/div><\/div>\r\n                <ul class=\"platform\"><li class=\"icon-wechat active\" data-bg-color=\"#05af4e\" data-thanks=\"~\u8c22\u8c22\u6253\u8d4f~\"><\/li><li class=\"icon-alipay \" data-bg-color=\"#00a2ea\" data-thanks=\"~\u8c22\u8c22\u6253\u8d4f~\"><\/li><li class=\"icon-hongbao \" data-bg-color=\"#dd5746\" data-thanks=\"<p>\u626b\u7801\u9886\u7ea2\u5305<\/p><p style='margin-top: 24px;'>\uff08\u4f59\u989d\u5b9d\u652f\u4ed8\u65f6\u53ef\u62b5\u73b0\uff09<\/p>\"><\/li><\/ul>\r\n            <\/div>\r\n            <a href=\"javascript:void(0);\" id=\"__youshang_btn\">\u8d4f<\/a>\r\n        <\/div>","protected":false},"excerpt":{"rendered":"<p>\u6574\u5408\u6b65\u9aa4 1. \u51c6\u5907\u5de5\u4f5c \u5bfc\u5165\u76f8\u5173jar\u5305 2. \u7f16\u5199\u914d\u7f6e\u6587\u4ef6\uff0c\u5728src\u4e0b\u6dfb\u52a0\u4e00\u4e2a \u6e90\u6587\u4ef6\u5939 conf \u5b58\u653e\u914d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8,3],"tags":[32,35],"class_list":["post-161","post","type-post","status-publish","format-standard","hentry","category-java","category-note","tag-mybatis","tag-ssm"],"_links":{"self":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/posts\/161","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=161"}],"version-history":[{"count":0,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/posts\/161\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}