2015-01-16 24 views
21

Tôi muốn thiết lập Khởi động mùa xuân bằng MySQL và JPA. Đối với điều này tôi tạo: PersonLàm thế nào để sử dụng Spring Boot với cơ sở dữ liệu MySQL và JPA?

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"; 
} 
} 

Bắt đầu lớp Ví dụ :

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); 
} 

} 

Và đối với cấu hình cơ sở dữ liệu, tôi có thể tạo 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 

Vì vậy, tôi có cấu trúc dự án:

enter image description here

Nhưng kết quả là tôi có trường hợp ngoại lệ:

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 

Ví dụ tôi sử dụng: spring-boot-sample-data-jpa/pom.xml

+0

Liên kết đó phải cho chúng tôi biết điều gì? Đó là tệp pom.xml cho mẫu 1.2.2.BUILD-SNAPSHOT của mẫu JPA Spring Boot Data. Ngoài ra, bạn đang chạy ứng dụng như thế nào? – Steve

+0

@Steve, tôi chạy Example.java của mình trong IDE IDEA – TestUser

+0

Nó có chạy từ dòng lệnh không? – Steve

Trả lời

22

Tôi đã tạo một dự án như bạn đã làm. Cấu trúc trông như thế này

project structure

Các lớp học được chỉ cần sao chép dán từ bạn.

Tôi đã thay đổi application.properties này:

spring.datasource.url=jdbc:mysql://localhost/testproject 
spring.datasource.username=root 
spring.datasource.password=root 
spring.datasource.driverClassName=com.mysql.jdbc.Driver 
spring.jpa.hibernate.ddl-auto=update 

Nhưng tôi nghĩ vấn đề của bạn là ở pom.xml của bạn:

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
<modelVersion>4.0.0</modelVersion> 

<parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.4.1.RELEASE</version> 
</parent> 

<artifactId>spring-boot-sample-jpa</artifactId> 
<name>Spring Boot JPA Sample</name> 
<description>Spring Boot JPA Sample</description> 

<dependencies> 
    <dependency> 
     <groupId>mysql</groupId> 
     <artifactId>mysql-connector-java</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-web</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-data-jpa</artifactId> 
    </dependency> 
</dependencies> 
<build> 
    <plugins> 
     <plugin> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-maven-plugin</artifactId> 
     </plugin> 
    </plugins> 
</build> 

Kiểm tra những tập tin này cho sự khác biệt. Hy vọng điều này sẽ giúp

Cập nhật 1: Tôi đã thay đổi tên người dùng của mình. Liên kết đến ví dụ bây giờ là https://github.com/Yannic92/stackOverflowExamples/tree/master/SpringBoot/MySQL

+0

Tôi sao chép các thay đổi của bạn. Nó rất buồn, nhưng tôi có ngoại lệ nhưng 'Gây ra bởi: java.io.FileNotFoundException: tài nguyên đường dẫn lớp [org/springframework/security/config/annotation/authentication/configurers/GlobalAuthenticationConfigurerAdapter.class] không thể mở được vì nó không tồn tại' – TestUser

+0

Sau đó, bạn có nhiều hơn trong dự án của bạn hơn bạn đã cho chúng tôi thấy. Vấn đề của bạn có liên quan đến an ninh mùa xuân. Bạn vẫn có bảo mật mùa xuân trong phụ thuộc maven của bạn? –

+0

Không, tôi chưa có. Tôi thể hiện tất cả kiến ​​trúc và lớp học của mình. – TestUser

-10

Bạn có thể di chuyển Application.java vào một thư mục trong java.

+0

Điều này không cung cấp câu trả lời cho câu hỏi. Để phê bình hoặc yêu cầu làm rõ từ tác giả, để lại nhận xét bên dưới bài đăng của họ - bạn luôn có thể nhận xét về bài đăng của riêng bạn và sau khi bạn có đủ [danh tiếng] (http://stackoverflow.com/help/whats-reputation), bạn sẽ có thể [nhận xét về bài đăng bất kỳ] (http://stackoverflow.com/help/privileges/comment). - [Từ đánh giá] (/ review/low-quality-posts/10777423) – SHAZ

+0

Tôi cố gắng chạy ứng dụng và nhận ngoại lệ tương tự, sau khi di chuyển lớp chính sang thư mục, Ứng dụng có thể chạy thành công. – wuyuexin

3

Khi di chuyển các lớp học vào các gói cụ thể như kho lưu trữ, bộ điều khiển, miền chỉ chung @SpringBootApplication là không đủ.

Bạn sẽ phải xác định các gói cơ sở cho thành phần quét

@ComponentScan("base_package") 

Đối với JPA

@EnableJpaRepositories(basePackages = "repository") 

cũng là cần thiết, vì vậy dữ liệu mùa xuân sẽ biết được nơi để xem xét cho các giao diện kho.

+1

Mh có và không. Nếu bạn sử dụng '@ SpringBootApplication', tất cả các thành phần của bạn phải ở cùng mức hoặc thấp hơn so với lớp được chú thích bằng' @ SpringBootApplication'. Trong ví dụ này, trường hợp này không cần '@ ComponentScan'. –

0

Trong spring boot reference, nó nói:

Khi một lớp không bao gồm một tuyên bố gói nó được coi là trong “gói mặc định”. Việc sử dụng “gói mặc định” nói chung là không khuyến khích và nên tránh. Nó có thể gây ra các vấn đề đặc biệt cho các ứng dụng Spring Boot sử dụng các chú thích @ComponentScan, @EntityScan hoặc @SpringBootApplication, vì mọi lớp từ mỗi jar, sẽ được đọc.

com 
+- example 
    +- myproject 
     +- Application.java 
     | 
     +- domain 
     | +- Customer.java 
     | +- CustomerRepository.java 
     | 
     +- service 
     | +- CustomerService.java 
     | 
     +- web 
      +- CustomerController.java 

Trong trường hợp của bạn. Bạn phải thêm scanBasePackages vào chú thích @SpringBootApplication.just như @SpringBootApplication(scanBasePackages={"domain","contorller"..})

Các vấn đề liên quan