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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Boot文档阅读笔记-DataSource configuration

發布時間:2025/3/15 javascript 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot文档阅读笔记-DataSource configuration 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

DataSource:一個工廠可以連接任意廠家的數據庫。通常使用URL以及一些認證去建立數據庫連接。

DataSource在代碼中是一個對象,這個對象貫徹并落實了javax.sql.DataSource接口中注冊JNDI服務,并且這個對象能發現并使用JNDI的名稱。

DataSource可以用來獲取:

? ? a. 標志Connection對象;

? ? b. 使用了連接池的connection;

? ? c. 使用了事務及連接池的connection;

?

下面是DataSource的配置

SpringBoot中可以通過兩種方式配置DataSource,一種是在Java代碼中,一種是在properties文件中。DataSourceAutoConfiguration檢測classpath下的DataSource.class或EmbeddedDatabaseType.class需要配置DataSource的bean。

maven中需要的配置:

以H2 db為例:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId> </dependency><dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><version>2.4.1</version> <scope>runtime</scope> </dependency>

application.properties中的配置

在application.properties可以直接配置DataSource,配置相關屬性為spring.datasource.*。

這種配置方式不需要在程序中敲代碼。

下面給出關于H2,MySQL,Oracle,SQL server相關的配置。

提示:driver-class-name不是必須的,SpringBoot會通過databases的url進行推算,得出其driver-class-name

# H2 spring.datasource.url=jdbc:h2:file:C:/temp/test spring.datasource.username=sa spring.datasource.password= spring.datasource.driverClassName=org.h2.Driver spring.jpa.database-platform=org.hibernate.dialect.H2Dialect# MySQL #spring.datasource.url=jdbc:mysql://localhost:3306/test #spring.datasource.username=dbuser #spring.datasource.password=dbpass #spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect# Oracle #spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl #spring.datasource.username=dbuser #spring.datasource.password=dbpass #spring.datasource.driver-class-name=oracle.jdbc.OracleDriver #spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect# SQL Server #spring.datasource.url=jdbc:sqlserver://localhost;databaseName=springbootdb #spring.datasource.username=dbuser #spring.datasource.password=dbpass #spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver #spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect

DataSource Bean

推薦使用DataSourceBuilder用于建立DataSource。代碼如下:

@Configuration public class JpaConfig {@Beanpublic DataSource getDataSource() {DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();dataSourceBuilder.driverClassName("org.h2.Driver");dataSourceBuilder.url("jdbc:h2:file:C:/temp/test");dataSourceBuilder.username("sa");dataSourceBuilder.password("");return dataSourceBuilder.build();} }

JNDI DataSource

如果將SpringBoot部署成應用服務,可以通過使用應用服務的內置特性配置和管理數據源,并使用JNDI訪問數據源。

如下:

#JBoss defined datasource using JNDIspring.datasource.jndi-name = java:jboss/datasources/testDB

Connection pooling

在配置文件中通過調整特定的前綴進行配置spring.datasource.hikari.*或spring.datasource.tomcat.*或spring.datasource.dbcp2.*

如下配置:

spring.datasource.dbcp2.initial-size = 50 spring.datasource.dbcp2.max-idle = 50 spring.datasource.dbcp2.default-query-timeout = 10000 spring.datasource.dbcp2.default-auto-commit = true...

SpringBoot配置Multiple DataSources

配置多個數據源,需要配置多個bean,其中一個要帶上@Primary。這里要注意,如果自己配置數據源,那么自動配置將會停止。代碼如下:

@Configuration public class JpaConfig {@Bean(name = "h2DataSource")public DataSource h2DataSource() {DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();dataSourceBuilder.driverClassName("org.h2.Driver");dataSourceBuilder.url("jdbc:h2:file:C:/temp/test");dataSourceBuilder.username("sa");dataSourceBuilder.password("");return dataSourceBuilder.build();}@Bean(name = "mySqlDataSource")@Primarypublic DataSource mySqlDataSource() {DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();dataSourceBuilder.url("jdbc:mysql://localhost/testdb");dataSourceBuilder.username("dbuser");dataSourceBuilder.password("dbpass");return dataSourceBuilder.build();} }

使用的時候帶上@Qualifier

@Autowired @Qualifier("h2DataSource") DataSource dataSource;

?

總結

以上是生活随笔為你收集整理的Spring Boot文档阅读笔记-DataSource configuration的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。