参考:http://www.cnblogs.com/200911/archive/2012/08/10/2631760.html
Spring对数据源的实现有三种,下面是在applicationContext.xml中数据源的配置。
(1)ComboPooledDataSource连接池--->需要有c3p0的包
<context:property-placeholder location="classpath:db.properties"/>
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <bean name="accountDao" class="cn.hu.dao.impl.AccountDaoImpl"> <property name="dataSource" ref="dataSource"></property> </bean>db.properties:
jdbc.jdbcUrl=jdbc:mysql:///hibernate
jdbc.driverClass=com.mysql.jdbc.Driver jdbc.user=root jdbc.password=mysql(2)BasicDataSource连接池----->需要commons-dbcp.jar、commons-pool.jar包
<context:property-placeholder location="classpath:db.properties"/>
<bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="url" value="${jdbc.url}"></property> <property name="driverClassName" value="${jdbc.driverClassName}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 配置Mybatis的工厂 --> <bean name="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:sqlMapConfig.xml"></property> </bean><!-- 直接扫描Mapper接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.hu.dao"></property> </bean>db.properties:
jdbc.url=jdbc:mysql:///springmvc
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.username=root jdbc.password=mysql(3)DruidDataSource连接池----->需要druid包