欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

h2database在springboot中的使用教程

 更新時間:2020年10月14日 08:30:48   作者:夢想家haima  
這篇文章主要介紹了h2database在springboot中的使用,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

h2為輕量級數(shù)據(jù)庫,使用特別方便,它可以不使用數(shù)據(jù)庫服務(wù)器,直接嵌入到j(luò)ava程序中。可以配置持久化,同樣也可以不持久化(數(shù)據(jù)在內(nèi)存中)進程結(jié)束后,數(shù)據(jù)就釋放,用做測試和演示特別方便。自帶后臺管理,非常方便,開源免費

  • 類庫,使用maven簡易安裝
  • 可以同應(yīng)用程序打包在一起發(fā)布
  • 可持久化,也可以直接基于內(nèi)存不保留數(shù)據(jù),適合于做單元測試

maven依賴

<dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>

    <dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
      <scope>runtime</scope>
      <version>1.4.193</version><!--低版本,支持訪問內(nèi)存數(shù)據(jù)庫-->
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>2.1.1</version>
    </dependency>
</dependencies>

application.yml配置

server:
 port: 8080

spring:
 datasource:
  driver-class-name: org.h2.Driver
#  schema: classpath:db/schema-h2.sql #初始化建表
#  data: classpath:db/data-h2.sql #初始化數(shù)據(jù)
#  url: jdbc:h2:mem:test  #不持久化,放在內(nèi)存中
  url: jdbc:h2:~/test
  username: root
  password: test
 h2:
  console:
   path: /h2-console
   enabled: true #必須配置,不然無法訪問
  • 配置中提供了初始化數(shù)據(jù)庫語句schema: classpath:db/schema-h2.sql
  • 配置中提供數(shù)據(jù)初始化語句data: classpath:db/data-h2.sql
  • 當(dāng)然你也可以不初始化數(shù)據(jù)和表,在程序啟動后,可以通過 localhost:8080/h2-console訪問數(shù)據(jù)庫管理后臺。通過后臺操作h2數(shù)據(jù)庫
  • 持久化與否url: jdbc:h2:mem:test代表數(shù)據(jù)放置于內(nèi)存中,這種適合做單元測試,一次性使用
  • url: jdbc:h2:~/test 代表數(shù)據(jù)存放于 家目錄/test

啟動Springboot應(yīng)用類,訪問http://localhost:8080/h2-console就可以使用數(shù)據(jù)庫管理后臺了

測試查詢功能

完整代碼參考:https://gitee.com/haimama/java-study/tree/master/h2db-demo-simple

Application.java

package demosimple.h2;


import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("demosimple.h2.mapper")
public class Application {

  public static void main(String[] args) {
    SpringApplication.run(Application.class,args);
  }
}

TestController.java

package demosimple.h2.controller;

import demosimple.h2.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

  @Autowired
  UserMapper userMapper;

  @GetMapping("/test")
  public Object test(){
    return userMapper.getById(1L);
  }
}

UserMapper.java

package demosimple.h2.mapper;

import demosimple.h2.pojo.User;
import org.apache.ibatis.annotations.Select;

public interface UserMapper {
  @Select("select * from user where id=#{id}")
  public User getById(Long id);
}

User.java

package demosimple.h2.pojo;

import lombok.Data;

@Data
public class User {
  private Long id;
  private String name;
  private Integer age;
  private String email;
}

訪問http://localhost:8080/test

返回結(jié)果{"id":1,"name":"Jone","age":18,"email":"test1@baomidou.com"}

問題收集

jdbc鏈接

控制臺默認鏈接是jdbc:h2:~/test,如果我們使用的是內(nèi)存jdbc:h2:mem:test,需要將鏈接改為jdbc:h2:mem:test

內(nèi)存鏈接報錯

當(dāng)我們使用jdbc:h2:mem:test鏈接時,報如下錯誤

Database "mem:test" not found, and IFEXISTS=true, so we cant auto-create it [90146-199] 90146/90146 (Help)

