<?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" xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 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 "> <!--扫描注解--> <context:annotation-config/> <context:component-scan base-package="com.bbs"/> <!--加载druid.properties属性文件--> <context:property-placeholder location="classpath:druid.properties"/> <bean id="druiddataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${druid.driver}" /> <property name="url" value="${druid.url}"/> <property name="username" value="${druid.userName}"/> <property name="password" value="${druid.passWord}"/> <!--初始化连接池--> <property name="initialSize" value="${druid.pool.init}"/> <!--最小连接个数--> <property name="minIdle" value="${druid.pool.minIde}"/> <!--最大连接个数--> <property name="maxActive" value="${druid.pool.maxActive}"/> <!--最大等待时间--> <property name="maxWait" value="${ruid.pool.timeout}"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--引入数据源--> <property name="dataSource" ref="druiddataSource"/> <!--解析Mapper文件--> <property name="mapperLocations" value="classpath:*Mapper.xml"/> <property name="typeAliasesPackage" value="com.bbs.po"/> </bean> <!--加载Dao包中的所有接口,通过sqlSessionFactory获取qlSession,然后创建所有的dao接口对象,存储在Spring容器种--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <property name="basePackage" value="com.bbs.dao"/> </bean> <!--将Spring事务管理交给Spring容器管理--> <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"></bean> `<!--通过Spring jdbc提供的tx标签,声明事务管理的机制--> <tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager"> <!--凡是inser方法开头的,都要用下面定义的事务隔离级别和事务传播机制--> <!--isolation设置事务的隔离级别: 1.READ_UNCOMMITTED 读未提交 2.READ_COMMITTED 读也提交 3.EPEATABLE_READ可重复阅读 4.SERIALIZABLE 序列化 propagation设置事务的传播机制 1.REQUIRED,如果上层方法没有事务,则创建一个事务,如果有事务就加入事务中 2.SUPPORTS ,如果上层方法没有事务,则以非事务的方法加进来,如果有事务就加入事务中 3.REQUIRES_NEW,如果上层方法没有事务,则创建一个事务,如果有事务就挂起 4.NOT_SUPPORTED,如果上层方法没有事务,则以非事务的方法加进来,如果有事务就挂起 5.NEVER,如果上层方法没有事务,则以非事务的方法加进来,如果有事务就抛出异常 6.MANDATORY,必须在事务中运行,没有事务则不会创建新的事务,意思是如果上层方法有事务则加入事务中执行,如果没有事务则抛出异常 7.NESTED,如果上层方法没有事务,则创建一个事务,如果有事务就嵌套到当前的事务中 --> <tx:attributes> <tx:method name="inser" isolation="SERIALIZABLE" propagation="NESTED"/> </tx:attributes> </tx:advice> </beans>
Spring整合Mybatis的xml文件
最新推荐文章于 2023-11-25 21:51:53 发布