【问题标题】:Spring boot did not create datasourceSpring Boot 没有创建数据源
【发布时间】:2014-04-10 19:12:51
【问题描述】:

我实际上有问题,spring boot 没有初始化数据源 在这里您可以找到所有必要的信息:

Application.java

@Configuration
@ComponentScan
@EnableAutoConfiguration
class Application {

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

@Entity
class Device extends AbstractPersistable<Long> {
    private String name;

    Device() {
    }

    Device(String name) {

        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

interface DeviceRepository extends JpaRepository<Device, Long> {}

@RestController
class DeviceController {

    @Autowired
    private DeviceRepository deviceRepository;

    @RequestMapping("/devices")
    Collection<Device> getAllDevices() {
        return deviceRepository.findAll();
    }
}

应用程序.yaml

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

pom.xml sn-p

<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>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

在过去的几个小时里,我一直在尝试解决这个问题,并在堆栈溢出时阅读了几乎所有关于这个问题的线程,但直到现在我还没有得到解决方案。

【问题讨论】:

  • 根据本节:projects.spring.io/spring-boot/docs/docs/howto.html#toc_36 在 Spring Boot 文档中。我假设它必须与我的配置一起使用,因为 spring-jdbc 工件位于通过 spring-boot- 的类路径中starter-data-jpa 工件
  • 请在这篇文章中添加必要的代码,不要链接到它。您至少需要有 @EnableJpaRepository 注释,否则它不会开始扫描您的存储库。接下来确保您的类路径中有 tomcat-jdbc 或 commons-dbcp 以实际构建数据源。
  • 对不起,代码太薄了,我现在把它包括在内。但是根据projects.spring.io/spring-boot/docs/spring-boot-autoconfigure/… EnableAutocConfiguration 确实也设置了spring data jpa ...我将tomcat-jdbc工件明确添加到我的pom中,但是spring仍然没有为我创建一个dataSource bean。

标签: java mysql spring maven spring-boot


【解决方案1】:

是的,我明白了。我只是忘了添加

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

添加后一切正常:)

我傻了!

【讨论】:

  • 请注意,无论哪种方式,您还必须引入 org.apache.tomcat:tomcat-jdbc 依赖项。
猜你喜欢
  • 2014-12-27
  • 2016-08-09
  • 2017-02-16
  • 2019-08-23
  • 1970-01-01
  • 2019-12-02
  • 2021-03-04
  • 2018-10-13
  • 2019-01-21
相关资源
最近更新 更多