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

SpringBoot配置使用H2數(shù)據(jù)庫的簡單教程

 更新時間:2021年05月13日 09:53:10   作者:是小張啊  
H2是一個Java編寫的關(guān)系型數(shù)據(jù)庫,它可以被嵌入Java應(yīng)用程序中使用,或者作為一個單獨的數(shù)據(jù)庫服務(wù)器運行。本文將介紹SpringBoot如何配置使用H2數(shù)據(jù)庫

如何操作

依賴

<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>
    <optional>true</optional>
</dependency>

yml配置

server:
  # 服務(wù)端口
  port: 8090

#h2配置
#啟用SQL語句的日志記錄
spring:
  jpa:
    show-sql: true
  #設(shè)置ddl模式
    hibernate:
      ddl-auto: update
#    database-platform: org.hibernate.dialect.H2Dialect
  ##數(shù)據(jù)庫連接設(shè)置
  datasource:
    driverClassName: org.h2.Driver
  #可執(zhí)行程序的當(dāng)前路徑
    url: jdbc:h2:mem:test
  #指定的靜態(tài)配置路徑
    username: h2
    password: h2
  ##數(shù)據(jù)初始化設(shè)置
  #進行該配置后,每次啟動程序,程序都會運行resources/db/schema.sql文件,對數(shù)據(jù)庫的結(jié)構(gòu)進行操作。
    schema: classpath:db/schema.sql
  #進行該配置后,每次啟動程序,程序都會運行resources/db/data.sql文件,對數(shù)據(jù)庫的數(shù)據(jù)操作。
    data: classpath:db/data.sql
  ##h2 web console設(shè)置
  #表明使用的數(shù)據(jù)庫平臺是h2
    platform: h2
  # 進行該配置后,h2 web consloe就可以在遠程訪問了。否則只能在本機訪問。
  h2:
    console:
      settings:
        web-allow-others: true
  #進行該配置,你就可以通過YOUR_URL/h2訪問h2 web consloe。YOUR_URL是你程序的訪問URl。
      path: /h2
  #進行該配置,程序開啟時就會啟動h2 web consloe。當(dāng)然這是默認的,如果你不想在啟動程序時啟動h2 web consloe,那么就設(shè)置為false。
      enabled: true

生成內(nèi)存數(shù)據(jù)庫H2

啟動H2數(shù)據(jù)庫有兩種方式

第一種:

自動執(zhí)行.sql文件,每次重新啟動項目是,都會去運行一次操作文件

DROP TABLE IF EXISTS `student`;
CREATE TABLE `student`
(
    `id`   varchar(225),
    `name` varchar(225) ,
    `age`  int(11)
);

-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('0', '張三', 20);
INSERT INTO `student` VALUES ('1', '少杰', 20);
INSERT INTO `student` VALUES ('10', '趙子龍', NULL);

第二種

自動去創(chuàng)建數(shù)據(jù)庫,借用Spring Data Jpa的注解,啟動時自動創(chuàng)建內(nèi)存數(shù)據(jù)庫

@Entity
@Data
@Accessors(chain = true)
@Table(name = "student")
@AllArgsConstructor
@NoArgsConstructor
public class Student implements Serializable {

    /**
     * 學(xué)生id
     */
    @Id
    @Column(name = "id")
    private String id;

    /**
     * 學(xué)生名稱
     */
    @Column(name = "name")
    private String name;

    /**
     * 學(xué)生年紀
     */
    @Column(name = "age")
    private Integer age;

}

使用配置的賬號密碼進行登錄,別的不說,先跑起來最要緊

(注意:這時對數(shù)據(jù)庫中的數(shù)據(jù)進行任何的改變都將是無效的,需要對數(shù)據(jù)做持久化才能保存數(shù)據(jù))

持久化

修改yaml配置

# windows本地數(shù)據(jù)庫地址
spring.data.url: jdbc:h2:file:D:/tools/development/h2/db/student

重新啟動,發(fā)現(xiàn)多了兩個文件,分別是student.mv.dbstudent.trace.db

操作數(shù)據(jù)庫什么的,都和MySQL沒什么太大的區(qū)別,當(dāng)前用的是Spring Data Jpa那就更簡單了,非常nice

以上就是SpringBoot配置使用H2數(shù)據(jù)庫的簡單教程的詳細內(nèi)容,更多關(guān)于SpringBoot配置使用H2數(shù)據(jù)庫的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論