【问题标题】:why I have to define driver for sql db in maven project manually为什么我必须在 maven 项目中手动为 sql db 定义驱动程序
【发布时间】:2018-08-28 01:52:30
【问题描述】:

我在设置 maven 项目时遇到了问题,我创建了 springboot 应用程序,它连接到 postgres db,所以我需要一个驱动程序,我在 maven 依赖项中以 3 种不同方式指定了它:

  1. <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>
    
  2. <dependency> 
         <groupId>postgresql</groupId> 
         <artifactId>postgresql</artifactId> 
        <version>9.1-901-1.jdbc4</version> 
    </dependency> 
    
  3. <dependency> 
        <groupId>org.postgresql</groupId> 
        <artifactId>postgresql</artifactId> 
        <version>42.2.1</version> 
    </dependency> 
    

它们都不起作用,所以我必须通过 STS(eclipse) 中的项目属性指定它,方法是转到项目 -> 属性 -> java 构建路径 -> 库 -> 添加外部 jar
并设置在postgres jdcb jar文件之前下载的路径,尽管它已经在maven依赖项中指定,因为我在pom.xml中留下了第一个postgres依赖项声明,所以现在驱动程序在Maven依赖项和引用库中定义,现在它正在工作,我不'没有看到错误:

无法加载驱动类:org.postgresql.Driver

但有趣的是,我在另一台电脑上下载了该项目,我不必手动指定驱动程序,maven 自己完成了这项工作。有人知道这是怎么回事吗?

【问题讨论】:

  • 这对我来说很好用:&lt;dependency&lt;groupId&gt;org.postgresql&lt;/groupId&gt;&lt;artifactId&gt;postgresql&lt;/artifactId&gt;&lt;version&gt;9.4-1200-jdbc41&lt;/version&gt;&lt;/dependency&gt;
  • 这不是我尝试了 30 个版本的情况,上面提到了其中的 3 个主要版本,但它们都不是 maven 依赖项。我必须引用我磁盘上的一个 jar 文件才能使其工作。
  • 你是如何启动你的应用程序的?如果来自 Eclipse,那么如果 pom 发生更改,它可能不会执行自动导入。
  • 运行应用程序/运行springboot应用程序

标签: java postgresql maven spring-boot jdbc


【解决方案1】:

你在使用 SpringBoot maven 插件吗?它应该将所有必需的 jar 添加到类路径中。对于 postgres,您应该只需要这 2 个依赖项:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency> 
        <groupId>org.postgresql</groupId> 
        <artifactId>postgresql</artifactId> 
        <version>42.2.1</version> 
    </dependency> 
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

您可以在此处找到示例 pom: https://spring.io/guides/gs/relational-data-access/

【讨论】:

  • 是的,springboot 依赖项在 pom.xml 中,正如我所说的那样,我必须在一台电脑上手动定义它才能使其工作,而在另一台电脑上我不需要因为 maven依赖项工作正常
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-08
  • 1970-01-01
  • 2011-10-09
  • 2021-05-14
  • 1970-01-01
相关资源
最近更新 更多