首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Springboot H2问题

Springboot H2问题
EN

Stack Overflow用户
提问于 2019-01-24 18:20:44
回答 1查看 263关注 0票数 0

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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

发布于 2019-01-25 10:50:19

我推荐documentation around sql databasesthis other question

大多数情况下,将以下行添加到您的application.properties中就可以解决这个问题。

spring.jpa.hibernate.ddl-auto = create

如果未检测到架构管理器,则

Spring Boot在内部将此参数值默认为create-drop,否则对于所有其他情况均为none。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54344293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档