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ù)庫的相關(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)新建一個配置文件。
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ù)庫連接。
到此這篇關(guān)于Spring引入外部屬性文件配置數(shù)據(jù)庫連接的步驟詳解的文章就介紹到這了,更多相關(guān)Spring外部屬性文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot任務調(diào)度器的實現(xiàn)代碼
SpringBoot自帶了任務調(diào)度器,通過注解的方式使用。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12Java及Android中常用鏈式調(diào)用寫法簡單示例
這篇文章主要介紹了Java及Android中常用鏈式調(diào)用寫法,結(jié)合實例形式分析了java編程中的鏈式調(diào)用概念、簡單使用方法及相關(guān)操作技巧,需要的朋友可以參考下2018-01-01MybatisPlus:使用SQL保留字(關(guān)鍵字)的操作
這篇文章主要介紹了MybatisPlus:使用SQL保留字(關(guān)鍵字)的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11