其他分享
首页 > 其他分享> > spring—配置数据源

spring—配置数据源

作者:互联网

数据源(连接池)的作用

数据源(连接池)是提高程序性能如出现的
事先实例化数据源,初始化部分连接资源
使用连接资源时从数据源中获取
使用完毕后将连接资源归还给数据源
常见的数据源(连接池):DBCP、C3P0、BoneCP、Druid等
开发步骤
①导入数据源的坐标和数据库驱动坐标
②创建数据源对象
③设置数据源的基本连接数据
④使用数据源获取连接资源和归还连接资源

数据源的手动创建

①导入c3p0

<!-- C3P0连接池 -->
 <dependency>    
 <groupId>c3p0</groupId> 
    <artifactId>c3p0</artifactId>    
    <version>0.9.1.2</version> 
    </dependency>

①导入mysql数据库驱动坐标

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>

②创建C3P0连接池

    @Test
    public void testC3P0() throws Exception
    {
        ComboPooledDataSource comboPooledDataSource=new ComboPooledDataSource();
        comboPooledDataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
        comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/book?useSSL=false&serverTimezone=UTC");
        comboPooledDataSource.setUser("root");
        comboPooledDataSource.setPassword("123456");
        Connection connection=comboPooledDataSource.getConnection();
        System.out.println(connection);
    }

提取jdbc.properties配置文件

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/book?useSSL=false&serverTimezone=UTC
jdbc.username=xxx
jdbc.password=xxx

Spring配置数据源

可以将DataSource的创建权交由Spring容器去完成 DataSource有无参构造方法,而Spring默认就是通过无参构造方法实例化对象的
DataSource要想使用需要通过set方法设置数据库连接信息,而Spring可以通过set方法进行字符串注入

    @Test
    public void testC3P0() throws Exception
    {
        ComboPooledDataSource comboPooledDataSource=new ComboPooledDataSource();
        ResourceBundle resourceBundle=ResourceBundle.getBundle("jdbc");
        comboPooledDataSource.setDriverClass(resourceBundle.getString("jdbc.driver"));
        comboPooledDataSource.setJdbcUrl(resourceBundle.getString("jdbc.url"));
        comboPooledDataSource.setUser(resourceBundle.getString("jdbc.username"));
        comboPooledDataSource.setPassword(resourceBundle.getString("jdbc.password"));
        Connection connection=comboPooledDataSource.getConnection();
        System.out.println(connection);
    }
<?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
">
<context:property-placeholder location="jdbc.properties"/>
    <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    </beans>
    @Test
    public void testC3P02() throws Exception
    {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
        ComboPooledDataSource comboPooledDataSource=(ComboPooledDataSource) applicationContext.getBean("datasource");
        System.out.println(comboPooledDataSource.getConnection());

    }

标签:jdbc,spring,数据源,配置,resourceBundle,comboPooledDataSource,mysql,ComboPooledDataSou
来源: https://blog.csdn.net/weixin_44560620/article/details/113661275