springboot项目简单集成h2数据库

简介:H2 是一个使用 Java 编写的数据库,支持内存、文件等多种模式,经常用于项目的测试环境。

一、功能

  1. 支持全文检索,提供了内置全文检索和使用 Apache Luncene 的全文索引
  2. 对数据类型和SQL有很好的支持,兼容性好,便于移植
  3. 支持嵌入式数据库、内存数据库、只读数据库等;
  4. 能够通过浏览器操控数据库。

二、界面

大致的界面示例如下,启动项目后访问地址固定为【localhost:端口号/h2】:

springboot项目简单集成h2数据库_第1张图片

 可切换语言,这里有个注意事项是。连接使用的url、用户名、密码必须与配置文件里的一样才能正常连接成功!这是我的配置文件

server.port=8080
# h2数据库配置
spring.datasource.url=jdbc:h2:~/h2-db
spring.datasource.username=test
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver

spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
#spring.jpa.hibernate.ddl-auto=update

##h2 web console设置
spring.datasource.platform=h2
#进行该配置后,h2 web consloe就可以在远程访问了。否则只能在本机访问。
spring.h2.console.settings.web-allow-others=true
#进行该配置,你就可以通过YOUR_URL/h2访问h2 web consloe
spring.h2.console.path=/h2
#进行该配置,程序开启时就会启动h2 web consloe
spring.h2.console.enabled=true

登录成功后显示如下,可做增删改查:

springboot项目简单集成h2数据库_第2张图片

 注意事项:这里如果项目没有配置SQL文件可能登录后是没有表信息的,需要在项目的pom文件里配置需执行的SQL。如下:

springboot项目简单集成h2数据库_第3张图片

三、相关配置

 1、pom配置


    org.springframework.boot
    spring-boot-starter-parent
    2.2.1.RELEASE
     



    
        org.springframework.boot
        spring-boot-starter-web
    
    
        org.springframework.boot
        spring-boot-starter-data-jpa
    
    
        com.h2database
        h2
    

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



    
        spring-snapshots
        Spring Snapshots
        https://repo.spring.io/libs-snapshot-local
        
            true
        
    
    
        spring-milestones
        Spring Milestones
        https://repo.spring.io/libs-milestone-local
        
            false
        
    
    
        spring-releases
        Spring Releases
        https://repo.spring.io/libs-release-local
        
            false
        
    

 2、属性配置与说明

# 数据库的相关配置
spring.datasource.url=jdbc:h2:~/h2-db
spring.datasource.username=test
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver


##h2 web console设置
spring.datasource.platform=h2
#进行该配置后,h2 web consloe就可以在远程访问了。否则只能在本机访问。
spring.h2.console.settings.web-allow-others=true
#进行该配置,你就可以通过YOUR_URL/h2访问h2 web consloe
spring.h2.console.path=/h2
#进行该配置,程序开启时就会启动h2 web consloe
spring.h2.console.enabled=true
  • jdbc:h2:~/h2-db: 嵌入式使用姿势,会在用户根目录下生成一个名为h2-db.mv.db的文件(数据库的schema 和d column就存在里面)
  • jdbc:h2:mem:DBName;DB_CLOSE_DELAY=-1: 内存模式,应用重启之后数据库会清空
  • jdbc:h2:tcp://localhost/~/test:支持通过tcp方式,指定一个远程的目录

你可能感兴趣的:(日常记录,java,h2db,springboot)