首页 > 代码库 > Maven+SpringMVC+Mybatis整合入门Demo

Maven+SpringMVC+Mybatis整合入门Demo

1 数据库准备

(1)建立一个名为mytest的数据库

(2)创建表

 

[plain] view plain copy
 
 技术分享技术分享
  1. CREATE TABLE `t_user` (   
  2. `USER_ID` int(11) NOT NULL AUTO_INCREMENT,   
  3. `USER_NAME` char(30) NOT NULL,   
  4. `USER_PASSWORD` char(10) NOT NULL,   
  5. PRIMARY KEY (`USER_ID`),   
  6. KEY `IDX_NAME` (`USER_NAME`)   
  7. )ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8  

 

 

(3)插入数据

[plain] view plain copy
 
 技术分享技术分享
  1. INSERT INTO t_user (USER_ID, USER_NAME,USER_PASSWORD) VALUES (1, ‘Zhang San‘, ‘654321‘);  
  2. INSERT INTO t_user (USER_ID, USER_NAME,USER_PASSWORD) VALUES (2, ‘Li Si‘, ‘123456‘);  

 

2 参考http://blog.csdn.net/haishu_zheng/article/details/51490299,用第二种方法创建一个名为maven_springmvc_mybatis_demo的工程。

 

3 文件目录结构如下图所示

技术分享

技术分享

 

4 pom.xml的完整内容为

 

[html] view plain copy
 
 技术分享技术分享
  1. <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>maven_springmvc_mybatis_demo</groupId>  
  5.     <artifactId>maven_springmvc_mybatis_demo</artifactId>  
  6.     <version>0.0.1-SNAPSHOT</version>  
  7.     <packaging>war</packaging>  
  8.     <name>maven_springmvc_mybatis_demo</name>  
  9.     <description/>  
  10.    
  11.     <properties>  
  12.         <!-- spring版本号 -->  
  13.         <spring.version>3.2.9.RELEASE</spring.version>  
  14.         <!-- junit版本号 -->  
  15.         <junit.version>4.10</junit.version>  
  16.         <!-- mybatis版本号 -->  
  17.         <mybatis.version>3.2.8</mybatis.version>  
  18.     </properties>  
  19.    
  20.     <dependencies>  
  21.         <!-- 添加Spring依赖 -->  
  22.         <dependency>  
  23.            <groupId>org.springframework</groupId>  
  24.            <artifactId>spring-core</artifactId>  
  25.            <version>${spring.version}</version>  
  26.         </dependency>  
  27.         <dependency>  
  28.            <groupId>org.springframework</groupId>  
  29.            <artifactId>spring-webmvc</artifactId>  
  30.            <version>${spring.version}</version>  
  31.         </dependency>  
  32.         <dependency>  
  33.            <groupId>org.springframework</groupId>  
  34.            <artifactId>spring-context</artifactId>  
  35.            <version>${spring.version}</version>  
  36.         </dependency>  
  37.         <dependency>  
  38.            <groupId>org.springframework</groupId>  
  39.            <artifactId>spring-context-support</artifactId>  
  40.            <version>${spring.version}</version>  
  41.         </dependency>  
  42.         <dependency>  
  43.            <groupId>org.springframework</groupId>  
  44.            <artifactId>spring-aop</artifactId>  
  45.            <version>${spring.version}</version>  
  46.         </dependency>  
  47.         <dependency>  
  48.            <groupId>org.springframework</groupId>  
  49.            <artifactId>spring-aspects</artifactId>  
  50.            <version>${spring.version}</version>  
  51.         </dependency>  
  52.         <dependency>  
  53.            <groupId>org.springframework</groupId>  
  54.            <artifactId>spring-tx</artifactId>  
  55.            <version>${spring.version}</version>  
  56.         </dependency>  
  57.         <dependency>  
  58.            <groupId>org.springframework</groupId>  
  59.            <artifactId>spring-jdbc</artifactId>  
  60.            <version>${spring.version}</version>  
  61.         </dependency>  
  62.         <dependency>  
  63.            <groupId>org.springframework</groupId>  
  64.            <artifactId>spring-web</artifactId>  
  65.            <version>${spring.version}</version>  
  66.         </dependency>  
  67.    
  68.         <!--单元测试依赖 -->  
  69.         <dependency>  
  70.            <groupId>junit</groupId>  
  71.            <artifactId>junit</artifactId>  
  72.            <version>${junit.version}</version>  
  73.            <scope>test</scope>  
  74.         </dependency>  
  75.    
  76.         <!--spring单元测试依赖 -->  
  77.         <dependency>  
  78.            <groupId>org.springframework</groupId>  
  79.            <artifactId>spring-test</artifactId>  
  80.            <version>${spring.version}</version>  
  81.            <scope>test</scope>  
  82.         </dependency>  
  83.    
  84.         <!--mybatis依赖 -->  
  85.         <dependency>  
  86.            <groupId>org.mybatis</groupId>  
  87.            <artifactId>mybatis</artifactId>  
  88.            <version>${mybatis.version}</version>  
  89.         </dependency>  
  90.    
  91.         <!-- mybatis/spring包 -->  
  92.         <dependency>  
  93.            <groupId>org.mybatis</groupId>  
  94.            <artifactId>mybatis-spring</artifactId>  
  95.            <version>1.2.2</version>  
  96.         </dependency>  
  97.    
  98.         <!-- mysql驱动包 -->  
  99.         <dependency>  
  100.            <groupId>mysql</groupId>  
  101.            <artifactId>mysql-connector-java</artifactId>  
  102.            <version>5.1.34</version>  
  103.         </dependency>  
  104.    
  105.         <!-- javaee-api包 注意和项目使用的JDK版本对应 -->  
  106.         <dependency>  
  107.            <groupId>javax</groupId>  
  108.            <artifactId>javaee-api</artifactId>  
  109.            <version>7.0</version>  
  110.            <scope>provided</scope>  
  111.         </dependency>  
  112.    
  113.         <!-- javaee-web-api包 注意和项目使用的JDK版本对应 -->  
  114.         <dependency>  
  115.            <groupId>javax</groupId>  
  116.            <artifactId>javaee-web-api</artifactId>  
  117.            <version>7.0</version>  
  118.            <scope>provided</scope>  
  119.         </dependency>  
  120.    
  121.     </dependencies>  
  122.    
  123. </project>  

 

 

