Spring數(shù)據(jù)源配置【掃盲篇】
點擊上方藍色字體,選擇“標星公眾號”
優(yōu)質文章,第一時間送達
? 作者?|??一只胡說八道的猴子?
來源 |? urlify.cn/jEfuIz
1、Spring系列之數(shù)據(jù)源的配置
2、數(shù)據(jù)源,連接池,數(shù)據(jù)庫三者的區(qū)別
連接池:這個應該都學習過,比如c3p0,druid等等,連接池的作用是為了提高程序的效率,因為頻繁的去創(chuàng)建,關閉數(shù)據(jù)庫連接,會對性能有很大的消耗,所以就有了連接池,連接池顧名思義是存儲多個連接的池子,池子中的連接都是創(chuàng)建好的,我們只要拿來使用即可,不用的時候就歸還給連接池,這就大大減少了關閉創(chuàng)建連接的時間,提高了效率
數(shù)據(jù)庫:存儲數(shù)據(jù)的地方
數(shù)據(jù)源:數(shù)據(jù)源顧名思義是數(shù)據(jù)的來源,存儲了連接數(shù)據(jù)庫所需要的信息,也可以說是用于管理數(shù)據(jù)庫連接池,并不存儲真正的數(shù)據(jù),僅僅記錄了連接哪個數(shù)據(jù)庫,怎么連接。如果把數(shù)據(jù)庫比作一個文件的話,那么數(shù)據(jù)源存儲的就是文件的名稱,可以通過文件名稱來找到對應的文件,算是一個抽象的映射,一個數(shù)據(jù)庫對應一個數(shù)據(jù)源,數(shù)據(jù)源可能是一個連接,也可能是一個連接池
如果你是玫瑰,他就是牛糞

呸呸呸,說錯了
如果數(shù)據(jù)是水,數(shù)據(jù)庫就是水庫,數(shù)據(jù)源就是管道,終端用戶看到的數(shù)據(jù)集是管道里流出來的水。

Spring功能這么強大,怎么可能少的了數(shù)據(jù)源呢
3、Spring配置數(shù)據(jù)源
配置步驟:
1.導入數(shù)據(jù)源的坐標與數(shù)據(jù)庫驅動坐標
2.創(chuàng)建數(shù)據(jù)源對象
3.設置數(shù)據(jù)源的基本連接信息
4.使用數(shù)據(jù)源獲取連接或歸還連接
需要導入的坐標信息
junit
????????junit
????????junit
????????4.13
????????test
????druid
????com.alibaba
????druid
????1.0.9
c3p0
????c3p0
????c3p0
????0.9.1.2
spring—context
????org.springframework
????spring-context
????5.0.3.RELEASE
mysql
??
????????mysql
????????mysql-connector-java
????????5.1.32
????我先手動配置一波,等一下再用Spring容器經(jīng)行配置,大家就能看到二者的巨大差別了
手動配置數(shù)據(jù)源
druid
?public?void?main()?throws?Exception{
????????//創(chuàng)建數(shù)據(jù)源
????????DruidDataSource?druidDataSource?=?new?DruidDataSource();
????????//設置連接參數(shù)
????????druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
????????druidDataSource.setUrl("jdbc:mysql://localhost:3309/one");
????????druidDataSource.setUsername("root");
????????druidDataSource.setPassword("1234");
????????//獲取連接對象
????????DruidPooledConnection?connection?=?druidDataSource.getConnection();
????????System.out.println(connection);
????}c3p0
?public?void?test2()?throws?Exception{
????????//創(chuàng)建數(shù)據(jù)源
????????ComboPooledDataSource?comboPooledDataSource?=?new?ComboPooledDataSource();
????????//設置連接參數(shù)
????????comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver");
????????comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3309/one");
????????comboPooledDataSource.setUser("root");
????????comboPooledDataSource.setPassword("1234");
????????//獲取連接對象
???????comboPooledDataSource.getConnection();
????????System.out.println(comboPooledDataSource);
????}為了降低耦合性之前我們是通過讀取配置文件的方法,這里我給大家重新復習一下
首先抽取要配置的信息到配置文件
右端的字符串注意不要加雙引號,否則會報錯,因為他默認就是字符串
jdbc.Driver=com.mysql.jdbc.Driver
jdbc.Url=jdbc:mysql://localhost:3309/one
jdbc.Username=root
jdbc.Password=1234再讀取配置文件來創(chuàng)建連接池
public?void?test3()?throws??Exception{
//加載路徑下的properties
????????ResourceBundle?bundle?=?ResourceBundle.getBundle("jdbc");
????????//創(chuàng)建數(shù)據(jù)源
????????ComboPooledDataSource?comboPooledDataSource?=?new?ComboPooledDataSource();
????????//設置連接參數(shù)
????????comboPooledDataSource.setDriverClass(bundle.getString("jdbc.Driver"));
????????comboPooledDataSource.setJdbcUrl(bundle.getString("jdbc.Url"));
????????comboPooledDataSource.setUser(bundle.getString("jdbc.Username"));
????????comboPooledDataSource.setPassword(bundle.getString("jdbc.Password"));
????????//獲取連接對象
????????comboPooledDataSource.getConnection();
????????System.out.println(comboPooledDataSource);
????}這樣的方式很好的降低了耦合性
重點來了,下面我們來講講如何使用Spring來配置數(shù)據(jù)源

4、Spring配置數(shù)據(jù)源
將DataSource的創(chuàng)建權交給Spring容器去完成
DataSource有無參構造方法,Spring默認就是通過無參構造方法實例化對象
DataSource要想使用需要通過set方法設置數(shù)據(jù)庫連接信息,Spring可以通過set方法進行注入
在Spring容器中配置Bean
?"dataSource"?class="com.mchange.v2.c3p0.ComboPooledDataSource">
????????"driverClass"?value="com.mysql.jdbc.Driver"/>
????????"jdbcUrl"?value="jdbc:mysql://localhost:3309/one"/>
????????"user"?value="root"/>
????????"password"?value="1234"/>
???? 到容器中獲取資源
public?void?two()?throws?SQLException?{
????????ClassPathXmlApplicationContext?classPathXmlApplicationContext?=?new???????ClassPathXmlApplicationContext("applicationcontext.xml");
????????DataSource?datasource?=?(DataSource)classPathXmlApplicationContext.getBean("datasource");
????????Connection?connection?=?datasource.getConnection();
????????System.out.println(connection);
????}
上面的方法是不是還不夠方便,我們可以用更方便的,即讀取配置文件的方法
我們首先引入命名空間與約束路徑
命名空間:xmlns:context="http://www.springframework.org/schema/context"
約束路徑:http://www.springframework.org/schema/context ???????????????????????
http://www.springframework.org/schema/context/spring-context.xsd容器加載配置文件
配置Bean
"dataSource"?class="com.mchange.v2.c3p0.ComboPooledDataSource">
????????"driverClass"?value="${jdbc.driver}">
????????"jdbcUrl"?value="${jdbc.url}">
????????"user"?value="${jdbc.username}">
????????"password"?value="${jdbc.password}">
???? 以上就是Spring配置源的一些知識,有志同道合的伙伴可以關注我或者私信我加好友一同學習章,共勉
粉絲福利:108本java從入門到大神精選電子書領取
???
?長按上方鋒哥微信二維碼?2 秒 備注「1234」即可獲取資料以及 可以進入java1234官方微信群
感謝點贊支持下哈?
