【发布时间】: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 数据库,也无法添加 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";
}
【问题讨论】:
标签: java mysql spring jpa spring-boot