如何将 Spring Boot 与 MySQL 数据库和 JPA 一起使用? [英] How to use Spring Boot with MySQL database and JPA?

查看:27
本文介绍了如何将 Spring Boot 与 MySQL 数据库和 JPA 一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 MySQL 和 JPA 设置 Spring Boot.为此,我创建:

包域;导入 javax.persistence.*;@实体@Table(name = "person")公共类人{@ID@GeneratedValue私人长ID;@Column(可为空 = 假)私人字符串名字;//setter 和 getter}

PersonRepository

包存储库;导入域.人;导入 org.springframework.data.domain.Page;导入 org.springframework.data.domain.Pageable;导入 org.springframework.data.repository.CrudRepository;公共接口 PersonRepository 扩展了 CrudRepository{页面<人>findAll(Pageable pageable);}

PersonController

包控制器;导入域.人;导入 org.springframework.beans.factory.annotation.Autowired;导入 org.springframework.stereotype.Controller;导入 org.springframework.web.bind.annotation.RequestMapping;导入 org.springframework.web.bind.annotation.ResponseBody;导入存储库.PersonRepository;@控制器公共类 PersonController {@自动连线私有 PersonRepository personRepository;@RequestMapping("/")@ResponseBody公共字符串测试(){人人 = 新人();person.setFirstName("First");person.setLastName("测试");personRepository.save(person);返回你好";}}

开始课程示例:

import org.springframework.boot.SpringApplication;导入 org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication公共类示例{public static void main(String[] args) 抛出异常 {SpringApplication.run(Example.class, args);}}

对于数据库配置,我创建了application.properties

spring.jpa.hibernate.ddl-auto=create-dropspring.jpa.properties.hibernate.globally_quoted_identifiers=truespring.datasource.url=jdbc:mysql://localhost/test_spring_bootspring.datasource.username=rootspring.datasource.password=rootspring.datasource.driverClassName=com.mysql.jdbc.Driver

所以我有项目结构:

但结果我有例外:

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [Example];嵌套异常是 java.io.FileNotFoundException: 类路径资源 [org/springframework/security/config/annotation/authentication/configurers/GlobalAuthenticationConfigurerAdapter.class] 无法打开,因为它不存在

作为一个例子,我使用:

I want to setting Spring Boot with MySQL and JPA. For this I create: Person

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

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

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

解决方案

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:

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:

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

这篇关于如何将 Spring Boot 与 MySQL 数据库和 JPA 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