Far*_*med 5 java spring jdbc maven
我在运行Spring启动应用程序时遇到异常,它说无法创建数据源bean,导致此异常的原因是它无法检测到SQLServer的DriverClass.
这是我的application.properties文件:
spring.jpa.database-platform=org.hibernate.dialect.SQLServerDialect
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driverClassName=com.microsoft.jdbc.sqlserver.SQLServerDriver
spring.datasource.url=jdbc:sqlserver://localhost;databaseName=dbname
spring.datasource.username=name
spring.datasource.password=****
Run Code Online (Sandbox Code Playgroud)
我已经下载了sqljdbc连接器jar并将其放在lib文件夹中,并在我的pom.xml文件中提供它的引用.
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.myproject</groupId>
<artifactId>myproject-notifications</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>netpace-notifications</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<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.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/sqljdbc4-2.0.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
我尝试过使用这些标签:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
但它在我的pom.xml文件中出错,所以我必须使用其他方法.我是新手,如果有人让我知道我在这里做错了什么,我会非常感激.提前致谢.
Mar*_*eel 14
您正在尝试加载错误的驱动程序.名称的驱动程序com.microsoft.jdbc.sqlserver.SQLServerDriver来自非常旧的SQL Server 2000 JDBC驱动程序.
在SQL Server 2005 JDBC驱动程序中,Microsoft将其更改为com.microsoft.sqlserver.jdbc.SQLServerDriver(请注意在sqlserver和之间切换顺序jdbc.
附注:您使用的是旧版本的驱动程序.Microsoft已开源SQL Server JDBC驱动程序,它在maven上可用:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.2.2.jre8</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
有关实际的最新版本,请参阅https://github.com/Microsoft/mssql-jdbc.
小智 0
您的系统上存在该 jar 吗?您正在强制 maven 从系统获取依赖项,如果它不在该路径上,maven 将失败。您可以将标签替换为以下内容:
<dependency>
<groupId>com.microsoft</groupId>
<artifactId>sqljdbc4</artifactId>
<version>3.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
来源: https ://mvnrepository.com/artifact/com.microsoft/sqljdbc4/3.0