首页 > 解决方案 > 如何将 Spring Boot 应用程序与 MySQL 数据库连接?

问题描述

我是spring框架的新手,我想将MySQL位于localhostSpring Boot 应用程序中的数据库连接起来。

标签: mysqlspring-bootspring-data-jpa

解决方案


我列出了最小配置:

在 pom.xml 中

<!-- JPA Data (We are going to use Repositories, Entities, Hibernate, etc...) -->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <!-- Use MySQL Connector-J -->

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

在 application.properties

spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword

你可以在任何地方找到这样的例子。喜欢阅读https://spring.io/guides/gs/accessing-data-mysql/

来源:https ://spring.io

也检查其他问题:

如何将 Spring Boot 与 MySQL 数据库和 JPA 一起使用?

Spring Boot MYSQL 连接

春季启动 - MySQL 设置不起作用


推荐阅读