2015-01-16 447 views
21

我想用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 

所以我有項目結構:

enter image description here

但作爲一個結果,我有例外:

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

+0

這個鏈接應該告訴我們什麼?這是Spring Boot Data JPA示例的1.2.2.BUILD-SNAPSHOT的pom.xml。另外,你如何運行應用程序? – Steve

+0

@Steve,我在IDE中運行我的Example.java IDEA – TestUser

+0

它是否從命令行運行? – Steve

回答

22

我創建了一個像你這樣的項目。該結構看起來像這樣

project structure

類是剛剛從複製粘貼你的。

我改變了application.properties這樣:

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 

但我覺得你的問題是在你的的pom.xml

<?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> 

檢查這些文件爲了差異。希望這可以幫助

更新1:我更改了我的用戶名。此示例的鏈接現在爲https://github.com/Yannic92/stackOverflowExamples/tree/master/SpringBoot/MySQL

+0

我複製您的更改。這是非常可悲的,但我有異常,但不能打開,因爲它不存在'java.io.FileNotFoundException:類路徑資源[org/springframework /安全/配置/註釋/身份驗證/配置器/ GlobalAuthenticationConfigurerAdapter.class] – TestUser

+0

然後你的項目中有更多的東西比你向我們展示的要多。你的問題與彈簧安全有關。你的Maven依賴關係中是否仍然具有spring安全性? –

+0

不,我沒有。我展示了我的所有架構和類。 – TestUser

-10

您可以將Application.java移動到java下的文件夾中。

+0

這不提供問題的答案。要批評或要求作者澄清,在他們的帖子下留下評論 - 你總是可以評論你自己的帖子,一旦你有足夠的[聲譽](http://stackoverflow.com/help/whats-reputation),你會能夠[評論任何帖子](http://stackoverflow.com/help/privileges/comment)。 - [發表評論](/ review/low-quality-posts/10777423) – SHAZ

+0

我嘗試運行應用程序並獲得相同的異常,將主類移動到文件夾後,應用程序可以運行成功。 – wuyuexin

3

當將類移動到特定的軟件包(如存儲庫,控制器)時,只需使用通用的@SpringBootApplication即可。

您必須指定的基本軟件包組件掃描

@ComponentScan("base_package") 

對於JPA

@EnableJpaRepositories(basePackages = "repository") 

也需要,所以春季數據就知道到哪裏尋找到的庫接口。

+1

是和不是。如果使用'@ SpringBootApplication',則所有組件必須與'@ SpringBootApplication'註釋的類相同或更低。在這個例子中是這種情況,所以不需要'@ ComponentScan'。 –

0

spring boot reference,它說:

當一個類不包括它被認爲是在「默認包」一個包聲明。通常不鼓勵使用「默認包」,應該避免使用「默認包」。對於使用@ComponentScan,@EntityScan或@SpringBootApplication註釋的Spring Boot應用程序,它可能會導致特定問題,因爲每個jar的每個類都將被讀取。

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

在您的案件。您必須在@SpringBootApplication註釋中添加scanBasePackages.just like @SpringBootApplication(scanBasePackages={"domain","contorller"..})

相關問題