Springboot H2问题

时间:2019-01-24 10:20:44

标签: spring-boot

我是spring-boot的初学者,所以我正在尝试所有基本功能。最近,我开始构建一个小项目,但无法连接到Spring-boot H2嵌入式数据库。据我所知,该表应该在h2-控制台中自动可用。但由于某种原因,我没有从pojo拿到桌子。

TestController.java

package com.example.theboot.testPhase.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.example.theboot.testPhase.Repo.ProfRepositories;

@RestController
public class TestController {

@Autowired
ProfRepositories repo;
@GetMapping(path="/showme/{id}")
public String getShowMe(@PathVariable("id") int ids) {      
    int c = (int) repo.count();
    return "Count " + c;
}

@GetMapping(path="/showme")
public String getMe() {

    return "showmeWorking";
}
}

ProfRepositories.java

package com.example.theboot.testPhase.Repo;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.example.theboot.testPhase.vo.Profiles;

@Repository
public interface ProfRepositories extends CrudRepository<Profiles, String>{
}

Profiles.java

package com.example.theboot.testPhase.vo;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Entity
@Table
public class Profiles {

@Id
@Column (name = "ID")
@GeneratedValue(strategy = GenerationType.AUTO)
String ID;
@Column (name = "NAME")
String name;

@Override
public String toString() {
    return "Profiles [ID=" + ID + ", name=" + name + "]";
}

}

TestPhaseApplication.java

package com.example.theboot.testPhase;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TestPhaseApplication {

public static void main(String[] args) {
    SpringApplication.run(TestPhaseApplication.class, args);
}

}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"                 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0     
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.theboot</groupId>
<artifactId>testPhase</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>testPhase</name>
<description>Demo project for Spring Boot</description>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>

    <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.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

application.properties

spring.h2.console.enabled=true
spring.datasource.platform=h2
spring.h2.console.path=/h2
spring.database.url=jdbc:h2:mem:A12

对不好的编辑深表歉意。我是新来的,我发现这很难处理。提前致谢 browser h2 console project structure

1 个答案:

答案 0 :(得分:0)

我建议使用documentation around sql databasesthis other question

通常在您的application.properties中添加以下行即可解决问题。

spring.jpa.hibernate.ddl-auto = create

  

如果未检测到任何模式管理器,Spring Boot在内部默认将此参数值设置为create-drop,否则在所有其他情况下均未设置。