這句話的意思是說數(shù)據(jù)庫未找到。經(jīng)查詢,高版本的h2不再允許遠程訪問內(nèi)存數(shù)據(jù)庫,可以將maven依賴添加一個低版本的

 <dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
    <version>1.4.193</version> <!--低版本,支持訪問內(nèi)存數(shù)據(jù)庫-->
  </dependency>

初始化sql執(zhí)行

  • 如果持久化到文件,也就是url: jdbc:h2:~/test,當(dāng)應(yīng)用再次啟動時,初始化的sql不會再執(zhí)行,并且操作后新增減的數(shù)據(jù)狀態(tài)將一直保存
  • 如果數(shù)據(jù)庫選擇的是url: jdbc:h2:mem:test,每次啟動時,數(shù)據(jù)都會重新初始化
  • 這里再補充一點兒前提,只有maven配置了 mybatis-spring-boot-starter 時,初始化的sql才會執(zhí)行

到此這篇關(guān)于h2database在springboot中的使用教程的文章就介紹到這了,更多相關(guān)springboot使用h2database內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • IntelliJ IDEA中使用mybatis-generator的示例

    IntelliJ IDEA中使用mybatis-generator的示例

    這篇文章主要介紹了IntelliJ IDEA中使用mybatis-generator,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • jsp+dao+bean+servlet(MVC模式)實現(xiàn)簡單用戶登錄和注冊頁面

    jsp+dao+bean+servlet(MVC模式)實現(xiàn)簡單用戶登錄和注冊頁面

    這篇文章主要介紹了jsp+dao+bean+servlet(MVC模式)實現(xiàn)簡單用戶登錄和注冊頁面,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • 淺談java安全編碼指南之死鎖dead lock

    淺談java安全編碼指南之死鎖dead lock

    java中為了保證共享數(shù)據(jù)的安全性,我們引入了鎖的機制。有了鎖就有可能產(chǎn)生死鎖。死鎖的原因就是多個線程鎖住了對方所需要的資源,然后現(xiàn)有的資源又沒有釋放,從而導(dǎo)致循環(huán)等待的情況。通常來說如果不同的線程對加鎖和釋放鎖的順序不一致的話,就很有可能產(chǎn)生死鎖。
    2021-06-06
  • mybatis中使用InsertProvider注解報錯解決全過程

    mybatis中使用InsertProvider注解報錯解決全過程

    這篇文章主要介紹了mybatis中使用InsertProvider注解報錯解決全過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • mybatis 延遲加載的深入理解

    mybatis 延遲加載的深入理解

    這篇文章主要介紹了mybatis 延遲加載的深入理解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • Maven如何解決添加依賴之后沒有加載jar包報錯問題

    Maven如何解決添加依賴之后沒有加載jar包報錯問題

    這篇文章主要介紹了Maven如何解決添加依賴之后沒有加載jar包報錯問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • mybatis入門_動力節(jié)點Java學(xué)院整理

    mybatis入門_動力節(jié)點Java學(xué)院整理

    這篇文章主要為大家詳細介紹了mybatis入門的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • SpringBoot如何手寫一個starter并使用這個starter詳解

    SpringBoot如何手寫一個starter并使用這個starter詳解

    starter是SpringBoot中的一個新發(fā)明,它有效的降低了項目開發(fā)過程的復(fù)雜程度,對于簡化開發(fā)操作有著非常好的效果,下面這篇文章主要給大家介紹了關(guān)于SpringBoot如何手寫一個starter并使用這個starter的相關(guān)資料,需要的朋友可以參考下
    2022-12-12
  • SpringMVC實現(xiàn)文件的上傳和下載實例代碼

    SpringMVC實現(xiàn)文件的上傳和下載實例代碼

    本篇文章主要介紹了SpringMVC實現(xiàn)文件的上傳和下載實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • javaWeb如何實現(xiàn)隨機圖片驗證碼詳解

    javaWeb如何實現(xiàn)隨機圖片驗證碼詳解

    這篇文章主要給大家介紹了關(guān)于javaWeb如何實現(xiàn)隨機圖片驗證碼的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03

最新評論