Spring引入外部屬性文件配置數(shù)據(jù)庫連接的步驟詳解
直接配置數(shù)據(jù)庫的信息
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>
一般不會這樣用,不便于修改,我們看下面的引入外部屬性文件配置的方法
引入外部屬性文件配置數(shù)據(jù)庫連接
1.引入德魯伊連接池jar包
(1)導入進來一個druid-1.0.9.jar,直接復制粘貼到當前目錄就可以了。

(2)引入到當前項目。


2.配置德魯伊連接池
(1)新建一個jdbc.properties文件,寫數(shù)據(jù)庫的相關信息。
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)新建一個配置文件。
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ù)庫連接。
到此這篇關于Spring引入外部屬性文件配置數(shù)據(jù)庫連接的步驟詳解的文章就介紹到這了,更多相關Spring外部屬性文件內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java動態(tài)數(shù)組添加數(shù)據(jù)的方法與應用示例
這篇文章主要介紹了Java動態(tài)數(shù)組添加數(shù)據(jù)的方法,結合實例形式詳細分析了Java動態(tài)數(shù)組的創(chuàng)建、添加、查找、打印等相關操作技巧,需要的朋友可以參考下2019-11-11
使用httpclient實現(xiàn)免費的google翻譯api
這篇文章主要介紹了使用httpclient實現(xiàn)免費的google翻譯api的方法,大家參考使用吧2014-01-01
Java實戰(zhàn)在線選課系統(tǒng)的實現(xiàn)流程
讀萬卷書不如行萬里路,只學書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+jsp+mysql+maven實現(xiàn)一個在線選課系統(tǒng),大家可以在過程中查缺補漏,提升水平2021-11-11
Java實現(xiàn)優(yōu)雅的參數(shù)校驗方法詳解
這篇文章主要為大家詳細介紹了Java語言如何實現(xiàn)優(yōu)雅的參數(shù)校驗,文中的示例代碼講解詳細,對我們學習Java有一定是幫助,需要的可以參考一下2022-06-06
MyBatis?Generator?ORM層面的代碼自動生成器(推薦)
Mybatis?Generator是一個專門為?MyBatis和?ibatis框架使用者提供的代碼生成器,也可以快速的根據(jù)數(shù)據(jù)表生成對應的pojo類、Mapper接口、Mapper文件,甚至生成QBC風格的查詢對象,這篇文章主要介紹了MyBatis?Generator?ORM層面的代碼自動生成器,需要的朋友可以參考下2023-01-01

