如何将 Spring Boot 与 MySQL 数据库和 JPA 结合使用?

2024-01-15

我想使用 MySQL 和 JPA 设置 Spring Boot。为此我创建: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
}

人员信息库

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

人员控制器

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

开始上课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);
}

}

对于数据库配置,我创建应用程序属性

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 https://github.com/spring-projects/spring-boot/blob/master/spring-boot-samples/spring-boot-sample-data-jpa/pom.xml


我像你一样创建了一个项目。结构看起来像这样

这些课程只是从您的课程中复制粘贴的。

我改变了应用程序属性对此:

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 https://github.com/Yannic92/stackOverflowExamples/tree/master/SpringBoot/MySQL

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将 Spring Boot 与 MySQL 数据库和 JPA 结合使用? 的相关文章

随机推荐

  • docker-compose:在詹金斯上找不到命令

    我有一个 docker compose 文件 version 3 services mysql image mysql latest container name locations service mysql environment MY
  • R XTS 对象的逐月增长百分比

    我如何绘制以下数据的逐月增长情况 A 2008 07 01 0 2008 08 01 87 2008 09 01 257 2008 10 01 294 2008 11 01 325 2008 12 01 299 以 dput 格式 在约书亚
  • 更新 hadoop HDFS 文件

    我是 Hadoop 的新手 我一直读到 HDFS 主要是 一次写入 多次读取 我有一个用例 我可能需要对 HDFS 中存储的文件进行修改 我一直在研究是否有任何方法可以做到这一点 我的问题是是否可以将 HDFS 文件加载到 HBase 中
  • 使用 RVM 在 OS X 上安装 Ruby 1.9.3 时出现“没有适用于 gcc46 的可用公式”:

    我已经安装了 Homebrew 我正在尝试使用以下命令安装 RVM rvm install 1 9 3 head 我收到此错误 Installing required packages gcc46 Error running require
  • 如何在 github actions 中使用私有 docker 镜像

    我正在尝试在 github actions 中设置一个运行私有 docker 映像的作业 我将使用容器选项在该 docker 映像内进行构建 link https docs github com en actions reference w
  • Google Play 游戏服务无法登录

    几天来我一直在为这个问题而头撞墙 尽管阅读了许多描述 GPS 登录失败问题 解决方案的帖子 但我一直无法找到问题的解决方案 简而言之 我正在使用 LibGDX 框架在 Android Studio 中开发一款游戏 并添加了 BaseGame
  • IE10 上触摸点之间的间距

    我正在开发插件 flot touch js 它在 webkit 浏览器的图表上添加触摸交互性 平移和缩放 我也想让它在 IE10 上运行 但是我不知道如何恢复触摸点之间的空间 我需要这个来计算比例 在 webkit 浏览器上 我们可以使用这
  • iOS 检查 WiFi 辅助是否已启用

    我正在尝试检查 WiFi 辅助是否已启用 当我连接到接入点获取一些数据时遇到问题 当我的连接状况不佳时 我的蜂窝数据会被使用并干扰我的接入点 有什么方法可以检查该选项是否启用 好吧 我想我能帮上一点忙 你需要check SCNetworkR
  • 路由到 Laravel 5 子文件夹中的控制器

    这是我的routes php Route get Panel PanelController index 这是我的文件夹 Http Controllers Panel PanelController php 这是我的控制器 namespac
  • 如何使用 Google Assist API 实现 Google Assistant

    我一直在查看并阅读有关 Google Now on Tap 的内容 来自http developer android com training articles assistant html http developer android c
  • 如何从元数据查找 OData 版本

    我可以访问 OData 服务 现在我需要找到该服务的 OData 版本 metadata 中有版本属性 但我不知道该选哪一个 我需要从
  • 如何保留 HTML
     标记中包含的文本的空白缩进,不包括文档中 
     标记的当前缩进级别?                
                

    我正在尝试在网站上显示我的代码 但在正确保留空格缩进方面遇到问题 例如给出以下代码片段 Here is my code pre def some funtion return Hello World end pre 这在浏览器中显示为 He
  • 如何在pyspark中将数据分成组

    我需要在时间序列数据中找到组 数据样本 我需要输出列group基于value and day 我尝试过使用 lag lead 和 row number 但最终一无所获 似乎每次值发生变化时您都想增加该组 如果是这样 这就是一种缺口和岛屿问题
  • 如何修复类型“Future”不是“String”类型的子类型 Flutter

    颤振初学者在这里 我收到此错误 type Future
  • Python 中的函数重载:缺失 [关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions As 函数重载 http web a
  • 平移/旋转后重置 CSS 变换原点

    在 CSS 中翻译元素后 其变换原点仍保留在其原始位置 在这种特殊情况下 我希望变换原点为stay在所有变换期间相对于元素居中 我希望起源是这样的follow被变换的元素 我知道关于transform origin属性 但这似乎需要我每次手
  • 为什么我在设置 LibGDX 时没有通用补间引擎

    接下来是关于设置 LibGDX 项目的教程 教程说在第三方部分应该有一个通用 Tween 引擎 地雷不显示 有什么办法可以显示吗 我需要下载什么 提前致谢 Gdx setup new ui jar 不附带 TweenEngine 库 还有另
  • z-index 不适用于绝对位置

    如果你看看我的测试网站here http www hosting mate com marinebuoysaustralia 您将看到 向下滚动 按钮与我的所有内容重叠 无论我输入什么 z index 有办法解决这个问题吗 我意识到我的立场
  • 在 VBscript 中使用命令行参数

    如何在 VBscript 中传递和访问命令行参数 Set args Wscript Arguments For Each arg In args Wscript Echo arg Next 从命令提示符运行脚本 如下所示 CSCRIPT M
  • 如何将 Spring Boot 与 MySQL 数据库和 JPA 结合使用?

    我想使用 MySQL 和 JPA 设置 Spring Boot 为此我创建 Person package domain import javax persistence Entity Table name person public cla