【发布时间】:2015-03-14 22:18:52
【问题描述】:
我想用 MySQL 和 JPA 设置 Spring Boot。为此,我创建: 人
package domain;
import javax.persistence.*;
@Entity
@Table(name = "person")
public class Person {
@Id
@GeneratedValue
private Long id;
@Column(nullable = false)
private String firstName;
// setters and getters
}
PersonRepository
package repository;
import domain.Person;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.CrudRepository;
public interface PersonRepository extends CrudRepository<Person, Long> {
Page<Person> findAll(Pageable pageable);
}
PersonController
package controller;
import domain.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import repository.PersonRepository;
@Controller
public class PersonController {
@Autowired
private PersonRepository personRepository;
@RequestMapping("/")
@ResponseBody
public String test() {
Person person = new Person();
person.setFirstName("First");
person.setLastName("Test");
personRepository.save(person);
return "hello";
}
}
开始类示例:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Example {
public static void main(String[] args) throws Exception {
SpringApplication.run(Example.class, args);
}
}
对于数据库配置,我创建 application.properties
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.datasource.url=jdbc:mysql://localhost/test_spring_boot
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.jdbc.Driver
所以我有项目结构:
但结果我有例外:
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [Example]; nested exception is java.io.FileNotFoundException: class path resource [org/springframework/security/config/annotation/authentication/configurers/GlobalAuthenticationConfigurerAdapter.class] cannot be opened because it does not exist
作为一个例子,我使用:spring-boot-sample-data-jpa/pom.xml
【问题讨论】:
-
该链接应该告诉我们什么?这是 Spring Boot Data JPA 示例的 1.2.2.BUILD-SNAPSHOT 的 pom.xml。另外,您是如何运行该应用程序的?
-
@Steve,我在 IDE IDEA 中运行我的 Example.java
-
是否从命令行运行?
-
不是答案。但是,如果您遇到问题,您可能正在使用已弃用的驱动程序类名称。请改用
spring.datasource.driverClassName=com.mysql.cf.jdbc.Driver。
标签: java mysql spring jpa spring-boot