Cannot resolve class or package 'h2'

白昼怎懂夜的黑 提交于 2019-12-23 12:19:08

问题


I prepere back-end for my web app. In my project (SpringBoot + Maven) i would like to add h2 date base. According to tutorials from web:

  1. Added to 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. Added dependencies to 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>
    

PROBLEM:

in application.properties : "Cannot resolve class or package 'h2'"

full inspection: "Checks Spring Boot application .properties configuration files. Highlights unresolved and deprecated configuration keys and invalid values. Works only for Spring Boot 1.2 or higher."

Certainly, my Spring Boot is higher than 1.2 (1.5.8). I've found similar problem in web, but "reloding dependencies" doesnt work, so still i dont have a solution :). Please help.


回答1:


Looks like the issue is somewhere in your pom file. Please find my pom.xml file which I checked with your application.properties file.

<?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>

Once you spring boot will start, check h2-console: http://localhost:8088/h2

Hope this will help. P.S. If you are not aware I suggest you use http://start.spring.io/ for generating of your projects. There you can pick any component which you plan to use. More about that you can read for example here.



来源:https://stackoverflow.com/questions/50890147/cannot-resolve-class-or-package-h2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!