Maven+SpringMVC+Mybatis整合入门Demo

时间:2023-12-18 21:48:26

数据库准备

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

(2)创建表

  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)插入数据

  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 文件目录结构如下图所示

Maven+SpringMVC+Mybatis整合入门Demo

Maven+SpringMVC+Mybatis整合入门Demo

4 pom.xml的完整内容为

  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. <properties>
  11. <!-- spring版本号 -->
  12. <spring.version>3.2.9.RELEASE</spring.version>
  13. <!-- junit版本号 -->
  14. <junit.version>4.10</junit.version>
  15. <!-- mybatis版本号 -->
  16. <mybatis.version>3.2.8</mybatis.version>
  17. </properties>
  18. <dependencies>
  19. <!-- 添加Spring依赖 -->
  20. <dependency>
  21. <groupId>org.springframework</groupId>
  22. <artifactId>spring-core</artifactId>
  23. <version>${spring.version}</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework</groupId>
  27. <artifactId>spring-webmvc</artifactId>
  28. <version>${spring.version}</version>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework</groupId>
  32. <artifactId>spring-context</artifactId>
  33. <version>${spring.version}</version>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework</groupId>
  37. <artifactId>spring-context-support</artifactId>
  38. <version>${spring.version}</version>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.springframework</groupId>
  42. <artifactId>spring-aop</artifactId>
  43. <version>${spring.version}</version>
  44. </dependency>
  45. <dependency>
  46. <groupId>org.springframework</groupId>
  47. <artifactId>spring-aspects</artifactId>
  48. <version>${spring.version}</version>
  49. </dependency>
  50. <dependency>
  51. <groupId>org.springframework</groupId>
  52. <artifactId>spring-tx</artifactId>
  53. <version>${spring.version}</version>
  54. </dependency>
  55. <dependency>
  56. <groupId>org.springframework</groupId>
  57. <artifactId>spring-jdbc</artifactId>
  58. <version>${spring.version}</version>
  59. </dependency>
  60. <dependency>
  61. <groupId>org.springframework</groupId>
  62. <artifactId>spring-web</artifactId>
  63. <version>${spring.version}</version>
  64. </dependency>
  65. <!--单元测试依赖 -->
  66. <dependency>
  67. <groupId>junit</groupId>
  68. <artifactId>junit</artifactId>
  69. <version>${junit.version}</version>
  70. <scope>test</scope>
  71. </dependency>
  72. <!--spring单元测试依赖 -->
  73. <dependency>
  74. <groupId>org.springframework</groupId>
  75. <artifactId>spring-test</artifactId>
  76. <version>${spring.version}</version>
  77. <scope>test</scope>
  78. </dependency>
  79. <!--mybatis依赖 -->
  80. <dependency>
  81. <groupId>org.mybatis</groupId>
  82. <artifactId>mybatis</artifactId>
  83. <version>${mybatis.version}</version>
  84. </dependency>
  85. <!-- mybatis/spring包 -->
  86. <dependency>
  87. <groupId>org.mybatis</groupId>
  88. <artifactId>mybatis-spring</artifactId>
  89. <version>1.2.2</version>
  90. </dependency>
  91. <!-- mysql驱动包 -->
  92. <dependency>
  93. <groupId>mysql</groupId>
  94. <artifactId>mysql-connector-java</artifactId>
  95. <version>5.1.34</version>
  96. </dependency>
  97. <!-- javaee-api包 注意和项目使用的JDK版本对应 -->
  98. <dependency>
  99. <groupId>javax</groupId>
  100. <artifactId>javaee-api</artifactId>
  101. <version>7.0</version>
  102. <scope>provided</scope>
  103. </dependency>
  104. <!-- javaee-web-api包 注意和项目使用的JDK版本对应 -->
  105. <dependency>
  106. <groupId>javax</groupId>
  107. <artifactId>javaee-web-api</artifactId>
  108. <version>7.0</version>
  109. <scope>provided</scope>
  110. </dependency>
  111. </dependencies>
  112. </project>

5 web.xml中的完整内容为

  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. <!-- 读取spring配置文件 -->
  5. <context-param>
  6. <param-name>contextConfigLocation</param-name>
  7. <param-value>classpath:application.xml</param-value>
  8. </context-param>
  9. <listener>
  10. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  11. </listener>
  12. <!-- springMVC核心配置 -->
  13. <servlet>
  14. <servlet-name>dispatcherServlet</servlet-name>
  15. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  16. <init-param>
  17. <param-name>contextConfigLocation</param-name>
  18. <!--spingMVC的配置路径  -->
  19. <param-value>classpath:springmvc/spring-mvc.xml</param-value>
  20. </init-param>
  21. <load-on-startup>1</load-on-startup>
  22. </servlet>
  23. <!-- 拦截设置 -->
  24. <servlet-mapping>
  25. <servlet-name>dispatcherServlet</servlet-name>
  26. <url-pattern>/</url-pattern>
  27. </servlet-mapping>
  28. </web-app>

