溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Spring配置數(shù)據(jù)源的三種方式是什么

發(fā)布時間:2022-01-19 16:51:49 來源:億速云 閱讀:163 作者:iii 欄目:開發(fā)技術

這篇文章主要介紹“Spring配置數(shù)據(jù)源的三種方式是什么”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“Spring配置數(shù)據(jù)源的三種方式是什么”文章能幫助大家解決問題。

一、數(shù)據(jù)源的作用

  • 數(shù)據(jù)源(連接池)是提高程序性能如出現(xiàn)的

  • 事先實例化數(shù)據(jù)源,初始化部分連接資源

  • 使用連接資源時從數(shù)據(jù)源中獲取

  • 使用完畢后將連接資源歸還給數(shù)據(jù)源

常見的數(shù)據(jù)源:DBCPC3P0、BoneCPDruid等等,本文主要以Druid數(shù)據(jù)源為案例實現(xiàn)Spring對數(shù)據(jù)源的開發(fā)應用

二、開發(fā)數(shù)據(jù)源的方式

方式1:手動輸入

先創(chuàng)建一個maven工程,引入依賴,為了方便起見,我還導入了Junit的依賴,此外,還有mysql的驅動依賴、Druid數(shù)據(jù)源的依賴和spring依賴

 <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.27</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.22</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.14</version>
        </dependency>
    </dependencies>

直接編寫一個測試類,開始測試

    @Test
    public void test1() throws SQLException {
    	//創(chuàng)建數(shù)據(jù)源對象
        DruidDataSource dataSource = new DruidDataSource();
        //設置數(shù)據(jù)源的基本連接數(shù)據(jù)
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/test");
        dataSource.setUsername("root");
        dataSource.setPassword("0315");
        //使用數(shù)據(jù)源獲取連接資源
        Connection connection = dataSource.getConnection();
        //打印連接資源的信息
        System.out.println(connection);
        //關閉連接資源
        connection.close();
    }

分析: setDriverClassName()填入的是連接驅動類Driver的包路徑、setUrl()設置要連接的數(shù)據(jù)庫的地址、setUsername()自己的數(shù)據(jù)庫用戶名、setPassword()數(shù)據(jù)庫密碼

運行結果:

Spring配置數(shù)據(jù)源的三種方式是什么

方式2:Properties配置文件

resources下建一個名為jdbc.properties的文件,填入數(shù)據(jù)源的基本連接數(shù)據(jù)

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=0315

編寫一個測試類,開始測試

	@Test
    public void test2() throws SQLException {
    	//ResourceBundle這個類專門用來讀取properties類型的文件
        ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
        //設置數(shù)據(jù)源的基本連接數(shù)據(jù)
        String driver = bundle.getString("jdbc.driver");
        String url = bundle.getString("jdbc.url");
        String username = bundle.getString("jdbc.username");
        String password = bundle.getString("jdbc.password");

        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        DruidPooledConnection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();
    }

這種方式就比方式一好很多了,如果我們使用的數(shù)據(jù)庫發(fā)生了改變,就只需要在Properties文件中進行修改,從而不需要從代碼中修改,提高了開發(fā)的效率

方式3:Spring配置數(shù)據(jù)源

繼續(xù)使用前面的jdbc.properties文件,我們可以將數(shù)據(jù)源的創(chuàng)建權交由Spring容器去完成,編寫一個名為applicationContext.xml的spring配置文件,把數(shù)據(jù)源放入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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
        <property name="username" value="root"></property>
        <property name="password" value="0315"></property>
    </bean>
</beans>

通過這種spring配置文件的方式,我們就可以獲取了數(shù)據(jù)源,接下來寫一個代碼用來測試

  @Test
    public void test3() throws SQLException {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        DruidDataSource dataSource = applicationContext.getBean(DruidDataSource.class);
        DruidPooledConnection connection = dataSource.getConnection();
        //打印連接信息
        System.out.println(connection);
        connection.close();
    }

運行結果:

Spring配置數(shù)據(jù)源的三種方式是什么

不知道小伙伴們看到value的屬性值那么長,有沒有感覺到一絲絲的不舒服,反正我是有。那么有沒有一種方法能夠將配置更加的清晰明了呢?答案是:有!那么該如何做呢?

首先要做的是,把jdbc.properties配置文件的對象放進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"
       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="classpath:jdbc.properties"/>

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverClassName" value="${jdbc.driver}"></property>
    <property name="url" value="${jdbc.url}"></property>
    <property name="username" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
    </bean>
</beans>

分析: 首先要在頭文件中引入下圖所示的名稱空間,最后value的屬性值用${key}的方式獲取到jdbc.properties的value值,這樣的運行結果也是跟上面一樣

Spring配置數(shù)據(jù)源的三種方式是什么

三、總結

我們最需要掌握的就是最后一種方法,一定要學會這種配置方式!

Spring配置數(shù)據(jù)源的三種方式是什么

關于“Spring配置數(shù)據(jù)源的三種方式是什么”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識,可以關注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI