步骤一:在web.xml中配置spring
<
context-param
>
< param-name >contextConfigLocation </ param-name >
< param-value >classpath*:spring/applicationContext*.xml </ param-value >
</ context-param >
<!-- Spring的ApplicationContext 载入 -->
< listener >
< listener-class >org.springframework.web.context.ContextLoaderListener </ listener-class >
</ listener >
< param-name >contextConfigLocation </ param-name >
< param-value >classpath*:spring/applicationContext*.xml </ param-value >
</ context-param >
<!-- Spring的ApplicationContext 载入 -->
< listener >
< listener-class >org.springframework.web.context.ContextLoaderListener </ listener-class >
</ listener >
步骤二:配置applicationContext.xml
<!--设置配置文件-->
<
bean
id
="propertyConfigurer"
class
="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
>
< property name ="locations" >
< list >
< value >classpath*:/application.properties </ value >
< value >classpath*:/db.properties </ value >
</ list >
</ property >
</ bean >
< property name ="locations" >
< list >
< value >classpath*:/application.properties </ value >
< value >classpath*:/db.properties </ value >
</ list >
</ property >
</ bean >
<
bean
id
="dataSource"
class
="org.apache.commons.dbcp.BasicDataSource"
>
< property name ="maxActive" value ="20" />
< property name ="maxIdle" value ="20" />
< property name ="maxWait" value ="3000" />
< property name ="testWhileIdle" value ="true" />
< property name ="timeBetweenEvictionRunsMillis" value ="3600000" />
< property name ="validationQuery" value ="select 1" />
< property name ="removeAbandoned" value ="true" />
< property name ="removeAbandonedTimeout" value ="1" />
< property name ="driverClassName" value ="${jdbc.driverClassName}" />
< property name ="url" value ="${jdbc.url}" />
< property name ="username" value ="${jdbc.username}" />
< property name ="password" value ="${jdbc.password}" />
</ bean >
< property name ="maxActive" value ="20" />
< property name ="maxIdle" value ="20" />
< property name ="maxWait" value ="3000" />
< property name ="testWhileIdle" value ="true" />
< property name ="timeBetweenEvictionRunsMillis" value ="3600000" />
< property name ="validationQuery" value ="select 1" />
< property name ="removeAbandoned" value ="true" />
< property name ="removeAbandonedTimeout" value ="1" />
< property name ="driverClassName" value ="${jdbc.driverClassName}" />
< property name ="url" value ="${jdbc.url}" />
< property name ="username" value ="${jdbc.username}" />
< property name ="password" value ="${jdbc.password}" />
</ bean >
<
bean
id
="sqlSessionFactory"
class
="org.springframework.orm.ibatis3.SqlSessionFactoryBean"
>
< property name ="configLocation" value ="classpath:ibatis-config.xml" />
< property name ="dataSource" ref ="dataSource" />
</ bean >
< bean id ="sqlSessionTemplate" class ="org.springframework.orm.ibatis3.SqlSessionTemplate" >
< property name ="sqlSessionFactory" ref ="sqlSessionFactory" />
</ bean >
<!--事务配置-->
< tx:annotation-driven transaction-manager ="transactionManager" />
< bean id ="transactionManager" class ="org.springframework.jdbc.datasource.DataSourceTransactionManager" autowire ="byName" />
< property name ="configLocation" value ="classpath:ibatis-config.xml" />
< property name ="dataSource" ref ="dataSource" />
</ bean >
< bean id ="sqlSessionTemplate" class ="org.springframework.orm.ibatis3.SqlSessionTemplate" >
< property name ="sqlSessionFactory" ref ="sqlSessionFactory" />
</ bean >
<!--事务配置-->
< tx:annotation-driven transaction-manager ="transactionManager" />
< bean id ="transactionManager" class ="org.springframework.jdbc.datasource.DataSourceTransactionManager" autowire ="byName" />
注:db.properties内容如下
jdbcNum=2
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.1.1:3306/bookdb?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false
jdbc.username=sophie
jdbc.passworld=123456
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.1.1:3306/bookdb?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false
jdbc.username=sophie
jdbc.passworld=123456
转载于:https://blog.51cto.com/mmqzlj/498975