亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot數(shù)據(jù)層處理方案精講

 更新時間:2022年10月22日 14:15:17   作者:執(zhí)久呀  
這篇文章主要介紹了SpringBoot數(shù)據(jù)層技術(shù)的解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧

數(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)文章

最新評論