简体   繁体   中英

couldn't not recognize spring.datasource.driverClassName in spring boot

I am trying to config datasource in the application.yml for a maven project in spring boot 2.4.5.

I want to add some config in application.yml like:

(The following is in properties format, but the IDEA will convert it to yml format for me. So please ignore the format here. The main issue is these properties cannot be found in spring boot 2.4.5 at all)

spring.datasource.url=XXX
spring.datasource.driverClassName=XXX

My pom.xml looks like:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.imooc</groupId>
    <artifactId>sell</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>sell</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</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>
                <version>2.4.5</version>
            </plugin>
        </plugins>
    </build>

</project>

And I also imported all these dependencies.

The issue is when I tried to add the datasource config in the application.yml , it can not be found. The only thing I can find with datasource is jta related datasource (refer to the snapshot I pasted here): 在此处输入图像描述

As you can see, IntelliJ doesn't find the spring.datasource.* config at all. It can only find jta related datasource config. Could anyone tell me what I am missing here? What's the correct config for datasource in spring boot 2.4.5?

After I change the spring boot version from 2.4.5 to 2.1.1 , now I can config the spring.datasource related properties. I still don't know why it can not work in spring boot 2.4.5 but to whom may encounter the same issue in spring boot 2.4.5 , you could try to change the version.

Did you add the dependency in your maven/gradle file?

Step 1. Add the maven/gradle dependency in your pom.xml/build.gradle file

If you're using maven -

<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.1.4</version>
</dependency>

If you're using gradle -

// https://mvnrepository.com/artifact/org.postgresql/postgresql
implementation group: 'org.postgresql', name: 'postgresql', version: '42.1.4'

Step 2. Build or reload your project.

You would see the error will be gone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM