2016-03-15 164 views
1

好吧我是Java Spring的新手,我只是试图从MYSQL数据库访问一些数据。不幸的是,我似乎无法加载任何数据 - 我敢肯定这是一个愚蠢的错误。 (我已经按照多种来源,包括this oneSpring Boot,JPA和MySQL返回空结果

我使用: java的春天开机,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 ,我可以从标签罚款查询它,但我无法通过弹簧加载数据。考虑到我无法删除嵌入的h2数据库,除非有例外,我的设置中肯定有问题,我也不能添加findByAge,findByFirstname等组合(全都抛出异常)。

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

回答

1

显然,你不跟着你的链接教程,因为属性名称是驼峰那里和这里的首字母大写。 Spring需要camelCase的属性,因为entity scanner uses camelCase for accessor/mutator generation

[根据注释]

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

+0

感谢您的回答。你是对的,完全错过了骆驼案件。我已经更新了我的答案和类,它实际上允许我使用'findByAge'和'findByLastName'(而不是像以前那样获取异常),但两者都返回null。 – gudthing

+0

仍然在等待运行日志 – hd1

+0

确定[这里是](https://gist.github.com/gudthing/d29c377bd64cd639461f) – gudthing

0

你应该使用小写的属性

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

我测试过,并得到同样的异常。但有其最高审计机关嵌套异常:

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