Spring Boot查询错误的表

时间:2018-07-01 13:41:17

标签: java mysql spring spring-boot

我是Spring Boot的新手,目前正在关注本教程(https://www.callicoder.com/spring-boot-rest-api-tutorial-with-mysql-jpa-hibernate/)。我设法创建了一个API,可以查询表中的getAll和addOne。问题在于它不使用这些查询中的现有表。相反,我的代码生成新表并在该表中执行查询。如何配置使用原始表的项目?

在本示例中我想用作数据源的数据库表。我已经在此表中存储了一行数据。

mysql> show columns from palauteUser;
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| userUUID  | varchar(30) | NO   | PRI | NULL    |       |
| firstName | varchar(30) | YES  |     | NULL    |       |
| lastName  | varchar(30) | YES  |     | NULL    |       |
| email     | varchar(30) | YES  |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+

Spring Boot生成的数据库表

mysql> show columns from palaute_user;
+------------+--------------+------+-----+---------+-------+
| Field      | Type         | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| useruuid   | varchar(255) | NO   | PRI | NULL    |       |
| email      | varchar(255) | YES  |     | NULL    |       |
| first_name | varchar(255) | YES  |     | NULL    |       |
| last_name  | varchar(255) | YES  |     | NULL    |       |
+------------+--------------+------+-----+---------+-------+

项目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>

    <groupId>fi.oppari.mysql.service</groupId>
    <artifactId>SpringService</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>SpringService</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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


</project>

application.properties文件

## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url = jdbc:mysql://localhost:3306/oppariDB?useSSL=false
spring.datasource.username = root
spring.datasource.password = root

spring.jpa.show-sql=true

## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update
server.port=9090

模型类

package fi.oppari.mysql.service.SpringService.model;

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



@Entity
@Table(name = "palauteUser")
public class PalauteUser {

    @Id
    private String userUUID;

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

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

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

    public String getUserUUID() {
        return userUUID;
    }

    public void setUserUUID(String userUUID) {
        this.userUUID = userUUID;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

}

存储库界面:

package fi.oppari.mysql.service.SpringService.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import fi.oppari.mysql.service.SpringService.model.PalauteUser;

@Repository
public interface PalauteUserRepository extends JpaRepository<PalauteUser, String>{

}

控制器:

package fi.oppari.mysql.service.SpringService.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import fi.oppari.mysql.service.SpringService.model.PalauteUser;
import fi.oppari.mysql.service.SpringService.repository.PalauteUserRepository;

@RestController
@RequestMapping("/api")
public class PalauteUserController {

    @Autowired
    PalauteUserRepository palauteUserRepository;

    @GetMapping("/all")
    public List<PalauteUser> getAll() {
        System.out.println("Getting all");
        return palauteUserRepository.findAll();
    }

    @PostMapping("/addOne")
    public PalauteUser addUser(@Valid @RequestBody PalauteUser user) {
        return palauteUserRepository.save(user);
    }

}

在mvn spring-boot:run命令之后,我将邮递员与GET localhost:9090 / api / all一起使用,这将返回空数组。带有json数据的POST localhost:9090 / api / addOne也成功,它将信息添加到palaute_user表中。

欢迎针对此问题提出任何建议,在此先感谢您! :)

2 个答案:

答案 0 :(得分:1)

将以下行放在application.properties上

spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

答案 1 :(得分:1)

Maruf所说的第一个建议是更改命名策略。这是因为Spring将palauteUser转换为palate_user并尝试在数据库上找到该表。

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

第二个建议是摆脱自动ddl生成:

#spring.jpa.hibernate.ddl-auto = update

相反,我建议您使用数据库版本控件,例如FlyWay或Liquibase。最简单的一种是FlyWay,您应该将其添加到类路径(Maven或Graddle)上。它会在应用程序启动时自动引导,比较/resources/db/migration/上的最新文件并运行所有脚本,直到版本匹配为止。您会看到Flyway在DB中创建了一个表来跟踪版本,文件和校验和。