spring-jdbc 似乎不适用于 spring-boot-starter

spring-jdbc doesn't seem to work with spring-boot-starter

我正在遵循 15 分钟的 sprint-boot 指南 (gs-relational-data-access)

因此,本指南使用 H2 数据库。 所以现在我通过在运行时提供 jars 来更改它以使用 DB2。

已修改build.gradle

dependencies {
    compile("org.springframework.boot:spring-boot-starter")
    compile("org.springframework:spring-jdbc")
    runtime fileTree(dir: 'libs', include: '*.jar')
    //compile("com.h2database:h2")
    testCompile("junit:junit")
}

现在应用程序失败,抱怨未找到 JdbcTemplate bean 定义或类似的问题。

所以现在我进一步修改build.gradle注释掉spring-jdbc,并使用spring-boot-starter-jdbc

dependencies {
    compile("org.springframework.boot:spring-boot-starter-jdbc")
    //compile("org.springframework:spring-jdbc")
    runtime fileTree(dir: 'libs', include: '*.jar')
    //compile("com.h2database:h2")
    testCompile("junit:junit")
}

现在应用程序再次运行。我想知道为什么 spring-jdbc 依赖项不能仅与 sprint-boot-starter 一起使用?

spring-jdbc 具有 spring 支持 JDBC API 的所有 类,但 spring-boot-starter-jdbc 允许启用所有需要的自动配置.由于自动配置,您可以在 application.properties

中使用简单的配置自动连接 JdbcTemplateJdbcOperations