好吧我是Java Spring的新手,我只是試圖從MYSQL數據庫訪問一些數據。不幸的是,我似乎無法加載任何數據 - 我敢肯定這是一個愚蠢的錯誤。 (我已經按照多種來源,包括this one)Spring 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數據庫(數據庫標籤下) ,我可以從標籤罰款查詢它,但我無法通過彈簧加載數據。考慮到我無法刪除嵌入的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
更新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";
}
感謝您的回答。你是對的,完全錯過了駱駝案件。我已經更新了我的答案和類,它實際上允許我使用'findByAge'和'findByLastName'(而不是像以前那樣獲取異常),但兩者都返回null。 – gudthing
仍然在等待運行日誌 – hd1
確定[這裏是](https://gist.github.com/gudthing/d29c377bd64cd639461f) – gudthing