如何在MySQL数据库和JPA中使用Spring Boot?
I want to setting Spring Boot with MySQL and JPA. For this I create: Person
我想用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
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
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";
}
}
Start class Example:
启动类示例:
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);
}
}
And for database configuration, I create application.properties
对于数据库配置,我创建了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
So I have project structure:
所以我有项目结构:
But as a result I have exceptions:
但结果我有例外:
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
As a example I use: spring-boot-sample-data-jpa/pom.xml
作为一个例子,我使用:spring-boot-sample-data-jpa / pom.xml
4 个解决方案
#1
22
I created a project like you did. The structure looks like this
我创建了一个像你一样的项目。结构看起来像这样
The Classes are just copy pasted from yours.
这些课程只是从你的复制粘贴。
I changed the application.properties to this:
我将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
But I think your problem is in your pom.xml:
但我认为你的问题在你的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>
Check these files for differences. Hope this helps
检查这些文件是否存在差异希望这可以帮助
Update 1: I changed my username. The link to the example is now https://github.com/Yannic92/stackOverflowExamples/tree/master/SpringBoot/MySQL
更新1:我更改了用户名。该示例的链接现在是https://github.com/Yannic92/stackOverflowExamples/tree/master/SpringBoot/MySQL
更多相关文章
- 【文档】五、Mysql Binlog事件结构
- MySQL官网示例数据库emploees分析使用
- mysql 树形结构查询(存储过程)
- InnoDB辅助索引页面的物理结构是什么样子的
- MySQL Internal - InnoDB存储引擎(行结构)
- 组织这种结构的最佳方式?
- MySQL修改表结构操作命令总结
- 从mysql中导出单个表结构和数据
- 【转】mysql-innodb-buffer pool 结构分析