spring—配置數據源

數據源(連接池)的作用

數據源(連接池)是提高程序性能如出現的
事先實例化數據源,初始化部分連接資源
使用連接資源時從數據源中獲取
使用完畢后將連接資源歸還給數據源
常見的數據源(連接池):DBCP、C3P0、BoneCP、Druid等
開發步驟
①導入數據源的坐標和數據庫驅動坐標
②創建數據源對象
③設置數據源的基本連接數據
④使用數據源獲取連接資源和歸還連接資源

數據源的手動創建

①導入c3p0

<!-- C3P0連接池 --><dependency>    <groupId>c3p0</groupId> <artifactId>c3p0</artifactId>    <version>0.9.1.2</version> </dependency>

①導入mysql數據庫驅動坐標

        <dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.11</version></dependency>

②創建C3P0連接池

    @Testpublic void testC3P0() throws Exception{ComboPooledDataSource comboPooledDataSource=new ComboPooledDataSource();comboPooledDataSource.setDriverClass("com.mysql.cj.jdbc.Driver");comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/book?useSSL=false&serverTimezone=UTC");comboPooledDataSource.setUser("root");comboPooledDataSource.setPassword("123456");Connection connection=comboPooledDataSource.getConnection();System.out.println(connection);}

提取jdbc.properties配置文件

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/book?useSSL=false&serverTimezone=UTC
jdbc.username=xxx
jdbc.password=xxx

Spring配置數據源

可以將DataSource的創建權交由Spring容器去完成 DataSource有無參構造方法,而Spring默認就是通過無參構造方法實例化對象的
DataSource要想使用需要通過set方法設置數據庫連接信息,而Spring可以通過set方法進行字符串注入

    @Testpublic void testC3P0() throws Exception{ComboPooledDataSource comboPooledDataSource=new ComboPooledDataSource();ResourceBundle resourceBundle=ResourceBundle.getBundle("jdbc");comboPooledDataSource.setDriverClass(resourceBundle.getString("jdbc.driver"));comboPooledDataSource.setJdbcUrl(resourceBundle.getString("jdbc.url"));comboPooledDataSource.setUser(resourceBundle.getString("jdbc.username"));comboPooledDataSource.setPassword(resourceBundle.getString("jdbc.password"));Connection connection=comboPooledDataSource.getConnection();System.out.println(connection);}
<?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"xmlns:p="http://www.springframework.org/schema/p"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.xsd
">
<context:property-placeholder location="jdbc.properties"/><bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driver}"/><property name="jdbcUrl" value="${jdbc.url}"/><property name="user" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/></bean></beans>
    @Testpublic void testC3P02() throws Exception{ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");ComboPooledDataSource comboPooledDataSource=(ComboPooledDataSource) applicationContext.getBean("datasource");System.out.println(comboPooledDataSource.getConnection());}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391962.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391962.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391962.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

大型網站系統與Java中間件實踐pdf

下載地址&#xff1a;網盤下載 基本介紹 編輯內容簡介 到底是本什么書&#xff0c;擁有這樣一份作序推薦人列表&#xff1a;阿里集團章文嵩博士|新浪TimYang|去哪網吳永強|丁香園馮大輝|蘑菇街岳旭強|途牛湯崢嶸|豆瓣洪強寧|某電商陳皓/林昊…… 這本書出自某電商技術部總監之手…

office漏洞利用--獲取shell

環境&#xff1a; kali系統&#xff0c; windows系統 流程&#xff1a; 在kali系統生成利用文件&#xff0c; kali系統下監聽本地端口&#xff0c; windows系統打開doc文件&#xff0c;即可中招 第一種利用方式&#xff0c; 適合測試用&#xff1a; 從git下載代碼&#xff1a; …

pandas之DataFrame合并merge

一、merge merge操作實現兩個DataFrame之間的合并&#xff0c;類似于sql兩個表之間的關聯查詢。merge的使用方法及參數解釋如下&#xff1a; pd.merge(left, right, onNone, howinner, left_onNone, right_onNone, left_indexFalse, right_indexFalse,    sortFalse, suffi…

typescript_如何掌握高級TypeScript模式

typescriptby Pierre-Antoine Mills皮埃爾安托萬米爾斯(Pierre-Antoine Mills) 如何掌握高級TypeScript模式 (How to master advanced TypeScript patterns) 了解如何為咖喱和Ramda創建類型 (Learn how to create types for curry and Ramda) Despite the popularity of curry…

html函數splice,js數組的常用函數(slice()和splice())和js引用的三種方法總結—2019年1月16日...

總結&#xff1a;slice()和splice()slice(參數1,參數2)可以查找數組下對應的數據&#xff0c;參數1為起始位置&#xff0c;參數2為結束位置&#xff0c;參數2可以為負數&#xff0c;-1對應的是從后向前數的第一個數值。splice()可以進行增刪改查數據操作&#xff0c;splice(參數…

leetcode 643. 子數組最大平均數 I(滑動窗口)

給定 n 個整數&#xff0c;找出平均數最大且長度為 k 的連續子數組&#xff0c;并輸出該最大平均數。 示例&#xff1a; 輸入&#xff1a;[1,12,-5,-6,50,3], k 4 輸出&#xff1a;12.75 解釋&#xff1a;最大平均數 (12-5-650)/4 51/4 12.75 代碼 class Solution {publ…

python ==字符串

字符串類型(str)&#xff1a; 包含在引號&#xff08;單&#xff0c;雙&#xff0c;三&#xff09;里面&#xff0c;由一串字符組成。 用途&#xff1a;姓名&#xff0c;性別&#xff0c;地址&#xff0c;學歷&#xff0c;密碼 Name ‘zbk’ 取值: 首先要明確&#xff0c;字符…

認證鑒權與API權限控制在微服務架構中的設計與實現(一)

作者&#xff1a; [Aoho’s Blog] 引言&#xff1a; 本文系《認證鑒權與API權限控制在微服務架構中的設計與實現》系列的第一篇&#xff0c;本系列預計四篇文章講解微服務下的認證鑒權與API權限控制的實現。 1. 背景 最近在做權限相關服務的開發&#xff0c;在系統微服務化后&a…

mac下完全卸載程序的方法

在國外網上看到的&#xff0c;覺得很好&#xff0c;不僅可以長卸載的知識&#xff0c;還對mac系統有更深的認識。比如偏好設置文件&#xff0c;我以前設置一個程序壞了&#xff0c;打不開了&#xff0c;怎么重裝都打不開&#xff0c;后來才知道系統還保留著原來的偏好設置文件。…

機器學習集群_機器學習中的多合一集群技術在無監督學習中應該了解

機器學習集群Clustering algorithms are a powerful technique for machine learning on unsupervised data. The most common algorithms in machine learning are hierarchical clustering and K-Means clustering. These two algorithms are incredibly powerful when appli…

自考本科計算機要學什么,計算機自考本科需要考哪些科目

高科技發展時代&#xff0c;怎離得開計算機技術&#xff1f;小學生都要學編程了&#xff0c;未來趨勢一目了然&#xff0c;所以如今在考慮提升學歷的社會成人&#xff0c;多半也青睞于計算機專業&#xff0c;那么計算機自考本科需要考哪些科目&#xff1f;難不難&#xff1f;自…

審查指南 最新版本_代碼審查-最終指南

審查指南 最新版本by Assaf Elovic通過阿薩夫埃洛維奇 代碼審查-最終指南 (Code Review — The Ultimate Guide) 構建團隊代碼審查流程的終極指南 (The ultimate guide for building your team’s code review process) After conducting hundreds of code reviews, leading R…

非對稱加密

2019獨角獸企業重金招聘Python工程師標準>>> 概念 非對稱加密算法需要兩個密鑰&#xff1a;公鑰&#xff08;publickey&#xff09;和私鑰&#xff08;privatekey&#xff09;。公鑰與私鑰是一對&#xff0c;如果用公鑰對數據進行加密&#xff0c;只有用對應的私…

管理Sass項目文件結構

http://www.w3cplus.com/preprocessor/architecture-sass-project.html 編輯推薦&#xff1a; 掘金是一個高質量的技術社區&#xff0c;從 CSS 到 Vue.js&#xff0c;性能優化到開源類庫&#xff0c;讓你不錯過前端開發的每一個技術干貨。 點擊鏈接查看最新前端內容&#xff0c…

Spring—注解開發

Spring原始注解 Spring是輕代碼而重配置的框架&#xff0c;配置比較繁重&#xff0c;影響開發效率&#xff0c;所以注解開發是一種趨勢&#xff0c;注解代替xml配置文 件可以簡化配置&#xff0c;提高開發效率。 Component 使用在類上用于實例化BeanController 使用在web層類…

政府公開數據可視化_公開演講如何幫助您設計更好的數據可視化

政府公開數據可視化What do good speeches and good data visualisation have in common? More than you may think.好的演講和好的數據可視化有什么共同點&#xff1f; 超出您的想象。 Aristotle — the founding father of all things public speaking — believed that th…

C++字符串完全指引之一 —— Win32 字符編碼 (轉載)

C字符串完全指引之一 —— Win32 字符編碼原著&#xff1a;Michael Dunn翻譯&#xff1a;Chengjie Sun 原文出處&#xff1a;CodeProject&#xff1a;The Complete Guide to C Strings, Part I 引言  毫無疑問&#xff0c;我們都看到過像 TCHAR, std::string, BSTR 等各種各樣…

網絡計算機無法訪問 請檢查,局域網電腦無法訪問,請檢查來賓訪問帳號是否開通...

局域網電腦無法訪問&#xff0c;有時候并不是由于網絡故障引起的&#xff0c;而是因為自身電腦的一些設置問題&#xff0c;例如之前談過的網絡參數設置不對造成局域網電腦無法訪問。今天分析另一個電腦設置的因素&#xff0c;它也會導致局域網電腦無法訪問&#xff0c;那就是賓…

unity中創建游戲場景_在Unity中創建Beat Em Up游戲

unity中創建游戲場景Learn how to use Unity to create a 3D Beat Em Up game in this full tutorial from Awesome Tuts. 在Awesome Tuts的完整教程中&#xff0c;了解如何使用Unity來創建3D Beat Em Up游戲。 This tutorial covers everything you need to know to make a …

雷軍的金山云D輪獲3億美元!投后估值達19億美金

12月12日&#xff0c;雷軍旗下金山云宣布D輪完成3億美元融資&#xff0c;金額為云行業單輪融資最高。至此金山云投后估值達到19億美元&#xff0c;成為國內估值最高的獨立云服務商。金山集團相關公告顯示&#xff0c;金山云在本輪融資中總計發行3.535億股D系列優先股。驪悅投資…