Env:Eclipse + sts
我新建了一个Spring Starter项目,选择JDBC API,MS SQL Server驱动程序,Spring Web Starter。
第一次创建时,会出现如下错误:
Multiple annotations found at this line:
- Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-resources-plugin:3.1.0:testResources (execution: default-testResources, phase: process-test-resources)
- Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-resources-plugin:3.1.0:resources (execution: default-resources, phase: process-resources)
我忽略了它。我在pom文件中添加了资源,
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.yml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.yml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
然后我在src / main / resources / application.yml中添加SQL Server内容
spring:
datasource:
driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
password: sly
url: jdbc:sqlserver://localhost:1433;DatabaseName=msdb
username: sa
但是我运行@SpringBootApplication,它提供信息
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
我尝试过application.properties,它也不起作用。
spring.datasource.url=jdbc:sqlserver://localhost:1433;DatabaseName=msdb
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.username=sa
spring.datasource.password=sly
<include>**/*.properties</include>
然后我尝试另一种方法,即创建文件DatasourceConfig.java并对其进行编码
package com.ycr;
import javax.sql.DataSource;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class DatasourceConfig {
@Bean
public DataSource datasource() {
return DataSourceBuilder.create().driverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
.url("jdbc:sqlserver://localhost:1433;DatabaseName=msdb").username("sa").password("sly").build();
}
}
,它起作用。 我不知道为什么它无法读取我的yml文件?(将yml更改为属性不起作用。)
我想知道为什么它无法读取我的yml文件?(将yml更改为属性不起作用。)
我想知道为什么它无法读取我的yml文件?(将yml更改为属性不起作用。)