applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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">
<!-- 自动生成代理类-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- 注解类的位置-->
<context:component-scan base-package="com.spyouth"/>
<!-- 导入其他 bean spring 配置文件-->
<import resource="classpath:spring-*.xml"/>
<!-- 导入配置 name=value -->
<context:property-placeholder location="classpath:JDBC-config.properties"/>
<!-- 阿里巴巴 德鲁伊 数据源,数据库连接池-->
<bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 创建 sqlSessionFactory 对象-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定数据源-->
<property name="dataSource" ref="myDataSource"/>
<!-- 指定配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!-- 用于自动生成 mybatis getMapper 返回的dao对象-->
<bean id="scannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定 sqlSessionFactory 对象名称-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 指定 dao 接口所在目录-->
<property name="basePackage" value="com.spyouth.dao"/>
</bean>
<!-- 声明事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="myDataSource"/>
</bean>
<!-- 事务注解驱动-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 配置文件配置事务 声明事务方法,指定事务属性-->
<tx:advice id="advice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 方法名可使用通配符-->
<tx:method name="bugGoods" propagation="REQUIRED" isolation="DEFAULT"
rollback-for="com.spyouth.ex.GoodsNotEnoughException,java.lang.NullPointerException"/>
<tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT"
rollback-for="java.lang.RuntimeException"/>
<tx:method name="insert*" propagation="REQUIRED" isolation="DEFAULT"
rollback-for="java.lang.RuntimeException"/>
<tx:method name="*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 配置 aop 关联上面的-->
<aop:config >
<aop:pointcut id="servicePt" expression="execution(* *..service..*.*(..))"/>
<aop:advisor advice-ref="advice" pointcut-ref="servicePt"/>
</aop:config>
<bean id="studentService" class="com.spyouth.service.StudentServiceImpl" autowire="byName"/>
<bean id="buyGoodsService" class="com.spyouth.service.BuyGoodsServiceImpl2" autowire="byName"/>
</beans>
mybatis-config.xml
<?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>
<settings>
<!-- 延迟加载-->
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
<!-- 二级缓存,除了主配文件外,还要在需要二级缓存的 dao 配置文件中添加 <cache/> 标签 -->
<setting name="cacheEnabled" value="true"/>
</settings>
<!-- 实体类别名-->
<typeAliases>
<package name="com.spyouth.beans"/>
</typeAliases>
<mappers>
<!-- 自动为包下面的dao注册 mapper-->
<package name="com.spyouth.dao"/>
</mappers>
</configuration>
网友评论