6 User.Java的完整代码为

  1. package com.zheng.model;
  2. public class User {
  3. private Integer userId;
  4. private String userName;
  5. private String userPassword;
  6. public Integer getUserId() {
  7. return userId;
  8. }
  9. public void setUserId(Integer userId) {
  10. this.userId = userId;
  11. }
  12. public String getUserName() {
  13. return userName;
  14. }
  15. public void setUserName(String userName) {
  16. this.userName = userName;
  17. }
  18. public String getUserPassword() {
  19. return userPassword;
  20. }
  21. public void setUserPassword(String userPassword) {
  22. this.userPassword = userPassword;
  23. }
  24. }

7 UserDao.java的完整代码为

  1. package com.zheng.dao;
  2. import com.zheng.model.User;
  3. public interface UserDao {
  4. public User selectUserById(Integer userId);
  5. }

8 UserService.java的完整代码为

  1. package com.zheng.service;
  2. import com.zheng.model.User;
  3. public interface UserService {
  4. User selectUserById(Integer userId);
  5. }

9 UserServiceImpl.java的完整代码为

  1. package com.zheng.service;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Service;
  4. import com.zheng.dao.UserDao;
  5. import com.zheng.model.User;
  6. import com.zheng.service.UserService;
  7. @Service
  8. public class UserServiceImpl implements UserService {
  9. @Autowired
  10. private UserDao userDao;
  11. public User selectUserById(Integer userId) {
  12. return userDao.selectUserById(userId);
  13. }
  14. }

10 UserController.java的完整代码为

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

11 User2Mapper.xml的完整代码为

  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的完整代码为

  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的完整代码为

  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的完整代码为

  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. <mvc:annotation-driven />
  15. <!-- 扫描controller(controller层注入) -->
  16. <context:component-scan base-package="com.zheng.controller"/>
  17. <!-- 对模型视图添加前后缀 -->
  18. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
  19. p:prefix="/WEB-INF/view/" p:suffix=".jsp"/>
  20. </beans>

15 application.xml的完整代码为

  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. <!-- 引入jdbc配置文件 -->
  13. <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  14. <property name="locations">
  15. <list>
  16. <value>classpath:properties/*.properties</value>
  17. <!--要是有多个配置文件,只需在这里继续添加即可 -->
  18. </list>
  19. </property>
  20. </bean>
  21. <!-- 配置数据源 -->
  22. <bean id="dataSource"
  23. class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  24. <!-- 不使用properties来配置 -->
  25. <!-- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  26. <property name="url" value="jdbc:mysql://localhost:3306/learning" />
  27. <property name="username" value="root" />
  28. <property name="password" value="christmas258@" /> -->
  29. <!-- 使用properties来配置 -->
  30. <property name="driverClassName">
  31. <value>${jdbc_driverClassName}</value>
  32. </property>
  33. <property name="url">
  34. <value>${jdbc_url}</value>
  35. </property>
  36. <property name="username">
  37. <value>${jdbc_username}</value>
  38. </property>
  39. <property name="password">
  40. <value>${jdbc_password}</value>
  41. </property>
  42. </bean>
  43. <!-- 自动扫描了所有的XxxxMapper.xml对应的mapper接口文件,这样就不用一个一个手动配置Mpper的映射了,只要Mapper接口类和Mapper映射文件对应起来就可以了。 -->
  44. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  45. <property name="basePackage"
  46. value="com.zheng.dao" />
  47. </bean>
  48. <!-- 配置Mybatis的文件 ,mapperLocations配置**Mapper.xml文件位置,configLocation配置mybatis-config文件位置-->
  49. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  50. <property name="dataSource" ref="dataSource" />
  51. <property name="mapperLocations" value="classpath:mapper/*.xml"/>
  52. <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
  53. <!-- <property name="typeAliasesPackage" value="com.tiantian.ckeditor.model"
  54. /> -->
  55. </bean>
  56. <!-- 自动扫描注解的bean -->
  57. <context:component-scan base-package="com.zheng.service" />
  58. </beans>

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

  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中并运行

Maven+SpringMVC+Mybatis整合入门Demo

在浏览器中输入

http://localhost:8080/maven_springmvc_mybatis_demo/showUserInfo

显示结果为

Maven+SpringMVC+Mybatis整合入门Demo

18 源码下载地址

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

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