Java 将 MySQL 连接到 Spring Boot REST 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24222684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:53:34  来源:igfitidea点击:

Connecting MySQL to a Spring Boot REST Application

javaspring

提问by Hines Bourne

Based off a tutorial on the Spring site I was able to setup a simple demo RESTapplication. It works fine but I just can't figure out how to tie it into my MySQL database. I have it configured to automatically connect but I know I am missing some glue. I have seen random blog posts about configuring a datasourcebean in Application.javabut I also read that using actuator this should all be done automatically. In the log output it looks like my database is being connected successfully, and when I hit the REST endpoints with cURLthey work fine but just don't interact at all with my MySQL db. Am I missing the datasource? If so, could you provide guidance on getting it to work? Thanks!

根据 Spring 站点上的教程,我能够设置一个简单的演示REST应用程序。它工作正常,但我不知道如何将它绑定到我的MySQL database. 我已将其配置为自动连接,但我知道我缺少一些胶水。我看过一些关于在其中配置datasourcebean 的随机博客文章,Application.java但我也读到使用执行器这一切都应该自动完成。在日志输出中,我的数据库似乎已成功连接,当我使用 REST 端点时,cURL它们工作正常,但根本不与我的MySQL db. 我错过了datasource吗?如果是这样,您能否提供有关使其工作的指导?谢谢!

The code is very simple:

代码非常简单:

Application.java package hello;

Application.java包你好;

import ...

@Configuration
@EnableJpaRepositories
@Import(RepositoryRestMvcConfiguration.class)
@EnableAutoConfiguration
@ComponentScan
@PropertySource("classpath:application.properties")
public class Application
{
    public static void main(String[] args)
    {
        SpringApplication.run(Application.class, args);
    }
}

User.java

用户.java

package hello;

import javax.persistence.*;

@Entity
@Table(name = "user")
public class User
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    private String username;

    public User()
    {  
    }

    public User(long id, String username)
    {
        this.id = id;
        this.username = username;
    }

    public String getUsername()
    {
        return username;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }
}

UserRepository.java

用户存储库.java

package hello;

import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

@RepositoryRestResource(collectionResourceRel = "user", path = "user")
public interface UserRepository extends PagingAndSortingRepository<User, Long>
{
}

application.properties:

应用程序属性:

spring.datasource.url=jdbc:mysql://localhost:3306/chrdb
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driverClassName=com.mysql.jdbc.Driver

采纳答案by M. Deinum

You don't need the @PropertySources, @EnableJpaRepositoriesand @Import(RepositoryRestMvcConfiguration.class)as Spring Boot handles that for you already when Spring Data JPA and Spring Data REST are detected. (That is what @EnableAutoConfigurationis for).

您不需要@PropertySources,@EnableJpaRepositories并且@Import(RepositoryRestMvcConfiguration.class)当检测到 Spring Data JPA 和 Spring Data REST 时,Spring Boot 已经为您处理了。(这就是为什么@EnableAutoConfiguration)。


import ...

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Specifying the url, username and password should be enough (if you are using Spring Boot 1.1.x).

指定 url、用户名和密码就足够了(如果您使用的是 Spring Boot 1.1.x)。

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

Finally make sure you don't have H2, HQSQLDBor Derbyon your classpath as those will automatically detected and might override your database settings.

最后确保您的类路径上没有H2HQSQLDBDerby,因为它们会自动检测到并可能覆盖您的数据库设置。

I have seen random blog posts about configuring a datasource bean in Application.java but I also read that using actuator this should all be done automatically.

我看过一些关于在 Application.java 中配置数据源 bean 的随机博客文章,但我也读到使用执行器这一切都应该自动完成。

Autoconfiguration hasn't so much to do with the actuator (only for Spring Security, metrics and management) but with the auto configuration part of Spring Boot.

自动配置与执行器(仅用于 Spring Security、指标和管理)没有太大关系,而是与 Spring Boot 的自动配置部分有关。