Spring Boot H2数据库 - 表不显示

时间:2018-03-14 17:53:55

标签: java spring-boot h2

我正在尝试在开发Spring Boot应用程序时使用H2数据库。我正在使用Spring Data JPA启动器。

build.gradle

    dependencies {
    compile('org.springframework.boot:spring-boot-starter-data-rest')
    compile('org.springframework.boot:spring-boot-starter-jdbc')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-security')
    compile('commons-io:commons-io:2.5')    
    compile('org.springframework.security:spring-security-jwt:1.0.7.RELEASE')
    compile('org.springframework.security.oauth:spring-security-oauth2:2.2.1.RELEASE')
    // compile 'com.microsoft.sqlserver:mssql-jdbc:6.2.2.jre8'   
    runtime('org.springframework.boot:spring-boot-devtools')
    compile ('org.apache.httpcomponents:httpclient:4.5.5')
    testCompile('com.h2database:h2:1.4.196')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile('org.springframework.security:spring-security-test')    

}

使用Java注释来定义实体。

示例类:

@Entity
@Table(name="auth_user")
public class OAuthUser {

//    @Autowired 
//    @Transient
//    private PasswordEncoder passwordEncoder;
//    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name= "username")
    private String userName;

    @Column(name="password")
    @JsonIgnore
    private String password;

    @Column(name="first_name")
    private String firstName;

    @Column(name="last_name")
    private String lastName;

    @Column(name="email")
    private String email;

    @Column(name="is_enabled")
    private boolean isEnabled;

     /**
      * Reference: https://github.com/nydiarra/springboot-jwt/blob/master/src/main/java/com/nouhoun/springboot/jwt/integration/domain/User.java
     * Roles are being eagerly loaded here because
     * they are a fairly small collection of items for this example.
     */
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "user_role", joinColumns
            = @JoinColumn(name = "user_id",
            referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(name = "role_id",
                    referencedColumnName = "id"))
private List<Role> roles;


    public OAuthUser() {};
    public OAuthUser(String firstName, String lastName, String user, String pass) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.userName = user;
        this.password = pass;
    }

/// Getters and Setters omitted

属性(source)

spring.h2.console.enabled=true

spring.h2.console.path=/h2

spring.datasource.url=jdbc:h2:file:~/test

spring.datasource.username=sa

spring.datasource.password=

spring.datasource.driver-class-name=org.h2.Driver

但是,每当我访问http://localhost:8090/h2并点击“连接”时,我只会转到空白页面。当我使用“测试连接”按钮时,它显示“测试成功”。正如本question中所提到的,我尝试了JDBC URL的jdbc:h2:mem:testdb和jdbc:h2:〜/ test。

IDE控制台中不显示任何错误。问题可能是什么?感谢。

1 个答案:

答案 0 :(得分:8)

jdbc:h2:mem:testdb是默认URL。如果从属性中删除有关H2的所有配置,则应在此URL下运行。

编辑: 因此,如果配置和路径是正确的,我会想到你正在使用Spring Security,我想到了另外一个解决方案。 尝试将此添加到您的安全配置:         http.headers()frameOptions()禁用();

举个例子,你可以去那里: http://shengwangi.blogspot.com/2016/02/how-to-use-h2-embeded-database-in-spring.html