[关闭]
@MrXiao 2018-03-05T08:48:53.000000Z 字数 11350 阅读 646

Spring MVC(2)——整合spring-mybatis

SpringMVC


1、整合思路

dao层:

mybatis+spring
目标:
使用spring对sqlSessionFactory进行管理
使用spring和mybatis整合包中的mapper扫描器对mapper接口进行扫描生成代理对象。

service层:

spring
目标:
让spring管理service类,将mapper代理对象注入到service对象中。
spring要对service方法执行进行事务控制

controller层:

springmvc+spring
目标:
使用注解处理器映射器和适配器进行开发Handler
Handler使用组件扫描方式在spring容器中进行注册

2、工程搭建

2.1 创建maven web工程

2.2 设置jar包依赖(pom设置)

  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.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.topvision.maven</groupId>
  5. <artifactId>ssm</artifactId>
  6. <packaging>war</packaging>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <name>ssm Maven Webapp</name>
  9. <url>http://maven.apache.org</url>
  10. <!-- 属性 -->
  11. <properties>
  12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  13. <spring.version>4.3.12.RELEASE</spring.version>
  14. <mybatis.version>3.4.5</mybatis.version>
  15. </properties>
  16. <!-- 锁定版本 -->
  17. <dependencyManagement>
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework</groupId>
  21. <artifactId>spring-context</artifactId>
  22. <version>${spring.version}</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework</groupId>
  26. <artifactId>spring-aspects</artifactId>
  27. <version>${spring.version}</version>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework</groupId>
  31. <artifactId>spring-orm</artifactId>
  32. <version>${spring.version}</version>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework</groupId>
  36. <artifactId>spring-test</artifactId>
  37. <version>${spring.version}</version>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.springframework</groupId>
  41. <artifactId>spring-web</artifactId>
  42. <version>${spring.version}</version>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.springframework</groupId>
  46. <artifactId>spring-webmvc</artifactId>
  47. <version>${spring.version}</version>
  48. </dependency>
  49. <dependency>
  50. <groupId>org.mybatis</groupId>
  51. <artifactId>mybatis</artifactId>
  52. <version>${mybatis.version}</version>
  53. </dependency>
  54. </dependencies>
  55. </dependencyManagement>
  56. <dependencies>
  57. <!-- Spring -->
  58. <dependency>
  59. <groupId>org.springframework</groupId>
  60. <artifactId>spring-test</artifactId>
  61. </dependency>
  62. <dependency>
  63. <groupId>org.springframework</groupId>
  64. <artifactId>spring-context</artifactId>
  65. </dependency>
  66. <dependency>
  67. <groupId>org.springframework</groupId>
  68. <artifactId>spring-aspects</artifactId>
  69. </dependency>
  70. <dependency>
  71. <groupId>org.springframework</groupId>
  72. <artifactId>spring-orm</artifactId>
  73. </dependency>
  74. <dependency>
  75. <groupId>org.springframework</groupId>
  76. <artifactId>spring-web</artifactId>
  77. </dependency>
  78. <dependency>
  79. <groupId>org.springframework</groupId>
  80. <artifactId>spring-webmvc</artifactId>
  81. </dependency>
  82. <!-- mybatis -->
  83. <dependency>
  84. <groupId>org.mybatis</groupId>
  85. <artifactId>mybatis</artifactId>
  86. </dependency>
  87. <dependency>
  88. <groupId>org.mybatis</groupId>
  89. <artifactId>mybatis-spring</artifactId>
  90. <version>1.3.1</version>
  91. </dependency>
  92. <!-- mysql驱动 -->
  93. <dependency>
  94. <groupId>mysql</groupId>
  95. <artifactId>mysql-connector-java</artifactId>
  96. <version>5.1.6</version>
  97. <scope>runtime</scope>
  98. </dependency>
  99. <!-- c3p0 -->
  100. <dependency>
  101. <groupId>c3p0</groupId>
  102. <artifactId>c3p0</artifactId>
  103. <version>0.9.1.2</version>
  104. </dependency>
  105. <!-- slf4j -->
  106. <dependency>
  107. <groupId>org.slf4j</groupId>
  108. <artifactId>slf4j-log4j12</artifactId>
  109. <version>1.7.2</version>
  110. </dependency>
  111. <!-- junit -->
  112. <dependency>
  113. <groupId>junit</groupId>
  114. <artifactId>junit</artifactId>
  115. <version>4.12</version>
  116. <scope>test</scope>
  117. </dependency>
  118. <dependency>
  119. <groupId>org.hamcrest</groupId>
  120. <artifactId>hamcrest-library</artifactId>
  121. <version>1.3</version>
  122. </dependency>
  123. <!-- jstl -->
  124. <dependency>
  125. <groupId>javax.servlet</groupId>
  126. <artifactId>jstl</artifactId>
  127. <version>1.2</version>
  128. </dependency>
  129. <!-- servlet jsp -->
  130. <dependency>
  131. <groupId>javax.servlet</groupId>
  132. <artifactId>servlet-api</artifactId>
  133. <version>2.5</version>
  134. <scope>provided</scope>
  135. </dependency>
  136. <dependency>
  137. <groupId>javax.servlet</groupId>
  138. <artifactId>jsp-api</artifactId>
  139. <version>2.0</version>
  140. <scope>provided</scope>
  141. </dependency>
  142. </dependencies>
  143. <build>
  144. <plugins>
  145. <plugin>
  146. <groupId>org.apache.maven.plugins</groupId>
  147. <artifactId>maven-compiler-plugin</artifactId>
  148. <configuration>
  149. <source>1.8</source>
  150. <target>1.8</target>
  151. <encoding>UTF-8</encoding>
  152. </configuration>
  153. </plugin>
  154. </plugins>
  155. <finalName>ssm</finalName>
  156. </build>
  157. </project>

