27.Spring配置四种数据源

 

1.Spring

Spring自带的数据源配置

applicationContext.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
   <!-- Spring数据源 -->
<bean id="dataSource1" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 注入相关属性 -->
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/spring_test"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>

<!-- 声明sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource1"></property>
<!-- 注入hibernate.cfg.xml -->
<property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
</bean>

2.c3p0

依赖包:

1
2
c3p0-0.9.2.1.jar
mchange-commons-java-0.2.3.4.jar

applicationContext.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!-- c3p0数据源 -->
<bean id="dataSource2" class="com.mchange.v2.c3p0.DriverManagerDataSource">
<!-- 注入相关属性 -->
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/spring_test"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean>

<!-- 声明sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource2"></property>
<!-- 注入hibernate.cfg.xml -->
<property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
</bean>

3.dbcp

依赖包:

1
2
commons-dbcp-1.4.jar
commons-pool-1.6.jar

applicationContext.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!-- dbcp数据源 -->
<bean id="dataSource3" class="org.apache.commons.dbcp.BasicDataSource">
<!-- 注入相关属性 -->
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/spring_test"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>

<!-- 声明sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource3"></property>
<!-- 注入hibernate.cfg.xml -->
<property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
</bean>

4.druid

依赖包:

1
druid-1.1.10.jar

applicationContext.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!-- druid数据源 -->
<bean id="dataSource4" class="com.alibaba.druid.pool.DruidDataSource">
<!-- 注入相关属性 -->
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/spring_test"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>

<!-- 声明sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource4"></property>
<!-- 注入hibernate.cfg.xml -->
<property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
</bean>

注: druid提供了web端可视化界面,可以监控查看连接池的详细情况。