<?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" xmlns:mvc = "http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/dian?useUnicode=true&characterEncoding=utf-8"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> <!-- 配置sqlSessionFactoryBean --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 引用数据源组件 --> <property name="dataSource" ref="dataSource"></property> <!-- 引用MyBatis的配置 --> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <!-- 配置SqlSessionTemplate --> <bean id="SqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg> </bean> <!-- 配置DAo --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value= "cn.lz.mapper"></property> </bean> <!-- 配置Service --> <context:component-scan base-package="cn.lz.service"/> <!-- 配置数据库--> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref = "dataSource"> </property> </bean> <!-- 事物开启--> <tx:advice id="txAdivce" transaction-manager="txManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!-- 事物监控--> <aop:config> <aop:pointcut expression="execution(* cn.lz.service..*.*(..))" id="proS"/> <aop:advisor advice-ref="txAdivce" pointcut-ref="proS"/> </aop:config>
</beans>