·整合所需jar包
(1)Spring(包括SpringMVC)jar包
(2)Mybatis所需jar包
(3)mybatis-spring整合包
(4)数据库驱动包
(5) 第三方数据库连接池
一共需要31个jar包,如下:
这些jar包可以在我上传的资源中下载:https://download.csdn.net/download/alicewang99/10511337
这里用的是dbcp数据库连接池,当然也可以用c3p0等其他连接池,归归类后jar包就很有条理。
整合思路:
可以看出,spring在进行管理时是很有条理的。然后不同的层可以调用其它层。Handler调用service,service调用mapper等。根据这个架构,我们来总结下整合的思路。根据这个调用关系,我们可以从上往下一步步整合。
·Dao层
1. SqlMapConfig.xml,空文件即可(虽然文件为空,但必不可少);
2. applicationContext-dao.xml
a) 数据库连接池
b) SQLSessionFactory对象,需要Spring和MyBatis的整合包
c) 配置文件扫描器
·Service层
1. applicationContext-service.xml文件中配制包扫描器,扫描带@Service注解的类;
2. applicationContent-trans.xml文件配置事务。
·表现层
1. 包扫描器,扫描带@Controller注解的类
2. 配置注解驱动
3. 配置视图解析器
·web.xml
在web.xml文件中配置前端控制器
现在思路清晰了,接下来就开始整合了。在整合前先看下项目的目录结构:
·整合Dao层
整合Dao层也就是整合持久层,那么需要spring的核心包、持久层包、mybatis包,数据库以及连接池的包。
MyBatis全局配置文件:在classpath下创建mybatis/sqlMapConfig.xml这样的MyBatis全局配置文件,如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
可以看出,整合的时候,这个全局配置文件已经很清爽了,因为数据源、mapper都交给Spring去管理了。
配置Spring配置文件:配置完MyBatis的全局配置文件后,接下来就要配置Spring的配置文件了。Spring的配置文件我将分类写在不同文件中,都放在classpath下的spring文件夹下,这里是对Dao的整合,所以起名applicationContext-dao.xml.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
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
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean>
<!-- mapper配置 -->
<!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 加载mybatis的全局配置文件,虽然这个全局配置文件是空的,但是这个全局配置文件是必不可少的 -->
<property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />
</bean>
<!-- 配置Mapper扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.blackboard.syncData.mapper"/>
</bean>
</beans>
注意:一定记得加载mybatis的全局配置文件,虽然这个全局配置文件是空的,但是这个全局配置文件是必不可少的。
可以看出,整合Dao层的时候主要配置了一下数据源、sqlSessionFactory和mapper扫描器, 这样的话,数据源,sqlSessionFactory和mapper在tomcat启动时就被Spring实例化到了容器中。
这儿db.properties该属性配置文件的内容为(MySQL的,其他数据库可参考我以前的总结):
jdbc.driver=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
jdbc.username=test
jdbc.password=123
整合Service层
先把jar包导了再说,整合Service层需要配置事务。
配置applicationContext-service.xml
这里是第二个Spring的配置文件了,还是在config/spring文件夹下面,主要是用来配置所有的service的,如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
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
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd">
<!-- 配置包扫描器,扫描带@Service注解的类 -->
<context:component-scan base-package="com.blackboard.syncData.service"></context:component-scan>
<!-- 扫描定时任务 -->
<context:component-scan base-package="com.blackboard.syncData.task" />
<mvc:annotation-driven />
<task:annotation-driven />
</beans>
这儿只须配置包扫描器,专门扫描带@Service注解的类。
配置applicationContext-trans.xml
这里是第三个Spring的配置文件了,还是在config/spring文件夹下面,主要是用来配置Spring事务管理的,如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
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
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 事务管理器,用的是Spring JDBC的事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 传播行为 -->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 切面 -->
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.blackboard.syncData.service.*.*(..))" />
</aop:config>
</beans>
整合表现层
上面提到过,SpringMVC是Spring的一个模块,所以不需要整合,我们只需要加入SpringMVC所需要的jar包即可。
配置处理器映射器、处理器适配器和视图解析器
这里使用注解的方式配置,因为注解的方式比较简单。如此一来SpringMVC配置文件--springmvc.xml的内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.blackboard.syncData.controller"/>
<!-- 配置一个注解驱动,如果配置此标签,那么就可以不用配置处理器映射器和处理器适配器 -->
<mvc:annotation-driven />
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/user"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
文件路径仍然在config/spring下。
配置前端控制器
前端控制器需要需要配置在WEB-INF/web.xml中,如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>springmvc-web</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 指定springmvc配置文件的路径。如果不指定,默认为:/WEB-INF/${servlet-name}-servlet.xml -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- 初始化Spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 日志监听 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/config/log/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
</web-app>
不要忘记加载Spring容器↑
到此为止,Spring、MyBatis和SpringMVC三大框架就整合完了。
对了,最近我整理了关于SpringMVC+MyBatis整合的Maven版本,就省的去引入那些jar包了,项目代码GitHub地址:
https://github.com/WinterVictoria/SpringMVC_MyBatis_Maven
-----温故而知新。