我正在用MongoDB做一个Spring Boot Batch示例,我已经启动了mongod服务器。

当我启动我的应用程序时,我得到下面的错误。

对这个问题有什么建议吗?

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
    If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

application.properties:

# Mongo database URI. Cannot be set with host, port and credentials.
spring.data.mongodb.uri=mongodb://localhost/test 

pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

我已经启动了mongod,输出如下:

C:\Users\pc>mongod
2018-07-07T14:39:39.223+0530 I JOURNAL  [initandlisten] journal dir=C:\data\db\journal
2018-07-07T14:39:39.230+0530 I JOURNAL  [initandlisten] recover : no journal files present, no recovery needed
2018-07-07T14:39:39.478+0530 I JOURNAL  [durability] Durability thread started
2018-07-07T14:39:39.589+0530 I CONTROL  [initandlisten] MongoDB starting : pid=11992 port=27017 dbpath=C:\data\db\ 64-bit host=DESKTOP-NQ639DU
2018-07-07T14:39:39.589+0530 I CONTROL  [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2018-07-07T14:39:39.591+0530 I CONTROL  [initandlisten] db version v3.0.5
2018-07-07T14:39:39.592+0530 I CONTROL  [initandlisten] git version: 8bc4ae20708dbb493cb09338d9e7be6698e4a3a3
2018-07-07T14:39:39.592+0530 I CONTROL  [initandlisten] build info: windows sys.getwindowsversion(major=6, minor=1, build=7601, platform=2, service_pack='Service Pack 1') BOOST_LIB_VERSION=1_49
2018-07-07T14:39:39.592+0530 I CONTROL  [initandlisten] allocator: tcmalloc
2018-07-07T14:39:39.593+0530 I CONTROL  [initandlisten] options: {}
2018-07-07T14:39:39.595+0530 I JOURNAL  [journal writer] Journal writer thread started
2018-07-07T14:39:40.485+0530 I NETWORK  [initandlisten] waiting for connections on port 27017
2018-07-07T14:40:39.140+0530 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:51340 #1 (1 connection now open)
2018-07-07T14:40:41.663+0530 I NETWORK  [conn1] end connection 127.0.0.1:51340 (0 connections now open)
2018-07-07T14:45:12.421+0530 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:51578 #2 (1 connection now open)
2018-07-07T14:45:12.870+0530 I NETWORK  [conn2] end connection 127.0.0.1:51578 (0 connections now open)
2018-07-07T14:46:21.734+0530 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:51591 #3 (1 connection now open)
2018-07-07T14:46:22.041+0530 I NETWORK  [conn3] end connection 127.0.0.1:51591 (0 connections now open)
2018-07-07T14:57:47.523+0530 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:52534 #4 (1 connection now open)
2018-07-07T14:57:47.910+0530 I NETWORK  [conn4] end connection 127.0.0.1:52534 (0 connections now open)


当前回答

这个方法对我来说很管用,适用于MySQL: (应用程序属性)

spring.datasource.url=jdbc:mysql://localhost:3306/db?useSSL=false&useUnicode=true&useJDBCCompliantTimezoneShift=true&
useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=admin
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true

其他回答

根本原因

JPA (Java持久性API)是ORM(对象-关系映射)工具的Java规范。spring-boot-starter-data-jpa依赖项在spring引导框架的上下文中启用ORM。

spring引导应用程序的JPA自动配置特性尝试使用JPA Datasource建立数据库连接。JPA DataSource bean需要数据库驱动程序连接到数据库。

数据库驱动程序应该作为pom.xml文件中的依赖项可用。对于外部数据库如Oracle、SQL Server、MySql、DB2、Postgres、MongoDB等都需要数据库的JDBC连接属性来建立连接。

您需要配置数据库驱动程序和JDBC连接属性来修复此异常。配置数据源失败:没有指定' url '属性,无法配置嵌入式数据源。原因:无法确定合适的驱动程序类别。

application.properties

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration 

application.yaml

spring:
  autoconfigure:
    exclude: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

通过编程

@SpringBootApplication(exclude =  {DataSourceAutoConfiguration.class })

检查你的application.properties

改变

spring.datasource.driverClassName=com.mysql.jdbc.Driver

to

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

为我工作。完整的配置:

spring.datasource.url=jdbc:mysql://localhost:3306/db
spring.datasource.username=
spring.datasource.password=   
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto = update

检查您的应用程序。属性文件。 其中一个可能的原因是

你可能被添加了“Spring Data”maven插件,而你没有 在应用程序中提供数据存储细节。属性文件。

对于spring引导版本2.X。X下面的配置对我来说是可行的。

spring.datasource.url=jdbc:mysql://localhost:3306/rest
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto = update 

旧的jdbc驱动程序已弃用。新的配置在上面提到了。 请使用相同的并重新启动项目。

这意味着你缺少“使用属性定义数据源”

(记住更改sql datasource, username和pass)

spring.datasource.url=jdbc:mysql://localhost:3306/basic_springboot 
spring.datasource.username=root
spring.datasource.password=manhcong
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

你可以参考这个

https://www.baeldung.com/spring-boot-failed-to-configure-data-source