5 web.xml中的完整内容为

 

[html] view plain copy
 
 技术分享技术分享
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">  
  3.   <display-name>maven_springmvc_mybatis_demo</display-name>  
  4.    
  5.     <!-- 读取spring配置文件 -->  
  6.     <context-param>  
  7.         <param-name>contextConfigLocation</param-name>  
  8.         <param-value>classpath:application.xml</param-value>  
  9.     </context-param>  
  10.    
  11.     <listener>  
  12.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  13.     </listener>  
  14.    
  15.     <!-- springMVC核心配置 -->  
  16.     <servlet>  
  17.         <servlet-name>dispatcherServlet</servlet-name>  
  18.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  19.         <init-param>  
  20.             <param-name>contextConfigLocation</param-name>  
  21.             <!--spingMVC的配置路径  -->  
  22.             <param-value>classpath:springmvc/spring-mvc.xml</param-value>  
  23.         </init-param>  
  24.         <load-on-startup>1</load-on-startup>  
  25.     </servlet>  
  26.     <!-- 拦截设置 -->  
  27.     <servlet-mapping>  
  28.         <servlet-name>dispatcherServlet</servlet-name>  
  29.         <url-pattern>/</url-pattern>  
  30.     </servlet-mapping>  
  31. </web-app>  

 

 

6 User.Java的完整代码为

 

[java] view plain copy
 
 技术分享技术分享
  1. package com.zheng.model;  
  2.   
  3. public class User {  
  4.     private Integer userId;    
  5.     private String userName;    
  6.     private String userPassword;    
  7.   
  8.     public Integer getUserId() {    
  9.         return userId;    
  10.     }    
  11.     
  12.     public void setUserId(Integer userId) {    
  13.         this.userId = userId;    
  14.     }    
  15.     
  16.     public String getUserName() {    
  17.         return userName;    
  18.     }    
  19.     
  20.     public void setUserName(String userName) {    
  21.         this.userName = userName;    
  22.     }    
  23.     
  24.     public String getUserPassword() {    
  25.         return userPassword;    
  26.     }    
  27.     
  28.     public void setUserPassword(String userPassword) {    
  29.         this.userPassword = userPassword;    
  30.     }   
  31. }  

 

 

