Spring创建连接池 c3p0方法和druid方法
一、c3p0方法,手动配置
手动配置:
// c3p0 测试获取数据链接
@Test
public void testDataSource() throws Exception {
ComboPooledDataSource pooledDataSource = new ComboPooledDataSource();
pooledDataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
pooledDataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/study-mybatis");
pooledDataSource.setUser("root");
pooledDataSource.setPassword("17633908064");
Connection connection = pooledDataSource.getConnection();
System.out.println(connection);
connection.close();
}
二、druid方法,手动配置
// druid获取数据链接
@Test
public void testDruid() throws Exception{
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/study-sql");
dataSource.setUsername("root");
dataSource.setPassword("17633908064");
DruidPooledConnection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}
三、spring配置,通过xml配置
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/study-sql"/>
<property name="username" value="root"/>
<property name="password" value="17633908064"/>
</bean>
spring支持SPEL可通过引入外部配置文件进行解耦
jdbc.properties文件:
jdbc.driver = com.mysql.cj.jdbc.Driver
jdbc.url = jdbc:mysql://127.0.0.1:3306/study-sql
jdbc.username = root
jdbc.password = 17633908064
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:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 引入jdbc.properties配置文件,Spring支持SPEL,可以加载外部配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
</beans>
四、手动配置也可以通过加载properties文件进行解耦
java自带解析properties文件类 ResourceBundle,通过ResourceBundle.getBundle()加载配置文件。(路径为全路径,一般在resources文件夹下)
// 加载配置文件获取数据链接,可解耦
@Test
public void testDataSourceFromProperties() throws Exception{
// resources 下的路径不需要带后缀名 .properties
ResourceBundle rs = ResourceBundle.getBundle("jdbc");
String driver = rs.getString("jdbc.driver");
String url = rs.getString("jdbc.url");
String username = rs.getString("jdbc.username");
String password = rs.getString("jdbc.password");
System.out.println(driver);
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
DruidPooledConnection connection = dataSource.getConnection();
System.out.println(connection);
}