首页 > 技术文章 > spring boot h2 数据库

lishuaiqi 2020-04-05 14:49 原文

1. 就和tomcat内嵌到springboot中, springboot也提供了一种内嵌数据库,可以让我们在开发过程中无需配置MySql数据库就可以工作.

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

直接引入H2数据库即可.无需配置用户名和密码就可以直接工作.

2.H2 数据库管理界面

直接在地址中输入  http://localhost:8080/h2-console/

也就是在地址中加入 h2-console 就可以看到h2 数据库的管理界面

 

 

Deive Class : org.h2.Driver

JDBC url : jdbc:h2:mem:testdb

user : sa

password :

密码为空 不用输入.

 

 

 

 我们就可以看到管理界面

 

3. 注意事项:

如果引入了spring Security 控制权限那么就需要在配置类中加入以下代码

 

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
            .antMatchers("/design", "/orders").hasRole("USER")
            //h2 路径
            .antMatchers("/", "/h2-console/**").permitAll()
            //h2数据库    
            .and()
            .csrf()
            .disable()
            
            //h2数据库使用了frame框架
            .headers()
            .frameOptions()
            .sameOrigin() 
....

第一个是让 h2-console不用检验权限控制,第二个是关闭跨域认证,因为是测试,建议开启,第三个是开始frame框架,frame框架也会跨域访问,所以spring security也是默认关闭.

 

推荐阅读