我是使用Spring MVC和JPA的新手。我有一个Postgresql数据库,我在其中创建了两个表:author和userType。
CREATE TABLE UserType (
idUserType INTEGER NOT NULL,
userType VARCHAR NOT NULL,
CONSTRAINT pkusertype PRIMARY KEY (idUserType)
);
CREATE TABLE Author (
idAuthor INTEGER NOT NULL DEFAULT nextval('author_idauthor_seq'),
lastName VARCHAR NOT NULL,
firstName VARCHAR NOT NULL,
CONSTRAINT pk_author PRIMARY KEY (idAuthor)
);
然后我想将它们映射到Spring中的代码并显示它们。问题是即使我以相同的方式执行,但是正确地显示了作者([某些JSON]),而当我想看到userType时,我收到一个空数组([])。我不知道它为什么会发生,并会感激帮助。
作者模型
package com.shareabook.model;
import javax.persistence.*;
@Entity
@Table(name = "author")
public class Author {
@Id
@GeneratedValue
@Column(name = "idauthor")
private Integer id;
@Column(name = "lastname")
private String lastName;
@Column(name = "firstname")
private String firstName;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
UserType模型
package com.shareabook.model;
import javax.persistence.*;
@Entity
@Table(name = "UserType ")
public class UserType {
@Id
@GeneratedValue
@Column(name = "idusertype")
private Integer id;
@Column(name = "usertype")
private String userType;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserType() {
return userType;
}
public void setUserType(String userType) {
this.userType = userType;
}
}
作者控制器
package com.shareabook.controller;
import com.shareabook.model.Author;
import com.shareabook.repository.AuthorRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping(value = "/rest/author")
public class AuthorController {
@Autowired
AuthorRepository authorRepository;
@GetMapping(value = "/all")
public List<Author> getAll(){
return authorRepository.findAll();
}
}
USERTYPE CONTROLLER
package com.shareabook.controller;
import com.shareabook.model.UserType;
import com.shareabook.repository.UserTypeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping(value = "/rest/usertype")
public class UserTypeController {
@Autowired
UserTypeRepository userTypeRepository;
@GetMapping(value = "/all")
private List<UserType> getAll(){
return userTypeRepository.findAll();
}
}
作者存储库
package com.shareabook.repository;
import com.shareabook.model.Author;
import org.springframework.data.jpa.repository.JpaRepository;
public interface AuthorRepository extends JpaRepository<Author, Integer>{
}
UserType存储库
package com.shareabook.repository;
import com.shareabook.model.UserType;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserTypeRepository extends JpaRepository<UserType, Integer>{
}
主要课程
package com.shareabook;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@EnableJpaRepositories(basePackages = "com.shareabook.repository")
@SpringBootApplication
public class ShareabookApplication {
public static void main(String[] args) {
SpringApplication.run(ShareabookApplication.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>
<groupId>com.shareabook</groupId>
<artifactId>shareabook</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>shareabook</name>
<description>Basic project for application Share A Book</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.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>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1201-jdbc4</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>1.5.3.RELEASE</version>
</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>
<version>1.16.16</version>
<scope>provided</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.dbcp2.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/shareabookthesis
spring.datasource.username=postgres
spring.datasource.password=wiktoria2
spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
答案 0 :(得分:-1)
Postgres中的数据库名称应小写,而CamelCase中的表名称则不应。表名称应为usertype而不是UserType。