2.3 配置service

加入spring。

2.3.1 web.xml

配置spring监听器,设置spring核心文件的位置。

  1. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  3. http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  4. version="3.1" metadata-complete="true">
  5. <!-- applicationContext对象仅加载一次,在服务器器启动时加载 -->
  6. <listener>
  7. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  8. </listener>
  9. <context-param>
  10. <param-name>contextConfigLocation</param-name>
  11. <param-value>classpath:applicationContext.xml</param-value>
  12. </context-param>
  13. </web-app>

2.3.2 spring核心文件

依据上一步的设置,在classpath下新建applicationContext.xml

设置spring约束的文件头

  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:aop="http://www.springframework.org/schema/aop"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
  7. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
  9. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
  10. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
  11. </beans>
  1. 开启注解功能,并设置扫描路径

    1. <!-- 1、开启注解、自动扫描 -->
    2. <context:annotation-config />
    3. <context:component-scan base-package="com.topvision.ssm">
    4. <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    5. </context:component-scan>


    注意:
    在配置扫描路径时,要避免对Controller注解的扫描,目的是将该注解所标识的bean交给springmvc管理而不是spring管理。

  2. 加载属性文件,如数据库配置文件

    1. <!-- 2、加载properties -->
    2. <!-- <context:property-placeholder location="classpath:jdbc.properties"/> -->
    3. <bean id="propertyConfigurer"
    4. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    5. <property name="locations">
    6. <list>
    7. <value>classpath:jdbc.properties</value>
    8. </list>
    9. </property>
    10. </bean>

    依上在classpath下新建jdbc.properties文件。

    1. s2sm.jdbc.driverClass = com.mysql.jdbc.Driver
    2. s2sm.jdbc.url = jdbc:mysql://localhost:3306/springdb
    3. s2sm.jdbc.username = root
    4. s2sm.jdbc.password = 123
  3. 设置数据源

    1. <!--3、 数据库C3P0 -->
    2. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    3. <property name="driverClass" value="${s2sm.jdbc.driverClass}" />
    4. <property name="jdbcUrl" value="${s2sm.jdbc.url}" />
    5. <property name="user" value="${s2sm.jdbc.username}" />
    6. <property name="password" value="${s2sm.jdbc.password}" />
    7. <property name="initialPoolSize" value="${s2sm.cpool.initialPoolSize}" />
    8. <property name="minPoolSize" value="${s2sm.cpool.minPoolSize}" />
    9. <property name="maxPoolSize" value="${s2sm.cpool.maxPoolSize}" />
    10. <property name="acquireIncrement" value="${s2sm.cpool.acquireIncrement}" />
    11. <property name="maxIdleTime" value="${s2sm.cpool.maxIdleTime}" />
    12. <property name="acquireRetryAttempts" value="${s2sm.cpool.acquireRetryAttempts}" />
    13. <property name="acquireRetryDelay" value="${s2sm.cpool.acquireRetryDelay}" />
    14. </bean>
  4. 设置SqlSessionFactory

    1. <!-- 4、配置SqlSessionFactory对象 -->
    2. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    3. <!-- 注入数据库连接池 -->
    4. <property name="dataSource" ref="dataSource" />
    5. <!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
    6. <property name="configLocation" value="classpath:sqlMapConfig.xml" />
    7. <!-- 扫描entity包 使用别名 -->
    8. <property name="typeAliasesPackage" value="com.topvision.ssm.domain" />
    9. <!-- 扫描sql配置文件:mapper需要的xml文件 -->
    10. <!-- <property name="mapperLocations" value="classpath:mapper/*.xml" /> -->
    11. <property name="mapperLocations"
    12. value="classpath*:com/topvision/ssm/dao/mapper/*.xml" />
    13. </bean>
  5. 扫描Dao层接口

    1. <!-- 5、配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
    2. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    3. <!-- 注入sqlSessionFactory -->
    4. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    5. <!-- 给出需要扫描Dao接口包 -->
    6. <property name="basePackage" value="com.topvision.ssm.dao" />
    7. </bean>
  6. Spring事务

    1. <!-- 5、事务,开启注解事务 -->
    2. <tx:annotation-driven transaction-manager="transactionManager" />
    3. <!-- 事务管理器 -->
    4. <bean id="transactionManager"
    5. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    6. <property name="dataSource" ref="dataSource"></property>
    7. </bean>

    2.4 配置Dao

    根据上面SqlSessionFactory的设置,在classpath下新建SqlMapConfig.xml。

    1. <?xml version="1.0" encoding="UTF-8" ?>
    2. <!DOCTYPE configuration
    3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
    5. <configuration>
    6. </configuration>