7 UserDao.java的完整代码为

 

[java] view plain copy
 
 技术分享技术分享
  1. package com.zheng.dao;  
  2.   
  3. import com.zheng.model.User;  
  4.   
  5. public interface UserDao {  
  6.     public User selectUserById(Integer userId);    
  7. }  

 

 

8 UserService.java的完整代码为

 

[java] view plain copy
 
 技术分享技术分享
  1. package com.zheng.service;  
  2.   
  3. import com.zheng.model.User;  
  4.   
  5. public interface UserService {  
  6.     User selectUserById(Integer userId);    
  7. }  

 

 

9 UserServiceImpl.java的完整代码为

 

[java] view plain copy
 
 技术分享技术分享
  1. package com.zheng.service;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.stereotype.Service;  
  5.   
  6. import com.zheng.dao.UserDao;  
  7. import com.zheng.model.User;  
  8. import com.zheng.service.UserService;  
  9.   
  10. @Service  
  11. public class UserServiceImpl implements UserService {  
  12.     @Autowired    
  13.     private UserDao userDao;    
  14.     
  15.     public User selectUserById(Integer userId) {    
  16.         return userDao.selectUserById(userId);    
  17.             
  18.     }   
  19. }  

 

 

10 UserController.java的完整代码为

 

[java] view plain copy
 
 技术分享技术分享
  1. package com.zheng.controller;  
  2.   
  3. import javax.annotation.Resource;  
  4.   
  5. import org.springframework.stereotype.Controller;  
  6. import org.springframework.web.bind.annotation.RequestMapping;  
  7. import org.springframework.web.servlet.ModelAndView;  
  8.   
  9. import com.zheng.model.User;  
  10. import com.zheng.service.UserService;  
  11.   
  12. @Controller  
  13. public class UserController {  
  14.       
  15.     @Resource    
  16.     private UserService userService;    
  17.         
  18.     @RequestMapping("/showUserInfo")      
  19.     public ModelAndView getIndex(){        
  20.         ModelAndView mav = new ModelAndView("index");     
  21.         User user = userService.selectUserById(1);    
  22.         mav.addObject("user", user);     
  23.         return mav;      
  24.     }   
  25. }  

 

 

11 User2Mapper.xml的完整代码为

 

[html] view plain copy
 
 技术分享技术分享
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"    
  3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  4. <mapper namespace="com.zheng.dao.UserDao">  
  5. <!--设置model类和数据库中表的字段一一对应,注意数据库字段和model类中的字段名称不致,此处一定要!-->  
  6.     <resultMap id="BaseResultMap" type="com.zheng.model.User">  
  7.         <id column="USER_ID" property="userId" jdbcType="INTEGER" />  
  8.         <result column="USER_NAME" property="userName" jdbcType="CHAR" />  
  9.         <result column="USER_PASSWORD" property="userPassword" jdbcType="CHAR" />  
  10.     </resultMap>  
  11.     <!-- 查询单条记录 -->  
  12.     <select id="selectUserById" parameterType="int" resultMap="BaseResultMap">  
  13.         SELECT * FROM t_user WHERE USER_ID = #{userId}  
  14.     </select>  
  15. </mapper>  

 

 

12 mybatis-config.xml的完整代码为

 

[html] view plain copy
 
 技术分享技术分享
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"    
  3. "http://mybatis.org/dtd/mybatis-3-config.dtd">    
  4. <configuration>      
  5. </configuration>  

 

 

13 jdbc.properties的完整代码为

 

[html] view plain copy
 
 技术分享技术分享
  1. jdbc_driverClassName=com.mysql.jdbc.Driver  
  2. jdbc_url=jdbc:mysql://localhost:3306/mytest  
  3. jdbc_username=root  
  4. jdbc_password=root  

 

 

14 spring-mvc.xml的完整代码为

 

[html] view plain copy
 
 技术分享技术分享
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.   xmlns:p="http://www.springframework.org/schema/p"  
  4.   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.   xmlns:context="http://www.springframework.org/schema/context"  
  6.   xmlns:mvc="http://www.springframework.org/schema/mvc"  
  7.   xsi:schemaLocation="  
  8.     http://www.springframework.org/schema/beans  
  9.     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
  10.     http://www.springframework.org/schema/context  
  11.     http://www.springframework.org/schema/context/spring-context-3.2.xsd  
  12.     http://www.springframework.org/schema/mvc  
  13.     http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">  
  14.        
  15.      <mvc:annotation-driven />   
  16.      <!-- 扫描controller(controller层注入) -->  
  17.      <context:component-scan base-package="com.zheng.controller"/>    
  18.        
  19.      <!-- 对模型视图添加前后缀 -->  
  20.      <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"  
  21.       p:prefix="/WEB-INF/view/" p:suffix=".jsp"/>  
  22. </beans>  

 

 

 

