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

歡迎訪問 生活随笔!

生活随笔

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

javascript

Spring Boot 集成数据库

發布時間:2024/9/30 javascript 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot 集成数据库 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

(一)操作數據庫

1.在pom.xml中引入相關的依賴

<!-- 添加對jdbc 的依賴 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><!-- 添加數據庫驅動 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency>

2.在application.properties 全局配置文件中設置數據庫的相關配置

spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/springboot spring.datasource.username=root spring.datasource.password=1234

3.測試代碼(入口類)

/** 入口類 */ @SpringBootApplication(scanBasePackages = "com.jas") public class SpringbootApplication { public static void main(String[] args) throws Exception{ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args);System.out.println(context.getBean(DataSource.class).getClass());System.out.println(context.getBean(DataSource.class).getConnection());} }

控制臺輸出

class org.apache.tomcat.jdbc.pool.DataSource ProxyConnection[PooledConnection[com.mysql.jdbc.JDBC4Connection@511505e7]]

從上面的輸出可以看出Spring Boot 默認使用的是tomcat 的數據源。

(二)自定義數據源

這里我們以c3p0 數據源為例,有兩種配置數據源的方式,分別是:在全局配置文件中進行設置與使用配置類。

2.1在全局配置文件中配置

1.首先在pom.xml 中引入c3p0 的依賴

<!-- 添加c3p0 數據源的依賴 --><dependency><groupId>c3p0</groupId><artifactId>c3p0</artifactId><version>0.9.1.2</version></dependency>

2.在application.properties 中配置數據庫信息與c3p0 數據源

spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/springboot spring.datasource.username=root spring.datasource.password=1234spring.datasource.type=com.mchange.v2.c3p0.ComboPooledDataSource

啟動入口類控制臺輸出

class com.mchange.v2.c3p0.ComboPooledDataSource com.mchange.v2.c3p0.impl.NewProxyConnection@64d43929

2.2使用配置類

1.在pom.xml 中引入c3p0 的依賴
2.在application.properties 中配置數據庫信息,也可以寫一個配置文件單獨封裝這些信息

spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/springboot spring.datasource.username=root spring.datasource.password=1234

3.配置類

@SpringBootConfiguration public class DatasourceConfiguration {/*** environment 用于讀取`application.properties` 配置文件中的配置信息*/@Autowiredprivate Environment environment;@Beanpublic DataSource createDataSource() throws Exception {ComboPooledDataSource dataSource = new ComboPooledDataSource();dataSource.setDriverClass(environment.getProperty("spring.datasource.driver-class-name"));dataSource.setJdbcUrl(environment.getProperty("spring.datasource.url"));dataSource.setUser(environment.getProperty("spring.datasource.username"));dataSource.setPassword(environment.getProperty("spring.datasource.password"));return dataSource;} }

(三)事務管理

在Spring Boot 中使用事務,需要在入口類中使用@EnableTransactionManagement注解啟用事務。
對需要進行事務管理的方法加上@Transactional注解。

PS:需要注意的是 @Transactional 注解默認只會對運行時異常(RuntimeException)起作用,對于I/O 等其他異常不起作用。對于其他方式的異常,Spring Boot 在@Transactional 注解中提供了rollbackFor與noRollbackFor 選項,可以讓我們自定義對異常的回滾。

/*** rollbackFor 設置回滾的異常,Exception 可以回滾所有的異常,* noRollbackFor 設置不回滾事務的異常*/`@Transactional `(rollbackFor = Exception.class, noRollbackFor = NullPointerException.class )

另一個地方需要注意的是:在同一個類中一個沒有設置注解的方法調用有注解的方法時,事務不生效。

總結

以上是生活随笔為你收集整理的Spring Boot 集成数据库的全部內容,希望文章能夠幫你解決所遇到的問題。

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