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服務器配置的數(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)文章
SpringBoot下使用MyBatis-Puls代碼生成器的方法
這篇文章主要介紹了SpringBoot下使用MyBatis-Puls代碼生成器的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10SpringBoot整合ZXing實現(xiàn)二維碼和條形碼的創(chuàng)建
如今我們越來越多的東西需要用到二維碼或者條形碼,商品的條形碼,付款的二維碼等等,所以本文小編給大家介紹了SpringBoot整合ZXing實現(xiàn)二維碼和條形碼的創(chuàng)建,文章通過代碼示例給大家介紹的非常詳細,需要的朋友可以參考下2023-12-12SSH框架網(wǎng)上商城項目第26戰(zhàn)之訂單支付后發(fā)送短信提醒
這篇文章主要為大家詳細介紹了SSH框架網(wǎng)上商城項目第26戰(zhàn)之訂單支付后發(fā)送短信提醒,感興趣的小伙伴們可以參考一下2016-06-06Java實現(xiàn)文件和base64流的相互轉(zhuǎn)換功能示例
這篇文章主要介紹了Java實現(xiàn)文件和base64流的相互轉(zhuǎn)換功能,涉及Java文件讀取及base64 轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下2018-05-05