1. 程式人生 > 程式設計 >h2database在springboot中的使用教程

h2database在springboot中的使用教程

h2為輕量級資料庫,使用特別方便,它可以不使用資料庫伺服器,直接嵌入到java程式中。可以配置持久化,同樣也可以不持久化(資料在記憶體中)程序結束後,資料就釋放,用做測試和演示特別方便。自帶後臺管理,非常方便,開源免費

  • 類庫,使用maven簡易安裝
  • 可以同應用程式打包在一起釋出
  • 可持久化,也可以直接基於記憶體不保留資料,適合於做單元測試

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><!--低版本,支援訪問記憶體資料庫-->
    </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 #初始化資料
#  url: jdbc:h2:mem:test  #不持久化,放在記憶體中
  url: jdbc:h2:~/test
  username: root
  password: test
 h2:
  console:
   path: /h2-console
   enabled: true #必須配置,不然無法訪問
  • 配置中提供了初始化資料庫語句schema: classpath:db/schema-h2.sql
  • 配置中提供資料初始化語句data: classpath:db/data-h2.sql
  • 當然你也可以不初始化資料和表,在程式啟動後,可以通過 localhost:8080/h2-console訪問資料庫管理後臺。通過後臺操作h2資料庫
  • 持久化與否url: jdbc:h2:mem:test代表資料放置於記憶體中,這種適合做單元測試,一次性使用
  • url: jdbc:h2:~/test 代表資料存放於 家目錄/test

啟動Springboot應用類,訪問http://localhost:8080/h2-console

就可以使用資料庫管理後臺了

h2database在springboot中的使用教程

h2database在springboot中的使用教程

h2database在springboot中的使用教程

測試查詢功能

完整程式碼參考: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

返回結果{"id":1,"name":"Jone","age":18,"email":"[email protected]"}

問題收集

jdbc連結

控制檯預設連結是jdbc:h2:~/test,如果我們使用的是記憶體jdbc:h2:mem:test,需要將連結改為jdbc:h2:mem:test

記憶體連結報錯

當我們使用jdbc:h2:mem:test連結時,報如下錯誤

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

這句話的意思是說資料庫未找到。經查詢,高版本的h2不再允許遠端訪問記憶體資料庫,可以將maven依賴新增一個低版本的

 <dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
    <version>1.4.193</version> <!--低版本,支援訪問記憶體資料庫-->
  </dependency>

初始化sql執行

  • 如果持久化到檔案,也就是url: jdbc:h2:~/test,當應用再次啟動時,初始化的sql不會再執行,並且操作後新增減的資料狀態將一直儲存
  • 如果資料庫選擇的是url: jdbc:h2:mem:test,每次啟動時,資料都會重新初始化
  • 這裡再補充一點兒前提,只有maven配置了 mybatis-spring-boot-starter 時,初始化的sql才會執行

到此這篇關於h2database在springboot中的使用教程的文章就介紹到這了,更多相關springboot使用h2database內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!