Spring part 4:数据库连接池的配置
内置连接池
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql:///test"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean>
DBCP连接池
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql:///test"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean>
C3P0连接池
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///test"></property> <property name="user" value="root"></property> <property name="password" value="root"></property> </bean>
引入外部资源文件
资源文件
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql:///test jdbc.username=root jdbc.password=root
配置
<context:property-placeholder location="classpath:jdbc.properties" /> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean>
相关推荐
wintershii 2020-08-17
点滴技术生活 2020-07-19
LeoHan 2020-06-13
Laxcus大数据技术 2020-06-11
thunderstorm 2020-06-06
PengQ 2020-06-06
Zhangdragonfly 2020-06-05
tlsmile 2020-06-03
Kele0 2020-05-30
鲁氏汤包王 2020-04-18
favouriter 2020-04-18
yongyoumengxiang 2020-03-26
heniancheng 2020-03-25
nan00zzu 2020-02-23
步行者 2020-02-20
Java学习 2020-02-17
nan00zzu 2020-02-11
wintershii 2020-02-10