Spring创建连接池 c3p0方法和druid方法

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);

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值