我想用MySQL和JPA設置Spring Boot。爲此,我創建: 人如何在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
@Steve,我在IDE中運行我的Example.java IDEA – TestUser
它是否從命令行運行? – Steve