简体   繁体   English

Spring Boot、JPA 和 MySQL 返回空结果

[英]Spring Boot, JPA and MySQL returns empty results

Ok I'm new to Java Spring and I am simply trying to access some data from a MYSQL database.好的,我是 Java Spring 的新手,我只是想从 MYSQL 数据库中访问一些数据。 Unfortunately I can't seem to load any data - I'm sure it's a silly mistake.不幸的是,我似乎无法加载任何数据 - 我确定这是一个愚蠢的错误。 (I've followed a number of sources, including this one ) (我关注了许多来源,包括这个

I am using: java spring-boot, mysql, jpa, gradle我正在使用: java spring-boot、mysql、jpa、gradle

Here is my build.gradle :这是我的build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.2.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'

jar {
    baseName = 'gs-serving-web-content'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    compile("org.springframework.boot:spring-boot-starter-data-jpa")

    compile("com.h2database:h2") //I get embedded db error if I take this out
    testCompile("junit:junit")
    compile('mysql:mysql-connector-java:5.1.35')
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.3'
}

Controller:控制器:

@Controller
public class PersonController {
    @Autowired
    private PersonRepository personRepository;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
public String greeting(...){
    List<Person> allPeople = personRepository.findAll(); //this comes back with 0 values
    ...
}

PersonRepository:人库:

public interface PersonRepository extends JpaRepository<Person, Long> {
List<Person> findAll(); //returns empty list

//Person findByAge(int Age); //throws an exception if I leave this on: "...nested exception is java.lang.IllegalArgumentException: Could not create query metamodel for method public abstract hello.model.Person hello.repository.PersonRepository.findByAge(int)!"
}

Person:人:

@Entity
@Table(name="person")
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long PersonID;
    private String Firstname;
    private String Lastname;
    private int Age;
    //getters and setters
}

Application.yaml应用程序.yaml

spring:
 datasource:
  url: jdbc:mysql://localhost:3306/datatest
  username: root
  password: ***
  driverClassName: com.mysql.jdbc.Driver

I've connected intelliJ successfully to MySQL database (under database tab)我已成功将 intelliJ 连接到 MySQL 数据库(在数据库选项卡下) 在此处输入图片说明 and I can query it fine from the tab, but I can't load up the data through spring.我可以从选项卡中很好地查询它,但我无法通过 spring 加载数据。 There's got to be something wrong with my setup considering I can't seem to remove the embedded h2 database without an exception, nor can I add findByAge , findByFirstname etc combinations (all throw exceptions).考虑到我似乎无法毫无例外地删除嵌入式 h2 数据库,我的设置肯定有问题,也不能添加findByAgefindByFirstname等组合(都抛出异常)。

Update 1: I've applied hd1's changes to the Person class but I'm still not getting any data back:更新 1:我已经将 hd1 的更改应用于 Person 类,但我仍然没有取回任何数据:

@Entity
@Table(name="person")
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long personID;
    private String firstName;
    private String lastName;
    private int age;
    //getters and setters updated

PersonRepository:人库:

List<Person> findAll();
Person findByAge(int age); //no more errors here, but returns null
Person findByLastName(String lastName);  //no more errors here, but returns null

Full Runlog file完整的运行日志文件

Update 2: the controller method:更新2:控制器方法:

@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String greeting(@RequestParam(value = "name", required = false, defaultValue = "world") String name, Model model) {
    List<Person> allPeople = personRepository.findAll();
        ...
    System.out.print(allPeople.size()); //always 0, but I have many entries in db

    //if I try to create and save new entry in the db: 
    Person person = new Person();
    person.setFirstName("New");
    person.setLastName("Person");
    person.setAge(99);
    personRepository.save(person); //does not show up in DB, why?
    return "greeting";
}

Apparently, you've not followed your linked tutorial, because property names are camelCase there and TitleCase here.显然,您没有遵循链接的教程,因为属性名称在那里是驼峰式,在这里是 TitleCase。 Spring wants camelCase for properties, because the entity scanner uses camelCase for accessor/mutator generation . Spring 需要 camelCase 作为属性,因为实体扫描器使用 camelCase 来生成访问器/修改器

[per comment] [每条评论]

In PersonRepository, try uncommenting the commented line.在 PersonRepository 中,尝试取消注释注释行。 If you still have problems, in the next edit, post the complete stacktrace and the runlog.如果仍有问题,请在下一次编辑中发布完整的堆栈跟踪和运行日志。

You should use lower case properties您应该使用小写属性

private long personID;
private String firstname;
private String lastname;
private int age;

I've tested it and get the same exception.我已经对其进行了测试并得到了相同的异常。 But there is a nested exception which sais:但是有一个嵌套的异常,它说:

Unable to locate Attribute  with the the given name [attributename]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM