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

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

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

一、前言

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

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

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

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

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

方式1:手動輸入

先創(chuàng)建一個maven工程,引入依賴,為了方便起見,我還導入了Junit的依賴,此外,還有mysql的驅(qū)動依賴、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);
        //關(guān)閉連接資源
        connection.close();
    }

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

運行結(jié)果:

方式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)建權(quán)交由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();
    }

運行結(jié)果:

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

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

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

四、總結(jié)

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

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

相關(guān)文章

最新評論

梅河口市| 岑溪市| 射阳县| 冀州市| 溧阳市| 土默特右旗| 德州市| 昭平县| 徐汇区| 胶州市| 新泰市| 雷州市| 平果县| 保靖县| 邛崃市| 刚察县| 社旗县| 台前县| 三江| 剑川县| 沙雅县| 扶沟县| 皮山县| 乌鲁木齐县| 罗城| 耿马| 合水县| 绥棱县| 革吉县| 宣恩县| 丹巴县| 望江县| 新平| 灌阳县| 兴隆县| 郸城县| 广宗县| 岳西县| 河源市| 咸丰县| 尼木县|