Java SpringBoot、JPA和MySQL返回空结果
好的,我是JavaSpring新手,我只是尝试从MYSQL数据库访问一些数据。不幸的是,我似乎无法加载任何数据-我确信这是一个愚蠢的错误。(我跟踪了许多来源,包括) 我正在使用:java spring boot、mysql、jpa、gradle 这是我的Java SpringBoot、JPA和MySQL返回空结果,java,mysql,spring,jpa,spring-boot,Java,Mysql,Spring,Jpa,Spring Boot,好的,我是JavaSpring新手,我只是尝试从MYSQL数据库访问一些数据。不幸的是,我似乎无法加载任何数据-我确信这是一个愚蠢的错误。(我跟踪了许多来源,包括) 我正在使用:java spring boot、mysql、jpa、gradle 这是我的build.gradle: buildscript { repositories { mavenCentral() } dependencies { classpath("org.sprin
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
...
}
应用程序.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:
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)!"
}
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
List findAll();
个人信息(国际年龄)//此处不再出现错误,但返回null
Person findByLastName(字符串lastName)//此处不再出现错误,但返回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";
}
@RequestMapping(value=“/hello”,method=RequestMethod.GET)
公共字符串问候语(@RequestParam(value=“name”,required=false,defaultValue=“world”)字符串名称,模型){
List allPeople=personRepository.findAll();
...
System.out.print(allPeople.size());//始终为0,但我在db中有许多条目
//如果我尝试在数据库中创建并保存新条目:
Person=新人();
person.setFirstName(“新”);
person.setLastName(“person”);
人设定值(99);
personRepository.save(person);//未显示在DB中,为什么?
回复“问候”;
}
您应该使用小写属性
private long personID;
private String firstname;
private String lastname;
private int age;
我对它进行了测试,得到了相同的异常。但存在一个嵌套的异常,即:
Unable to locate Attribute with the the given name [attributename]
显然,您没有遵循链接教程,因为属性名在这里是camelCase,在这里是TitleCase。Spring希望为属性设置camelCase,因为 [每评论]
在PersonRepository中,尝试取消注释注释行。如果您仍然有问题,请在下一次编辑中发布完整的堆栈跟踪和运行日志。谢谢您的回答。你是对的,完全错过了骆驼案。我已经更新了我的答案和类,它实际上允许我使用
findByAge
和findByLastName
(而不是像以前那样得到一个异常),但它们都返回null。仍然在等待运行日志是的,您已经让tomcat在端口上侦听,现在您需要调用您的控制器方法。向发出HTTP GET请求,然后应该创建您的记录。我已经多次调用GET请求,但出于某种原因,它没有从数据库获取任何值。我已经添加了我的控制器方法来展示我是如何尝试这样做的。我假设我应该能够通过:personRepository.findAll()简单地获取数据库中的所有“人员”代码>,但这会返回0个条目没问题,伙计,只是尽我所能提供帮助
Unable to locate Attribute with the the given name [attributename]