用SSM 框架做了一个小项目,把spring 配置文件整理了一下,配置如下:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
">
<!-- 自动扫描包中的类-->
<context:component-scan base-package="com.etc.service" />
<!-- 配置数据源 -->
<!-- DriverManagerDataSource Bean 没有运用连接池技术,在多并发情况下性能方面欠佳-->
<!-- <bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"> -->
<!--此方式运用了连接池技术,在多并发情况下性能较好 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/myschool?characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value=""/>
<property name="maxActive" value="100"/> <!-- 最大连接数 -->
<property name="maxIdle" value="20"/> <!-- 最小连接数 -->
<property name="maxWait" value="120000"/> <!-- 最大空闲时间 : 120秒 -->
</bean>
<!-- 配置mybatis的sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mappers.xml文件 -->
<property name="mapperLocations" value="classpath:com/etc/dao/*.xml"/>
<!-- mybatis配置文件
<property name="configLocation" value="classpath:mybatis-config.xml"/>-->
<property name="typeAliasesPackage" value="com.etc.entity"></property>
</bean>
<!-- 定义DeptMapper接口,DAO接口所在包名,Spring会自动查找其下的类-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.etc.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- (事务管理)-->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 启用事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 配置事务通知属性,上面采用了注解进行事务配置,下面的XML事务配置注释掉了 -->
<!-- <tx:advice id="txAdvice" transaction-manager="transactionManager"> -->
<!-- 定义事务传播属性
<tx:attributes>
<tx:method name="*"/>
</tx:attributes>
</tx:advice> -->
<!-- 配置事务切面
<aop:config>
<aop:pointcut id="txPointCut"
expression="execution(* com.etc.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut" />
</aop:config> -->
</beans>