SpringBoot數據層處理方案精講
數據層解決方案
現有數據層解決方案技術
Druid+Mybatis-Plus+mysql
數據源:DruidDataSource
持久化技術:MyBatis/MP
數據庫:MySQL
數據源配置格式
方式一
#配置相關信息
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
方式二
#配置相關信息
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC
username: root
password: 123456
當我們沒有指定數據源,導入了druid-web默認使用的是
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.6</version> </dependency>
當我們把這個注釋掉,就會顯示使用默認的數據源是Hikaripool
數據源配置
SpringBoot提供了3中內嵌的數據源對象供開發(fā)者選擇
HikariCp:默認內置數據源對象
Tomcat提供DataSource:HikariCP不可用的情況下,且在web環(huán)境中,將使用tomcat服務器配置的數據源對象
Commons DBCP:Hikari不可用,tomcat數據源也不可用,將使用dbcp數據源
內置持久化解決方案—JdbcTemplate
得先導入坐標
JdbcTemplate配置
springboot內置了這個JdbcTemple,寫起來比較繁瑣,不如用mybatis或MP
使用JdbcTemplate需要導入spring-boot-starter-jdbc
內嵌數據庫
SpringBoot提供了3中內嵌數據庫供選擇,提高開發(fā)測試效率
- H2
- HSQL
- DerBy
H2數據庫
在創(chuàng)建的時候勾選h2數據庫
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 查看數據庫
spring.h2.console.path=/h2-console
#default true。咱也可以用命令行訪問好數據庫,感興趣的同學點這個鏈接 http://www.h2database.com/html/tutorial.html?highlight=Mac&search=mac#firstFound
spring.h2.console.enabled=true
spring.h2.console.settings.trace=true
#指定數據庫的種類,這里 file意思是文件型數據庫
spring.datasource.url=jdbc:h2:file:~/test
#用戶名密碼不需要改,都是臨時值
spring.datasource.username=san
spring.datasource.password=
#指定Driver,有了Driver才能訪問數據庫
spring.datasource.driver-class-name=org.h2.Driver
spring.h2.console.enabled=true為true就是開放這個圖形界面,正式上線項目時得關閉。 http://127.0.0.1:8080/h2-console 查看數據庫可以得到下圖所示。
將用戶名改為san直接點登錄即可。
隨便添加一個表
create table test(id int ,name varchar ,age int )
到此這篇關于SpringBoot數據層處理方案精講的文章就介紹到這了,更多相關SpringBoot數據層處理內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot下使用MyBatis-Puls代碼生成器的方法
這篇文章主要介紹了SpringBoot下使用MyBatis-Puls代碼生成器的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10SpringBoot整合ZXing實現二維碼和條形碼的創(chuàng)建
如今我們越來越多的東西需要用到二維碼或者條形碼,商品的條形碼,付款的二維碼等等,所以本文小編給大家介紹了SpringBoot整合ZXing實現二維碼和條形碼的創(chuàng)建,文章通過代碼示例給大家介紹的非常詳細,需要的朋友可以參考下2023-12-12SSH框架網上商城項目第26戰(zhàn)之訂單支付后發(fā)送短信提醒
這篇文章主要為大家詳細介紹了SSH框架網上商城項目第26戰(zhàn)之訂單支付后發(fā)送短信提醒,感興趣的小伙伴們可以參考一下2016-06-06