【问题标题】:Spring Boot, JPA and MySQL returns empty resultsSpring Boot、JPA 和 MySQL 返回空结果
【发布时间】:2016-07-01 02:13:40
【问题描述】:

好的,我是 Java Spring 的新手,我只是想从 MYSQL 数据库中访问一些数据。不幸的是,我似乎无法加载任何数据——我确信这是一个愚蠢的错误。 (我关注了很多来源,包括this one

我正在使用: java spring-boot、mysql、jpa、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
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)!"
}

人:

@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

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

我已成功将 IntelliJ 连接到 MySQL 数据库(在数据库选项卡下) 我可以从选项卡中很好地查询它,但我无法通过 spring 加载数据。考虑到我似乎无法毫无例外地删除嵌入式 h2 数据库,也无法添加 findByAgefindByFirstname 等组合(所有抛出异常),我的设置肯定有问题。

更新 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

更新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";
}

【问题讨论】:

    标签: java mysql spring jpa spring-boot


    【解决方案1】:

    显然,您没有按照链接的教程进行操作,因为属性名称是 camelCase 那里和 TitleCase 这里。 Spring 需要 camelCase 属性,因为 entity scanner uses camelCase for accessor/mutator generation

    [每条评论]

    在 PersonRepository 中,尝试取消注释行。如果您仍有问题,请在下一次编辑中发布 complete 堆栈跟踪和运行日志。

    【讨论】:

    • 感谢您的回答。你是对的,完全错过了骆驼案。我已经更新了我的答案和课程,它实际上允许我使用 findByAgefindByLastName(而不是像以前那样得到一个异常),但两者都返回 null。
    • 还在等待运行日志
    • 是的,你已经在端口上监听了tomcat,现在你需要调用你的控制器方法。向localhost:8000/hello 发出 HTTP GET 请求,然后应该创建您的记录。
    • 我已经多次调用 GET 请求,但由于某种原因,它没有从数据库中获取任何值。我添加了我的控制器方法来展示我是如何尝试这样做的。我想我应该能够通过personRepository.findAll(); 简单地获取数据库中的所有“人”,但这会返回 0 个条目
    • 没问题,伙计,尽我所能提供帮助
    【解决方案2】:

    你应该使用小写的属性

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

    我已经对其进行了测试并得到了相同的异常。但是有一个嵌套的异常:

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

    【讨论】:

      猜你喜欢
      • 2018-06-26
      • 2017-08-29
      • 2018-10-02
      • 1970-01-01
      • 2019-05-15
      • 1970-01-01
      • 2020-02-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多