SpringBoot數(shù)據(jù)層處理方案精講
數(shù)據(jù)層解決方案
現(xiàn)有數(shù)據(jù)層解決方案技術(shù)
Druid+Mybatis-Plus+mysql
數(shù)據(jù)源:DruidDataSource
持久化技術(shù):MyBatis/MP
數(shù)據(jù)庫:MySQL
數(shù)據(jù)源配置格式
方式一
#配置相關(guān)信息
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
方式二
#配置相關(guān)信息
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC
username: root
password: 123456
當我們沒有指定數(shù)據(jù)源,導入了druid-web默認使用的是
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
當我們把這個注釋掉,就會顯示使用默認的數(shù)據(jù)源是Hikaripool

數(shù)據(jù)源配置
SpringBoot提供了3中內(nèi)嵌的數(shù)據(jù)源對象供開發(fā)者選擇
HikariCp:默認內(nèi)置數(shù)據(jù)源對象
Tomcat提供DataSource:HikariCP不可用的情況下,且在web環(huán)境中,將使用tomcat服務(wù)器配置的數(shù)據(jù)源對象
Commons DBCP:Hikari不可用,tomcat數(shù)據(jù)源也不可用,將使用dbcp數(shù)據(jù)源
內(nèi)置持久化解決方案—JdbcTemplate

得先導入坐標

JdbcTemplate配置

springboot內(nèi)置了這個JdbcTemple,寫起來比較繁瑣,不如用mybatis或MP
使用JdbcTemplate需要導入spring-boot-starter-jdbc
內(nèi)嵌數(shù)據(jù)庫
SpringBoot提供了3中內(nèi)嵌數(shù)據(jù)庫供選擇,提高開發(fā)測試效率
- H2
- HSQL
- DerBy
H2數(shù)據(jù)庫
在創(chuàng)建的時候勾選h2數(shù)據(jù)庫

pom.xml中
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>配置文件下自動會給我們寫
#remote visit
spring.h2.console.settings.web-allow-others=true
#console url。Spring啟動后,可以訪問 http://127.0.0.1:8080/h2-console 查看數(shù)據(jù)庫
spring.h2.console.path=/h2-console
#default true。咱也可以用命令行訪問好數(shù)據(jù)庫,感興趣的同學點這個鏈接 http://www.h2database.com/html/tutorial.html?highlight=Mac&search=mac#firstFound
spring.h2.console.enabled=true
spring.h2.console.settings.trace=true
#指定數(shù)據(jù)庫的種類,這里 file意思是文件型數(shù)據(jù)庫
spring.datasource.url=jdbc:h2:file:~/test
#用戶名密碼不需要改,都是臨時值
spring.datasource.username=san
spring.datasource.password=
#指定Driver,有了Driver才能訪問數(shù)據(jù)庫
spring.datasource.driver-class-name=org.h2.Driver
spring.h2.console.enabled=true為true就是開放這個圖形界面,正式上線項目時得關(guān)閉。 http://127.0.0.1:8080/h2-console 查看數(shù)據(jù)庫可以得到下圖所示。

將用戶名改為san直接點登錄即可。

隨便添加一個表
create table test(id int ,name varchar ,age int )

到此這篇關(guān)于SpringBoot數(shù)據(jù)層處理方案精講的文章就介紹到這了,更多相關(guān)SpringBoot數(shù)據(jù)層處理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis如何通過接口實現(xiàn)sql執(zhí)行原理解析
為了簡化MyBatis的使用,MyBatis提供了接口方式自動化生成調(diào)用過程,可以大大簡化MyBatis的開發(fā),下面這篇文章主要給大家介紹了關(guān)于Mybatis如何通過接口實現(xiàn)sql執(zhí)行原理解析的相關(guān)資料,需要的朋友可以參考下2023-01-01
詳解Spring Cloud Zuul中路由配置細節(jié)
本篇文章主要介紹了詳解Spring Cloud Zuul中路由配置細節(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
SpringBoot中ApplicationEvent和ApplicationListener用法小結(jié)
這篇文章介紹SpringBoot中ApplicationEvent用法,注意ApplicationEvent和MQ隊列雖然實現(xiàn)的功能相似,但是MQ還是有其不可替代性的,最本質(zhì)的區(qū)別就是MQ可以用于不同系統(tǒng)之間的消息發(fā)布,而SpringEvent這種模式只能在一個系統(tǒng)中,需要的朋友可以參考下2023-03-03

