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

詳解IDEA中便捷內(nèi)存數(shù)據(jù)庫H2的最簡使用方式

 更新時間:2021年03月07日 09:47:05   作者:歲月已走遠  
這篇文章主要介紹了詳解IDEA中便捷內(nèi)存數(shù)據(jù)庫H2的最簡使用方式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

  在IDEA中做練習(xí)或做demo時,有時候需要使用到數(shù)據(jù)庫,但如果自己機子上本來沒有安裝數(shù)據(jù)庫(全新安裝太麻煩或資源有限),也沒有可用的遠程數(shù)據(jù)庫時,我們可以直接在IDEA上使用便捷式的內(nèi)存數(shù)據(jù)庫H2,關(guān)于H2更多知識就自己去找一下資料了,接下來主要講一下如何在IDEA下的SpringBoot項目中把它用起來!

  首先在IDEA中新建一個空的Maven項目,編輯項目pom.xml文件,增加SpringBoot和最基礎(chǔ)的數(shù)據(jù)訪問相關(guān)jar包依賴:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>microservice-simple-provider-user</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <parent>
    <artifactId>spring-boot-starter-parent</artifactId>
    <groupId>org.springframework.boot</groupId>
    <version>2.4.2</version>
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
    <dependency>
      <!--SpringBoot的Web項目起步依賴-->
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <version>2.4.2</version>
    </dependency>
    <dependency>
      <!--SpringBoot的jpa數(shù)據(jù)訪問組件起步依賴-->
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
      <version>2.3.1.RELEASE</version>
    </dependency>
    <dependency>
      <!--自動構(gòu)建H2嵌入式或內(nèi)存數(shù)據(jù)庫的依賴-->
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
      <version>1.4.200</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>2.4.2</version>
      </plugin>
    </plugins>
  </build>
</project>

  然后在classpath路徑范圍內(nèi)增加建表SQL腳本文件和數(shù)據(jù)初始化文件:

  接著再添加SpringBoot項目配置文件application.yml,并添加jpa、datasource和h2的主要配置項:

  最后再為項目添加一個SpringBoot啟動類,即可將應(yīng)用跑起來,并進行H2內(nèi)存數(shù)據(jù)庫的連接了:

  注意:自動創(chuàng)建的H2內(nèi)存數(shù)據(jù)庫的管理地址就是我們在application.yml中配置的h2.console.path地址,進入管理頁后如下圖所示:

  這樣我們的應(yīng)用就相當(dāng)于自帶了一個便捷式內(nèi)存數(shù)據(jù)庫了!

  現(xiàn)在我們來為項目做一個最簡單的H2內(nèi)存數(shù)據(jù)庫的使用樣例——為H2數(shù)據(jù)庫中的User表增加對應(yīng)的實體類User.java、基于JPA的數(shù)據(jù)訪問對象UserRepository.java和API層控制類UserController.java:

User.java類:

package com.example.models;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import javax.persistence.*;
import java.math.BigDecimal;

@JsonIgnoreProperties(value = {"hibernateLazyInitializer"})
@Entity
public class User {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;

  @Column
  private String username;

  @Column
  private String name;

  @Column
  private Integer age;

  @Column
  private BigDecimal balance;

  public Long getId() {
    return id;
  }

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

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getName() {
    return name;
  }

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

  public Integer getAge() {
    return age;
  }

  public void setAge(Integer age) {
    this.age = age;
  }

  public BigDecimal getBalance() {
    return balance;
  }

  public void setBalance(BigDecimal balance) {
    this.balance = balance;
  }
}

UserRepository.java類:

package com.example.dao;

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

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}

UserController.java類:

package com.example.controller;

import com.example.dao.UserRepository;
import com.example.models.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
  @Autowired
  private UserRepository userRepository;

  @GetMapping("/{id}")
  public User findById(@PathVariable Long id) {
    User oneUser = this.userRepository.getOne(id);
    return oneUser;
  }
}

  最后的項目結(jié)構(gòu)如圖所示:

  來看一下效果:

到此這篇關(guān)于詳解IDEA中便捷內(nèi)存數(shù)據(jù)庫H2的最簡使用方式的文章就介紹到這了,更多相關(guān)IDEA內(nèi)存數(shù)據(jù)庫H2內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論