日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring—配置数据源

發(fā)布時間:2023/11/29 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring—配置数据源 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

數(shù)據(jù)源(連接池)的作用

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

數(shù)據(jù)源的手動創(chuàng)建

①導(dǎo)入c3p0

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

①導(dǎo)入mysql數(shù)據(jù)庫驅(qū)動坐標(biāo)

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

②創(chuàng)建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配置數(shù)據(jù)源

可以將DataSource的創(chuàng)建權(quán)交由Spring容器去完成 DataSource有無參構(gòu)造方法,而Spring默認(rèn)就是通過無參構(gòu)造方法實例化對象的
DataSource要想使用需要通過set方法設(shè)置數(shù)據(jù)庫連接信息,而Spring可以通過set方法進(jìn)行字符串注入

@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());}

總結(jié)

以上是生活随笔為你收集整理的spring—配置数据源的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。