SpringBoot數(shù)據(jù)層處理方案精講
數(shù)據(jù)層解決方案
現(xiàn)有數(shù)據(jù)層解決方案技術(shù)
Druid+Mybatis-Plus+mysql
數(shù)據(jù)源:DruidDataSource
持久化技術(shù):MyBatis/MP
數(shù)據(jù)庫(kù):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
當(dāng)我們沒(méi)有指定數(shù)據(jù)源,導(dǎo)入了druid-web默認(rèn)使用的是
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
當(dāng)我們把這個(gè)注釋掉,就會(huì)顯示使用默認(rèn)的數(shù)據(jù)源是Hikaripool

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

得先導(dǎo)入坐標(biāo)

JdbcTemplate配置

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

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>配置文件下自動(dòng)會(huì)給我們寫
#remote visit
spring.h2.console.settings.web-allow-others=true
#console url。Spring啟動(dòng)后,可以訪問(wèn) http://127.0.0.1:8080/h2-console 查看數(shù)據(jù)庫(kù)
spring.h2.console.path=/h2-console
#default true。咱也可以用命令行訪問(wèn)好數(shù)據(jù)庫(kù),感興趣的同學(xué)點(diǎn)這個(gè)鏈接 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ù)庫(kù)的種類,這里 file意思是文件型數(shù)據(jù)庫(kù)
spring.datasource.url=jdbc:h2:file:~/test
#用戶名密碼不需要改,都是臨時(shí)值
spring.datasource.username=san
spring.datasource.password=
#指定Driver,有了Driver才能訪問(wèn)數(shù)據(jù)庫(kù)
spring.datasource.driver-class-name=org.h2.Driver
spring.h2.console.enabled=true為true就是開放這個(gè)圖形界面,正式上線項(xiàng)目時(shí)得關(guān)閉。 http://127.0.0.1:8080/h2-console 查看數(shù)據(jù)庫(kù)可以得到下圖所示。

將用戶名改為san直接點(diǎn)登錄即可。

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

到此這篇關(guān)于SpringBoot數(shù)據(jù)層處理方案精講的文章就介紹到這了,更多相關(guān)SpringBoot數(shù)據(jù)層處理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis中常見(jiàn)的SQL執(zhí)行方式及其使用方法
MyBatis可能很多人都一直在用,但是MyBatis的SQL執(zhí)行流程可能并不是所有人都清楚了,下面這篇文章主要給大家介紹了關(guān)于MyBatis中常見(jiàn)的SQL執(zhí)行方式及其使用的相關(guān)資料,需要的朋友可以參考下2023-09-09
Mybatis如何通過(guò)接口實(shí)現(xiàn)sql執(zhí)行原理解析
為了簡(jiǎn)化MyBatis的使用,MyBatis提供了接口方式自動(dòng)化生成調(diào)用過(guò)程,可以大大簡(jiǎn)化MyBatis的開發(fā),下面這篇文章主要給大家介紹了關(guān)于Mybatis如何通過(guò)接口實(shí)現(xiàn)sql執(zhí)行原理解析的相關(guān)資料,需要的朋友可以參考下2023-01-01
Java如何通過(guò)File類方法刪除指定文件夾中的全部文件
這篇文章主要給大家介紹了關(guān)于Java如何通過(guò)File類方法刪除指定文件夾中的全部文件的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
java關(guān)于list集合做刪除操作時(shí)的坑及解決
這篇文章主要介紹了java關(guān)于list集合做刪除操作時(shí)的坑及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
mvn compile報(bào)錯(cuò)“程序包c(diǎn)om.XXX不存在”
本文主要介紹了mvn compile報(bào)錯(cuò)“程序包c(diǎn)om.XXX不存在”,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
詳解Spring Cloud Zuul中路由配置細(xì)節(jié)
本篇文章主要介紹了詳解Spring Cloud Zuul中路由配置細(xì)節(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
SpringBoot中ApplicationEvent和ApplicationListener用法小結(jié)
這篇文章介紹SpringBoot中ApplicationEvent用法,注意ApplicationEvent和MQ隊(duì)列雖然實(shí)現(xiàn)的功能相似,但是MQ還是有其不可替代性的,最本質(zhì)的區(qū)別就是MQ可以用于不同系統(tǒng)之間的消息發(fā)布,而SpringEvent這種模式只能在一個(gè)系統(tǒng)中,需要的朋友可以參考下2023-03-03

