Spring引入外部屬性文件配置數(shù)據(jù)庫(kù)連接的步驟詳解
直接配置數(shù)據(jù)庫(kù)的信息
xml配置文件直接配置:
<?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:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
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/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--直接配置連接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/userDb"></property>
<property name="username" value="root" ></property>
<property name="password" value="root" ></property>
</bean>
</beans>
一般不會(huì)這樣用,不便于修改,我們看下面的引入外部屬性文件配置的方法
引入外部屬性文件配置數(shù)據(jù)庫(kù)連接
1.引入德魯伊連接池jar包
(1)導(dǎo)入進(jìn)來(lái)一個(gè)druid-1.0.9.jar,直接復(fù)制粘貼到當(dāng)前目錄就可以了。

(2)引入到當(dāng)前項(xiàng)目。


2.配置德魯伊連接池
(1)新建一個(gè)jdbc.properties文件,寫(xiě)數(shù)據(jù)庫(kù)的相關(guān)信息。
jdbc.properties:
jdbc.driverClass=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/userDb?characterEncoding=utf8&useUnicode=true&useSSL=false jdbc.username=root jdbc.password=root
(2)新建一個(gè)配置文件。
bean6.xml:
<?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:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
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/util http://www.springframework.org/schema/util/spring-util.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.driverClass}"></property>
<property name="url" value="${jdbc.url}" ></property>
<property name="username" value="${jdbc.username}" ></property>
<property name ="password" value="${jdbc.password}" ></property>
</bean>
</beans>
完成以上步驟,就完成了引入外部屬性文件配置數(shù)據(jù)庫(kù)連接。
到此這篇關(guān)于Spring引入外部屬性文件配置數(shù)據(jù)庫(kù)連接的步驟詳解的文章就介紹到這了,更多相關(guān)Spring外部屬性文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java動(dòng)態(tài)數(shù)組添加數(shù)據(jù)的方法與應(yīng)用示例
這篇文章主要介紹了Java動(dòng)態(tài)數(shù)組添加數(shù)據(jù)的方法,結(jié)合實(shí)例形式詳細(xì)分析了Java動(dòng)態(tài)數(shù)組的創(chuàng)建、添加、查找、打印等相關(guān)操作技巧,需要的朋友可以參考下2019-11-11
使用httpclient實(shí)現(xiàn)免費(fèi)的google翻譯api
這篇文章主要介紹了使用httpclient實(shí)現(xiàn)免費(fèi)的google翻譯api的方法,大家參考使用吧2014-01-01
AndroidQ沙盒機(jī)制之分區(qū)存儲(chǔ)適配
這篇文章主要介紹了AndroidQ沙盒機(jī)制之分區(qū)存儲(chǔ)適配,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
Java實(shí)戰(zhàn)在線選課系統(tǒng)的實(shí)現(xiàn)流程
讀萬(wàn)卷書(shū)不如行萬(wàn)里路,只學(xué)書(shū)上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+jsp+mysql+maven實(shí)現(xiàn)一個(gè)在線選課系統(tǒng),大家可以在過(guò)程中查缺補(bǔ)漏,提升水平2021-11-11
Java實(shí)現(xiàn)優(yōu)雅的參數(shù)校驗(yàn)方法詳解
這篇文章主要為大家詳細(xì)介紹了Java語(yǔ)言如何實(shí)現(xiàn)優(yōu)雅的參數(shù)校驗(yàn),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Java有一定是幫助,需要的可以參考一下2022-06-06
SpringBoot利用Redis解決海量重復(fù)提交問(wèn)題
本文主要介紹了SpringBoot利用Redis解決海量重復(fù)提交問(wèn)題,介紹了三種常見(jiàn)的解決方案,包括使用Redis計(jì)數(shù)器,使用Redis分布式鎖和使用Redis發(fā)布/訂閱機(jī)制,感興趣的可以了解一下2024-03-03
Java實(shí)現(xiàn)多個(gè)數(shù)組間的排列組合
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)多個(gè)數(shù)組間的排列組合,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
MyBatis?Generator?ORM層面的代碼自動(dòng)生成器(推薦)
Mybatis?Generator是一個(gè)專門(mén)為?MyBatis和?ibatis框架使用者提供的代碼生成器,也可以快速的根據(jù)數(shù)據(jù)表生成對(duì)應(yīng)的pojo類、Mapper接口、Mapper文件,甚至生成QBC風(fēng)格的查詢對(duì)象,這篇文章主要介紹了MyBatis?Generator?ORM層面的代碼自動(dòng)生成器,需要的朋友可以參考下2023-01-01

