导入整合所需的库
Maven:
<properties>
<spring.version>4.3.10.RELEASE</spring.version>
</properties>
<!--Spring的JDBC支持-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--Spring的事务支持-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!--Mybatis的Spring整合支持库-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
一、创建数据库配置文件db.properties
jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=wang
jdbc.password=123
二、创建spring-mybatis.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:db.properties"/>
<context:component-scan base-package="com.learn_ssm.bean"/>
<context:component-scan base-package="com.learn_ssm.dao"/>
<context:component-scan base-package="com.learn_ssm.service"/>
</beans>
扫描相关的Bean
<context:component-scan base-package="com.learn_ssm.dao"/>
<context:component-scan base-package="com.learn_ssm.service"/>
引入数据库配置文件并创建dbcp连接池的BasicDataSource
<context:property-placeholder location="classpath:db.properties"/>
<!--Dbcp连接池作为数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<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="initialSize" value="${initialSize}"/>-->
<!-- 连接池最大数量 -->
<!--<property name="maxTotal" value="${maxActive}"/>-->
<!-- 连接池最大空闲 -->
<!--<property name="maxIdle" value="${maxIdle}"/>-->
<!-- 连接池最小空闲 -->
<!--<property name="minIdle" value="${minIdle}"/>-->
<!-- 获取连接最大等待时间 -->
<!--<property name="maxWaitMillis" value="${maxWait}"/>-->
</bean>
说明:也可以不使用dbcp连接池,自己写一个java.sql.DataSource接口的实现类作为数据源。
定义Mybatis提供给Spring整合的SqlSessionFactoryBean
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--定义Mybatis全局配置文件-->
<property name="configLocation" value="classpath:SqlMapConfig.xml"/>
<!-- 自动扫描mapper.xml文件,**表示迭代查找(这里指dao以及dao下的所有子目录和孙子目录) -->
<!--<property name="mapperLocations" value="classpath:com/learnssm/dao/**/*.xml"/>-->
</bean>
定义Mybatis提供给Spring整合的Mapper映射文件扫描器
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- DAO接口所在包名,Spring会自动查找其下的类 ,包下的类需要使用@MapperScan注解,否则容器注入会失败 -->
<property name="basePackage" value="com.learn_ssm.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
定义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="query*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.learn_ssm.dao.*.*(..))"/>
</aop:config>
propagation说明:
REQUIRED: 支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
SUPPORTS: 支持当前事务,如果当前没有事务,就以非事务方式执行。
MANDATORY: 支持当前事务,如果当前没有事务,就抛出异常。
REQUIRES_NEW: 新建事务,如果当前存在事务,把当前事务挂起。
NOT_SUPPORTED:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
NEVER: 以非事务方式执行,如果当前存在事务,则抛出异常。
NESTED: 支持当前事务,如果当前事务存在,则执行一个嵌套事务,如果当前没有事务,就新建一个事务。
三、创建spring-mvc.xml配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.learn_ssm.controller"/>
<mvc:annotation-driven/>
</beans>
四、在web.xml中配置Spring
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-mybatis.xml,
<!--classpath:spring-mvc.xml,-->
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
<!--classpath:spring-mybatis.xml,-->
classpath:spring-mvc.xml,
</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
说明:
两个spring配置文件可以分别任意定义在ContextLoaderListener或DispatcherServlet中。
区别是定义在ContextLoaderListener会在服务器启动时就加载,
而定义在DispatcherServlet会在第一次请求时才加载。
context-param 是全局性配置,servlet下的init-param 是局部性配置
若以上2种配置都引入了,那么全局性的bean配置文件会被加载2次。
而且2种方式的各自配置文件里的配置项在某种意义上并不是合并互补,
而是各成一个体系(虽然普通的bean看似是都加载到全局上下文来了,
但还是有一些特殊bean和配置项没有按预期的那样工作)。
比如配置了AOP,若只是在全局配置项中,而没有在DispatcherServlet 中加载,那么此aop会无效。
所以,在web项目中,就不要使用ContextLoaderListener和全局配置contextConfigLocation参数了,
统一在DispatcherServlet 下配置,应该就不那么混乱了。