java - 无法解析类或包 'h2'

标签 java spring maven spring-boot h2

我为我的网络应用程序开发后端应用程序。 在我的项目(SpringBoot + Maven)中,我想添加 h2 数据库。 根据网络教程:

  1. application.properties 文件中添加了以下行:

     server.port = 8088
     spring.h2.console.enabled=true
     spring.h2.console.path=/h2
     spring.datasource.url=jdbc:h2:mem:testdb
     spring.datasource.username=sa
     spring.datasource.password=
     spring.datasource.driverClassName=org.h2.Driver    //h2 seems to be NOT found
     spring.jpa.show-sql=true
    
  2. pom.xml 文件中添加了以下依赖项:

     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-data-jpa</artifactId>
     </dependency>
     <dependency>
         <groupId>com.h2database</groupId>
         <artifactId>h2</artifactId>
     </dependency>
    

问题:

application.properties 中:“无法解析类或包‘h2’”

全面检查:“检查 Spring Boot 应用程序 .properties 配置文件。突出显示未解析和弃用的配置键和无效值。仅适用于 Spring Boot 1.2 或更高版本。”

当然,我的 Spring Boot 高于 1.2(1.5.8)。 我在网络上发现了类似的问题,但是“重新定位依赖项”不起作用,所以我仍然没有解决方案:)。请帮忙。

最佳答案

看起来问题出在您的 pom 文件中。请找到我与您的 application.properties 文件核对过的 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.mberazouski.stackoverflow</groupId>
    <artifactId>spring-boot-hibernate</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-boot-hibernate</name>
    <description>Demo project for Spring Boot And Hibernate</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/>
    </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>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
</project>

一旦启动 spring boot,检查 h2-console: http://localhost:8088/h2

enter image description here

希望这会有所帮助。 附言如果您不知道,我建议您使用 http://start.spring.io/用于生成您的项目。您可以在那里选择您计划使用的任何组件。有关更多信息,您可以阅读例如 here .

关于java - 无法解析类或包 'h2',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50890147/

相关文章:

java - 如何在 Spring Boot 中仅记录错误

maven - scm 连接与 developerConnection

java - 使用 Maven 配置构建 Spring Boot 配置文件特定的属性

java - 如何创建我的项目使用的所有 jar 的内部远程存储库?

Java XSSFSheet 无法获取正确的日期值

java - @Qualifier 在 Junit4 中不起作用

java - Spring Batch-MySQL问题

java - 在 Spring Data MongoDB 存储库中计数

java - 复杂模式匹配

java - GC 会在 Android 上自动处理/释放在 jni 中分配的内存吗?