1 jar包

包括:spring(包括springmvc)、mybatis、mybatis-spring整合包、数据库驱动、第三方连接池。

参考:“mybatis与springmvc整合全部jar包”目录

2 整合思路

Dao层:

1、SqlMapConfig.xml,空文件即可。需要文件头。

2、applicationContext-dao.xml。

a) 数据库连接池

b) SqlSessionFactory对象,需要spring和mybatis整合包下的。

c) 配置mapper文件扫描器。

Service层:

1、applicationContext-service.xml包扫描器,扫描@service注解的类。

2、applicationContext-trans.xml配置事务。

表现层:

Springmvc.xml

1、包扫描器,扫描@Controller注解的类。

2、配置注解驱动。

3、视图解析器

Web.xml

配置前端控制器。

2.1 sqlMapConfig.xml

在classpath下创建mybatis/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.2 applicationContext-dao.xml

配置数据源、配置SqlSessionFactory、mapper扫描器。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
  8. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  9. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
  10. <!-- 加载配置文件 -->
  11. <context:property-placeholder location="classpath:jdbc.properties" />
  12. <!-- 数据库连接池 -->
  13. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  14. destroy-method="close">
  15. <property name="driverClassName" value="${jdbc.driver}" />
  16. <property name="url" value="${jdbc.url}" />
  17. <property name="username" value="${jdbc.username}" />
  18. <property name="password" value="${jdbc.password}" />
  19. <property name="maxActive" value="10" />
  20. <property name="maxIdle" value="5" />
  21. </bean>
  22. <!-- mapper配置 -->
  23. <!-- spring管理sqlsessionfactory 使用mybatisspring整合包中的 -->
  24. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  25. <!-- 数据库连接池 -->
  26. <property name="dataSource" ref="dataSource" />
  27. <!-- 加载mybatis的全局配置文件 -->
  28. <property name="configLocation" value="classpath:SqlMapConfig.xml" />
  29. </bean>
  30. <!-- 配置Mapper扫描器 -->
  31. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  32. <property name="basePackage" value="com.carven.dao"/>
  33. </bean>
  34. </beans>

jdbc.properties

  1. jdbc.driver=com.mysql.jdbc.Driver
  2. jdbc.url=jdbc:mysql://localhost:3306/springmvc?characterEncoding=utf-8
  3. jdbc.username=root
  4. jdbc.password=123456

2.3 applicationContext-service.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
  8. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  9. http://www.springframework.org/schema/tx
  10. http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  11. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
  12. <!-- @Service扫描 -->
  13. <context:component-scan base-package="com.carven.service"></context:component-scan>
  14. </beans>

2.4 applicationContext-transaction.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
  8. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  9. http://www.springframework.org/schema/tx
  10. http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  11. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
  12. <!-- 事务管理器 -->
  13. <bean id="transactionManager"
  14. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  15. <!-- 数据源 -->
  16. <property name="dataSource" ref="dataSource" />
  17. </bean>
  18. <!-- 通知 -->
  19. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  20. <tx:attributes>
  21. <!-- 传播行为 -->
  22. <tx:method name="save*" propagation="REQUIRED" />
  23. <tx:method name="insert*" propagation="REQUIRED" />
  24. <tx:method name="delete*" propagation="REQUIRED" />
  25. <tx:method name="update*" propagation="REQUIRED" />
  26. <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
  27. <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
  28. </tx:attributes>
  29. </tx:advice>
  30. <!-- 切面 -->
  31. <aop:config>
  32. <aop:advisor advice-ref="txAdvice"
  33. pointcut="execution(* com.carven.service.*.*(..))" />
  34. </aop:config>
  35. </beans>

2.5 springmvc.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:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  7. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
  8. http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
  9. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
  10. <!-- @Controller注解扫描 -->
  11. <context:component-scan base-package="com.carven.controller"/>
  12. <!-- 注解驱动:
  13. 替我们显示的配置了最新版的注解的处理器映射器和处理器适配器 -->
  14. <mvc:annotation-driven/>
  15. <!-- 配置视图解析器
  16. 作用:在controller中指定页面路径的时候就不用写页面的完整路径名称了,可以直接写页面去掉扩展名的名称
  17. -->
  18. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  19. <!-- 真正的页面路径 = 前缀 + 去掉后缀名的页面名称 + 后缀 -->
  20. <!-- 前缀 -->
  21. <property name="prefix" value="/WEB-INF/jsp/"></property>
  22. <!-- 后缀 -->
  23. <property name="suffix" value=".jsp"></property>
  24. </bean>
  25. </beans>

2.6 web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  5. id="WebApp_ID" version="2.5">
  6. <display-name>ssm</display-name>
  7. <welcome-file-list>
  8. <welcome-file>index.html</welcome-file>
  9. <welcome-file>index.htm</welcome-file>
  10. <welcome-file>index.jsp</welcome-file>
  11. <welcome-file>default.html</welcome-file>
  12. <welcome-file>default.htm</welcome-file>
  13. <welcome-file>default.jsp</welcome-file>
  14. </welcome-file-list>
  15. <!-- 加载spring容器 -->
  16. <context-param>
  17. <param-name>contextConfigLocation</param-name>
  18. <param-value>classpath:ApplicationContext-*.xml</param-value>
  19. </context-param>
  20. <listener>
  21. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  22. </listener>
  23. <!-- SpringMvc前端控制器 -->
  24. <servlet>
  25. <servlet-name>springmvc</servlet-name>
  26. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  27. <init-param>
  28. <param-name>contextConfigLocation</param-name>
  29. <param-value>classpath:SpringMvc.xml</param-value>
  30. </init-param>
  31. <!-- tomcat启动的时候就加载这个servlet -->
  32. <load-on-startup>1</load-on-startup>
  33. </servlet>
  34. <servlet-mapping>
  35. <servlet-name>springmvc</servlet-name>
  36. <url-pattern>*.action</url-pattern>
  37. </servlet-mapping>
  38. <!-- 配置Post请求乱码 -->
  39. <filter>
  40. <filter-name>CharacterEncodingFilter</filter-name>
  41. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  42. <init-param>
  43. <param-name>encoding</param-name>
  44. <param-value>utf-8</param-value>
  45. </init-param>
  46. </filter>
  47. <filter-mapping>
  48. <filter-name>CharacterEncodingFilter</filter-name>
  49. <url-pattern>/*</url-pattern>
  50. </filter-mapping>
  51. </web-app>

3 Dao:mybatis逆向工程

4 Service

1、Service由spring管理

2、spring对Service进行事务控制

1 ItemService接口(例子)

  1. public interface ItemService {
  2. List<Items> getItemsList();
  3. }

2 ItemServiceImpl实现类

  1. @Service
  2. public class ItemServiceImpl implements ItemService {
  3. @Autowired
  4. private ItemMapper itemMapper;
  5. @Override
  6. public List<Items> getItemsList() {
  7. List<Items> itemList = itemMapper.getItemList();
  8. return itemList;
  9. }
  10. }

5 Controller

  1. @Controller
  2. public class ItemController {
  3. @Autowired
  4. private ItemService itemService;
  5. @RequestMapping("/itemList")
  6. public ModelAndView getItemList() {
  7. List<Items>list = itemService.getItemsList();
  8. ModelAndView modelAndView = new ModelAndView();
  9. modelAndView.addObject("itemList", list);
  10. modelAndView.setViewName("itemList");
  11. returnmodelAndView;
  12. }
  13. }