<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- spring整合mybatis -->
<!-- 引入数据库配置文件 -->
<context:property-placeholder location="db.properties"/>
<!-- dataSource -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 配置mybatis的factory
mybatis为了整合spring推出了另一个获取factory的工具类:org.mybatis.spring.SqlSessionFactoryBean
-->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 配置加载mybatis的主配置文件 -->
<property name="configLocation" value="mybatis-config.xml"></property>
</bean>
<!-- 配置mapper:利用spring自动生成mapper对象
org.mybatis.spring.mapper.MapperScannerConfigurer:可以自动生成mapper对象
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描mapper所在的包,自动生成mapper
下面的代码意味着在com.woniuxy.mapper包类的所有接口都将生成对应的mapper对象
-->
<property name="basePackage" value="com.woniuxy.mapper"></property>
<!-- 指定factory,由factor创建session,再由session创建mapper
一个坑,这里引入factory不是用ref而是使用value
-->
<property name="sqlSessionFactoryBeanName" value="factory"></property>
</bean>
<!-- 配置service -->
<bean id="userService" class="com.woniuxy.service.impl.UserServiceImpl">
<property name="userMapper" ref="userMapper"></property>
<property name="logService" ref="logService"></property>
</bean>
<bean id="logService" class="com.woniuxy.service.impl.LogServiceImpl">
<property name="logMapper" ref="logMapper"></property>
</bean>
<!--================= 事务配置:aop============= -->
<!-- 配置切点 -->
<aop:config>
<!--这里的expression中的内容是切点表达式,将会为所有符合该表达式的方法配置切点-->
<aop:pointcut expression="execution(* com.woniuxy.service.impl.UserServiceImpl.*.*(..))" id="pc"/>
<aop:advisor advice-ref="tx_advice" pointcut-ref="pc"/>
</aop:config>
<!--事务通知bean -->
<tx:advice id="tx_advice" transaction-manager="transactionManager" >
<!-- 此处可以为不同的切点方法配置不同的事务处理方式 -->
<tx:attributes>
<!-- *代表所有方法,此处代表所有的方法上的事物都按照默认的配置进行处理 -->
<tx:method name="*"/>
<!--事务传播机制:如果执行一个方法遇到了两个事务,那么怎么来处理这两个事务
BankService send(); 事务A
send(){
//转出、转入
//添加日志
logService.addLog(log)
//xxxx
}
LogService addLog(); 事务B
设置方法:在上面tx:method 中添加 propagation="..." 默认为REQUIRED,代表当执行到事务B时已经存在了事务A 那么就不创建事务B,直接在事务A中执行
-->
</tx:attributes>
</tx:advice>
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 指定管理那个数据库的事务 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
spring配置文件中整合mybatis
最新推荐文章于 2024-08-26 00:37:01 发布