springboot测试使用H2数据库简介

H2数据库是springboot提供的内嵌数据库之一,可以提高开发过程中测试的效率。

在pom.xml中导入启动器


    org.springframework.boot
    spring-boot-starter-data-jpa
    

     com.h2database
     h2
     runtime

application.yml中添加配置

前面你虽然导入了,但是你无法访问,我们需要添加访问网址,方便管理数据库

spring:
  datasource:
#    driver-class-name: org.h2.Driver
    url: jdbc:h2:~/test
    username: sa
    password: 123456
  h2:    console:
      path: /h2
      enabled: true

启动服务器后,我们输入path的路径即可,你可以自行修改,测试完毕之后记得把enable设置为false。

进入之后需要输入用户名和密码:sa 123456,系统默认的

进去之后,就可以在输入框里面书写sql语句了。

然后就可以在测试类里面用了,它也可以和(mybatis、mybatis-plus、jdbctemplate)这些持久化框架搭配,druid,hikari,dbcp2等数据源搭配。

你可能感兴趣的:(Spring,Boot,spring,boot,数据库,java)