Spring Boot,JPA和MySQL返回空结果

时间:2016-03-15 14:52:34

标签: java mysql spring jpa spring-boot

好的我是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数据库(在数据库选项卡下) enter image description here 我可以从选项卡中查询它,但我无法通过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";
}

2 个答案:

答案 0 :(得分:1)

显然,你没有关注你的链接教程,因为属性名称是camelCase,而TitleCase就在这里。 Spring希望camelCase用于属性,因为entity scanner uses camelCase for accessor/mutator generation

[每条评论]

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

答案 1 :(得分:0)

您应该使用小写属性

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

我测试了它并得到了同样的例外。但是有一个嵌套的例外,即sais:

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