1. 程式人生 > 程式設計 >Spring boot2.x中整合H2資料庫程式碼例項

Spring boot2.x中整合H2資料庫程式碼例項

這篇文章主要介紹了Spring boot2.x中整合H2資料庫程式碼例項,文中通過示例程式碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

在spring boot中整合

1.新增依賴

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

<dependency>
  <groupId>com.h2database</groupId>
  <artifactId>h2</artifactId>
  <scope>runtime</scope>
</dependency>

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

2.新增H2相關配置,修改application.properties檔案

spring.jpa.database=h2
spring.jpa.show-sql=true
#ddl執行方式,update create 等

spring.datasource.url=jdbc:h2:./data/test;AUTO_SERVER=TRUE
spring.jpa.hibernate.ddl-auto=update
spring.datasource.username=sa
spring.datasource.password=123456
spring.datasource.driverClassName=org.h2.Driver

spring.h2.console.path=/h2-console
spring.h2.console.enabled=true

說明:

spring.datasource.url

資料庫檔案

(1)記憶體資料庫

jdbc:h2:mem:DBName

記憶體資料庫的資料存在記憶體中,當程式停止時,不會被儲存會丟失

eg:

spring.datasource.url=jdbc:h2:mem:test

(2)檔案資料庫

jdbc:h2:file:{FilePath} 也可以簡化為 jdbc:h2:{FilePath}

FilePath的格式

  • a) ./{path}/{fileName} 在當前程式的根目錄下建立目錄和資料庫檔案
  • b) ~/{path}/{fileName} 在當前使用者的根目錄下建立目錄和資料庫檔案
  • c) C:/{path}/{fileName} 在指定碟符的指定目錄下建立資料庫檔案

(3)遠端資料庫

jdbc:h2:tcp://<{IP|hostname}>[:{Port}]/[]<{dbName}>

附加引數:

  • AUTO_SERVER=TRUE 啟動自動混合模式,允許開啟多個連線,該引數不支援在記憶體中執行模式
  • DB_CLOSE_ON_EXIT=FALSE,當虛擬機器退出時並不關閉資料庫

3.程式碼

domain層,即User類(entity)

package com.example.demo.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;

@Entity
@Table(name = "user")
@Data
public class User {
  @Id
  @GeneratedValue(strategy= GenerationType.AUTO)
  private int id;
  private String name;

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}

dao層,即UserRepository 介面

package com.example.demo.dao;

import com.example.demo.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserRepository extends JpaRepository<User,Integer> {
  List<User> getUsersByName(String Name);
}

controller層,即Demo

package com.example.demo.controller;

import com.example.demo.dao.UserRepository;
import com.example.demo.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class Demo {
  @Autowired
  private UserRepository repo;

  @RequestMapping("find")
  public List<User> find() {
    return (List<User>) repo.findAll();
  }
}

編寫DemoApplication

package com.example.demo;

import com.example.demo.dao.UserRepository;
import com.example.demo.domain.User;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class DemoApplication {

  @Bean
  InitializingBean saveData(UserRepository repo){

    return ()->{
      User u = new User();
      u.setName("abc");
      repo.save(u);
      User u1 = new User();
      u1.setName("zyx");
      repo.save(u1);

    };
  }

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

}

啟動專案,開啟瀏覽器訪問http://localhost:8080/find

訪問http://localhost:8080/h2-console/

連線上後查詢資料

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。