Spring通過c3p0配置bean連接數(shù)據(jù)庫(kù)
Spring配置bean連接數(shù)據(jù)庫(kù)兩種方法:
(1)直接在.xml文件內(nèi)部配置:
<?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-4.3.xsd"> <!-- 配置鏈接數(shù)據(jù)庫(kù) --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="root"></property> <property name="password" value="wangjian"></property> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false"></property> </bean> </beans>
(2)將參數(shù)放在外部文件中,然后再讀入配置文件:
<?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-4.3.xsd">
<!-- 導(dǎo)入屬性文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 使用外部化屬性文件的屬性 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driverclass}"></property>
<property name="jdbcUrl" value="${jdbcurl}"></property>
</bean>
</beans>
外部的配置文件:
user=root password=wangjian driverclass=com.mysql.jdbc.Driver jdbcurl=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
測(cè)試函數(shù):
package com.primary.spring.beans.properties;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) throws SQLException {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-properties.xml");
DataSource dataSource = (DataSource) ctx.getBean("dataSource");
System.out.println(dataSource.getConnection());
}
}
測(cè)試結(jié)果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)多線程中的靜態(tài)代理模式
靜態(tài)代理屬于設(shè)計(jì)模式中的代理模式。這篇文章主要介紹了Java實(shí)現(xiàn)多線程中的靜態(tài)代理模式,詳細(xì)的介紹了靜態(tài)代理的使用,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
windows 部署JAVA環(huán)境安裝iDea的詳細(xì)步驟
這篇文章主要介紹了windows 部署JAVA環(huán)境安裝iDea的詳細(xì)步驟,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
基于Java實(shí)現(xiàn)無向環(huán)和有向環(huán)的檢測(cè)
這篇文章主要介紹了如何在?Java?中實(shí)現(xiàn)無向環(huán)和有向環(huán)的檢測(cè),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Java有一定的幫助,需要的可以參考一下2022-04-04
Springboot實(shí)現(xiàn)郵箱驗(yàn)證碼注冊(cè)與修改密碼及登錄功能詳解流程
驗(yàn)證碼作為一種自然人的機(jī)器人的判別工具,被廣泛的用于各種防止程序做自動(dòng)化的場(chǎng)景中。傳統(tǒng)的字符型驗(yàn)證安全性已經(jīng)名存實(shí)亡的情況下,各種新型的驗(yàn)證碼如雨后春筍般涌現(xiàn),今天給大家分享一篇SpringBoot實(shí)現(xiàn)滑塊驗(yàn)證碼2022-11-11
Java IO流對(duì)象的序列化和反序列化實(shí)例詳解
這篇文章主要介紹了Java IO流對(duì)象的序列化和反序列化實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05
Mybatis?Plus?新版lambda?表達(dá)式查詢異常的處理
這篇文章主要介紹了Mybatis?Plus?新版lambda?表達(dá)式查詢異常的處理方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
Spring Cloud EureKa Ribbon 服務(wù)注冊(cè)發(fā)現(xiàn)與調(diào)用
這篇文章主要介紹了Spring Cloud EureKa Ribbon 服務(wù)注冊(cè)發(fā)現(xiàn)與調(diào)用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02
SpringBoot整合JPA詳細(xì)代碼實(shí)例
這篇文章主要給大家介紹了關(guān)于SpringBoot整合JPA的相關(guān)資料,JPA(Java Persistence API)是Sun官方提出的Java持久化規(guī)范,它為Java開發(fā)人員提供了一種對(duì)象/關(guān)聯(lián)映射工具來管理Java應(yīng)用中的關(guān)系數(shù)據(jù),需要的朋友可以參考下2024-05-05