15 application.xml的完整代码为

 

[html] view plain copy
 
 技术分享技术分享
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xsi:schemaLocation="    
  6.            http://www.springframework.org/schema/beans    
  7.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
  8.            http://www.springframework.org/schema/aop    
  9.            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
  10.            http://www.springframework.org/schema/context    
  11.            http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  12.        
  13.      <!-- 引入jdbc配置文件 -->    
  14.      <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  15.         <property name="locations">  
  16.             <list>  
  17.                <value>classpath:properties/*.properties</value>  
  18.                 <!--要是有多个配置文件,只需在这里继续添加即可 -->  
  19.             </list>  
  20.         </property>  
  21.     </bean>  
  22.       
  23.     <!-- 配置数据源 -->  
  24.     <bean id="dataSource"  
  25.         class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  26.         <!-- 不使用properties来配置 -->  
  27.         <!-- <property name="driverClassName" value=http://www.mamicode.com/"com.mysql.jdbc.Driver" />   
  28.             <property name="url" value=http://www.mamicode.com/"jdbc:mysql://localhost:3306/learning" />   
  29.             <property name="username" value=http://www.mamicode.com/"root" />   
  30.             <property name="password" value=http://www.mamicode.com/"christmas258@" /> -->  
  31.        <!-- 使用properties来配置 -->  
  32.         <property name="driverClassName">  
  33.             <value>${jdbc_driverClassName}</value>  
  34.         </property>  
  35.         <property name="url">  
  36.             <value>${jdbc_url}</value>  
  37.         </property>  
  38.         <property name="username">  
  39.             <value>${jdbc_username}</value>  
  40.         </property>  
  41.         <property name="password">  
  42.             <value>${jdbc_password}</value>  
  43.         </property>  
  44.     </bean>  
  45.   
  46.     <!-- 自动扫描了所有的XxxxMapper.xml对应的mapper接口文件,这样就不用一个一个手动配置Mpper的映射了,只要Mapper接口类和Mapper映射文件对应起来就可以了。 -->  
  47.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  48.         <property name="basePackage"  
  49.             value=http://www.mamicode.com/"com.zheng.dao" />  
  50.     </bean>  
  51.   
  52.     <!-- 配置Mybatis的文件 ,mapperLocations配置**Mapper.xml文件位置,configLocation配置mybatis-config文件位置-->  
  53.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  54.         <property name="dataSource" ref="dataSource" />  
  55.         <property name="mapperLocations" value=http://www.mamicode.com/"classpath:mapper/*.xml"/>    
  56.         <property name="configLocation" value=http://www.mamicode.com/"classpath:mybatis/mybatis-config.xml" />  
  57.         <!-- <property name="typeAliasesPackage" value=http://www.mamicode.com/"com.tiantian.ckeditor.model"   
  58.             /> -->  
  59.     </bean>  
  60.   
  61.     <!-- 自动扫描注解的bean -->  
  62.     <context:component-scan base-package="com.zheng.service" />  
  63.   
  64. </beans>  

 

 

 

16 index.jsp中的完整代码为:

 

[java] view plain copy
 
 技术分享技术分享
  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
  2. <html>  
  3. <body>  
  4. <h2>User Info</h2>  
  5. 用户名: ${user.userName}<br>  
  6.  密码: ${user.userPassword}<br>  
  7. </body>  
  8. </html>  

 

 

 

17 将项目添加进Tomcat 7中并运行

技术分享

 

在浏览器中输入

http://localhost:8080/maven_springmvc_mybatis_demo/showUserInfo

显示结果为

技术分享

 

18 源码下载地址

CSDN:http://download.csdn.net/detail/haishu_zheng/9534859

Github:https://github.com/zhenghaishu/maven_springmvc_mybatis_demo

 

Maven+SpringMVC+Mybatis整合入门Demo