最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring配置數(shù)據(jù)源的三種方式(小結(jié))

 更新時(shí)間:2022年01月19日 15:18:12   作者:我是一棵卷心菜  
本文主要介紹了Spring配置數(shù)據(jù)源的三種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

一、前言

今天學(xué)習(xí)了用spring配置Druid數(shù)據(jù)源的三種方式,整理了學(xué)習(xí)筆記,希望大家喜歡!

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

  • 數(shù)據(jù)源(連接池)是提高程序性能如出現(xiàn)的
  • 事先實(shí)例化數(shù)據(jù)源,初始化部分連接資源
  • 使用連接資源時(shí)從數(shù)據(jù)源中獲取
  • 使用完畢后將連接資源歸還給數(shù)據(jù)源

常見(jiàn)的數(shù)據(jù)源:DBCP、C3P0BoneCP、Druid等等,本文主要以Druid數(shù)據(jù)源為案例實(shí)現(xiàn)Spring對(duì)數(shù)據(jù)源的開(kāi)發(fā)應(yīng)用

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

方式1:手動(dòng)輸入

先創(chuàng)建一個(gè)maven工程,引入依賴(lài),為了方便起見(jiàn),我還導(dǎo)入了Junit的依賴(lài),此外,還有mysql的驅(qū)動(dòng)依賴(lài)、Druid數(shù)據(jù)源的依賴(lài)和spring依賴(lài)

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

直接編寫(xiě)一個(gè)測(cè)試類(lèi),開(kāi)始測(cè)試

    @Test
    public void test1() throws SQLException {
    	//創(chuàng)建數(shù)據(jù)源對(duì)象
        DruidDataSource dataSource = new DruidDataSource();
        //設(shè)置數(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);
        //關(guān)閉連接資源
        connection.close();
    }

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

運(yùn)行結(jié)果:

方式2:Properties配置文件

resources下建一個(gè)名為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

編寫(xiě)一個(gè)測(cè)試類(lèi),開(kāi)始測(cè)試

	@Test
    public void test2() throws SQLException {
    	//ResourceBundle這個(gè)類(lèi)專(zhuān)門(mén)用來(lái)讀取properties類(lèi)型的文件
        ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
        //設(shè)置數(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ù)庫(kù)發(fā)生了改變,就只需要在Properties文件中進(jìn)行修改,從而不需要從代碼中修改,提高了開(kāi)發(fā)的效率

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

繼續(xù)使用前面的jdbc.properties文件,我們可以將數(shù)據(jù)源的創(chuàng)建權(quán)交由Spring容器去完成,編寫(xiě)一個(gè)名為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>

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

  @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();
    }

運(yùn)行結(jié)果:

不知道小伙伴們看到value的屬性值那么長(zhǎng),有沒(méi)有感覺(jué)到一絲絲的不舒服,反正我是有。那么有沒(méi)有一種方法能夠?qū)⑴渲酶拥那逦髁四兀看鸢甘牵河?!那么該如何做呢?/p>

首先要做的是,把jdbc.properties配置文件的對(duì)象放進(jìn)spring容器中,這樣就方便了以后的調(diào)用,具體代碼:

<?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>

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

四、總結(jié)

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

 到此這篇關(guān)于Spring配置數(shù)據(jù)源的三種方式(小結(jié))的文章就介紹到這了,更多相關(guān)Spring配置數(shù)據(jù)源內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

连城县| 乐都县| 句容市| 克什克腾旗| 德兴市| 临武县| 莫力| 比如县| 佛冈县| 于都县| 呼玛县| 波密县| 新邵县| 津南区| 洪湖市| 元朗区| 东乡| 吉木萨尔县| 南投市| 郑州市| 循化| 天等县| 阿合奇县| 眉山市| 贵州省| 剑河县| 班戈县| 恩施市| 新绛县| 乐亭县| 安吉县| 邹城市| 龙山县| 抚松县| 明星| 乐业县| 临桂县| 寻乌县| 阿图什市| 宜川县| 沭阳县|