【问题标题】:How to connect Spring Boot Application with MySQL database?如何将 Spring Boot 应用程序与 MySQL 数据库连接?
【发布时间】:2019-06-16 15:43:14
【问题描述】:

我是spring 框架的新手,我想将位于localhostMySQL 数据库与spring boot 应用程序连接起来。

【问题讨论】:

  • 可以在application.properties中添加数据库配置
  • 嗨,Appel,欢迎来到 stackoverflow。请提供您迄今为止所做的详细信息。
  • 给自己一个不错的尝试,并在发布之前搜索相关的帖子。这里有很多关于这个的问题。欢迎:)。

标签: mysql spring-boot spring-data-jpa


【解决方案1】:

连接mysql需要如下一些依赖

依赖项(maven -> pom.xml)。

<dependencies>
  <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>
  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
  </dependency>
</dependencies>

application.properties需要一些额外的配置设置,必须喜欢

application.properties (src/main/resources/application.properties)

# DataSource settings: set here your own configurations for the database 
# connection. In this example we have "netgloo_blog" as database name and 
# "root" as username and password.
spring.datasource.url = jdbc:mysql://localhost:8889/database_name
spring.datasource.username = mysql-userId
spring.datasource.password = mysql-pwd

# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

# Show or not log for each sql query
spring.jpa.show-sql = true

# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update

# Naming strategy (Not necessary to add but you can use this too)
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager)

# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

现在您的 EntityManager 对象将在您启动 Spring Boot 配置时构建。

以上定义的配置足以连接您的本地 mysql 数据库,但这里还有一些配置,您需要让您的代码更具可读性。

启用 JPARepositories,这样 CRUD 存储库必须在定义的包中定义。

像这样将@EnableJPARepositories("basepackage.*") 添加到您的SpringBootMainApplicationClass..

@SpringBootApplication
@EnableJPARepositories("com.demo.application.*.repositories")
@ComponentScan("com.demo.application.*")
public class SpringBootMainApplicationClass {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootMainApplicationClass.class, args);
    }
}

通过在MainClass 中添加@EnableJPARepositories 注释,用户可以使您的代码更具可读性,并且EntityManager 对象仅限于仅定义的包。

【讨论】:

    【解决方案2】:

    我列出了最低配置:

    在 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

    也可以查看其他问题:

    How to use Spring Boot with MySQL database and JPA?

    Spring Boot MYSQL connection

    Spring boot - MySQL settings are not working

    【讨论】:

    • 如何查看应用程序创建的表?
    • 好吧,在这种情况下使用 mysql 工作台并使用端口 3306 的本地主机,然后您可以连接到数据库并查看创建的架构
    【解决方案3】:

    你需要在你的application.properties中为Mysql添加数据库配置属性:

    spring.jpa.hibernate.ddl-auto=create
    spring.datasource.url=jdbc:mysql://localhost:3306/db_example
    spring.datasource.username=mysql
    spring.datasource.password=mysql
    
    1. spring.jpa.hibernate.ddl-auto=create 此配置将根据您创建的实体创建表。
    2. spring.datasource.url=jdbc:mysql://localhost:3306/db_example 此配置通过 jdbc 连接到名为 db_example 的数据库。您需要创建该数据库。
    3. spring.datasource.username=mysql这个配置你需要把你的用户通过jdbc连接到你的mysql数据库。
    4. spring.datasource.password=mysql这个配置你需要提供你的用户代表的密码,以便通过jdbc连接到你的mysql数据库。

    除了你还需要为 Mysql 和 JPA 添加依赖项。

    • 如果您使用的是 maven,请在 pom.xml 中添加依赖项:

      <dependencies>
          <!-- 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>
      </dependencies>
      
    • 如果你使用的是 gradle,在 build.gradle 中添加依赖:

      dependencies {
          // JPA Data (We are going to use Repositories, Entities, Hibernate, etc...)
          compile 'org.springframework.boot:spring-boot-starter-data-jpa'
      
          // Use MySQL Connector-J
          compile 'mysql:mysql-connector-java' 
      }
      

    【讨论】:

      【解决方案4】:

      pom.xml 中添加以下依赖项。

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

      在 application.properties 中添加以下数据库属性。

      spring.datasource.url=jdbc:mysql://localhost:3306/dbname
      spring.datasource.username=root
      spring.datasource.password=root
      

      更多详情请参考link

      【讨论】:

        【解决方案5】:

        application.properties

        spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
        spring.datasource.url = jdbc:mysql://localhost:3306/jpa
        spring.datasource.username = root
        spring.datasource.password =
        spring.jpa.show-sql = true
        spring.jpa.hibernate.ddl-auto = update
        spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
        

        build.gradle

        implementation 'mysql:mysql-connector-java:8.0.15'
        

        【讨论】:

          猜你喜欢
          • 2020-12-07
          • 2018-10-12
          • 1970-01-01
          • 2022-11-19
          • 1970-01-01
          • 1970-01-01
          • 2019-10-19
          • 2021-11-26
          • 1970-01-01
          相关资源
          最近更新 更多