2.5 整合Controller

2.5.1 配置springmvc前端控制器

  1. <servlet>
  2. <servlet-name>springmvc</servlet-name>
  3. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  4. <!--设置springmvc核心文件路径-->
  5. <init-param>
  6. <param-name>contextConfigLocation</param-name>
  7. <param-value>classpath:spring-mvc.xml</param-value>
  8. </init-param>
  9. </servlet>
  10. <servlet-mapping>
  11. <servlet-name>springmvc</servlet-name>
  12. <!--
  13. *.action,*.do等,以其结尾的url交由springmvc管理
  14. / 所有请求交由springmvc,但静态资源(css,js等)交由springmvc是不对的
  15. /* 此种配置不正确,当请求为jsp页面时,springmvc无法解析
  16. -->
  17. <url-pattern>*.do</url-pattern>
  18. </servlet-mapping>

2.5.2 springmvc核心文件

  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:mvc="http://www.springframework.org/schema/mvc"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/mvc
  9. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  14. http://www.springframework.org/schema/tx
  15. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
  16. <!-- 可以扫描标记有@controller、@service、@repository、@component的bean -->
  17. <context:component-scan base-package="com.topvision.ssm.controller">
  18. <context:include-filter type="annotation"
  19. expression="org.springframework.stereotype.Controller" />
  20. </context:component-scan>
  21. <!-- 注解的处理器映射器和适配器 -->
  22. <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
  23. <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"
  24. /> -->
  25. <!-- 简写 -->
  26. <mvc:annotation-driven />
  27. <!-- ViewResolver -->
  28. <bean
  29. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  30. <property name="prefix" value="/WEB-INF/jsp/" />
  31. <property name="suffix" value=".jsp" />
  32. </bean>
  33. <!--静态资源默认servlet配置 (1)加入对静态资源的处理:js,gif,png (2)允许使用"/"做整体映射 -->
  34. <!-- <mvc:default-servlet-handler/> -->
  35. </beans>

2.5.3 开发Controller

  1. <!--该注解标识handler-->
  2. @Controller
  3. public class Controller1 {
  4. @Autowired
  5. private BookService bookService;
  6. <!--该注解标识请求路径-->
  7. @RequestMapping("/queryBook")
  8. public ModelAndView getBookById() {
  9. Book book = bookService.findById(1000l);
  10. ModelAndView modelAndView = new ModelAndView();
  11. modelAndView.addObject("book", book);
  12. modelAndView.setViewName("bookDetail");
  13. return modelAndView;
  14. }
  15. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注