当前位置: 首页 > 知识库问答 >
问题:

Springboot打包jar在本地终端运行java -jar后说未找到Mybits映射文件?

安高翰
2024-06-15

Springboot打包jar在本地终端运行java -jar后说未找到Mybits映射文件,

2024-06-14 17:06:50.588  INFO 29408 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to 1 default profile: "default"2024-06-14 17:06:51.246  WARN 29408 --- [           main] o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[com.example.demo]' package. Please check your configuration.2024-06-14 17:06:51.336  INFO 29408 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.2024-06-14 17:06:51.358  INFO 29408 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 7 ms. Found 0 JPA repository interfaces.2024-06-14 17:06:51.491  WARN 29408 --- [           main] o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[com.example.demo]' package. Please check your configuration.

但是我在IDEA里面运行是没错的,这是我的项目结构:
image.png
之后我又检查了一下打包后的jar文件,发现目录demo-0.0.1-SNAPSHOT\BOOT-INF\classes\com\example\demo\demos\web的所有文件夹里面都没有我的代码Dao,Entity还有controller和service那些,只有三个其他文件,我不知道是不是全部整合进来了?image.png

之后我打开那三个文件看了一下,发现好像也不是
BasicController:

//// Source code recreated from a .class file by IntelliJ IDEA// (powered by FernFlower decompiler)//package com.example.demo.demos.web;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class BasicController {    public BasicController() {    }    @RequestMapping({"/hello"})    @ResponseBody    public String hello(@RequestParam(name = "name",defaultValue = "unknown user") String name) {        return "Hello " + name;    }    @RequestMapping({"/user"})    @ResponseBody    public User user() {        User user = new User();        user.setName("theonefx");        user.setAge(666);        return user;    }    @RequestMapping({"/save_user"})    @ResponseBody    public String saveUser(User u) {        return "user will save: name=" + u.getName() + ", age=" + u.getAge();    }    @RequestMapping({"/html"})    public String html() {        return "index.html";    }    @ModelAttribute    public void parseUser(@RequestParam(name = "name",defaultValue = "unknown user") String name, @RequestParam(name = "age",defaultValue = "12") Integer age, User user) {        user.setName("zhangsan");        user.setAge(18);    }}

PathVariableController:

//// Source code recreated from a .class file by IntelliJ IDEA// (powered by FernFlower decompiler)//package com.example.demo.demos.web;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class PathVariableController {    public PathVariableController() {    }    @RequestMapping(        value = {"/user/{userId}/roles/{roleId}"},        method = {RequestMethod.GET}    )    @ResponseBody    public String getLogin(@PathVariable("userId") String userId, @PathVariable("roleId") String roleId) {        return "User Id : " + userId + " Role Id : " + roleId;    }    @RequestMapping(        value = {"/javabeat/{regexp1:[a-z-]+}"},        method = {RequestMethod.GET}    )    @ResponseBody    public String getRegExp(@PathVariable("regexp1") String regexp1) {        return "URI Part : " + regexp1;    }}

我的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>2.7.1</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <groupId>com.example</groupId>    <artifactId>demo</artifactId>    <version>0.0.1-SNAPSHOT</version>    <name>demo</name>    <description>Demo project for Spring Boot</description>    <properties>        <java.version>1.8</java.version>    </properties>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>            <version>2.7.1</version>        </dependency>        <dependency>            <groupId>org.mybatis.spring.boot</groupId>            <artifactId>mybatis-spring-boot-starter</artifactId>            <version>2.2.2</version>        </dependency>        <dependency>            <groupId>com.baomidou</groupId>            <artifactId>mybatis-plus-boot-starter</artifactId>            <version>3.1.2</version>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-devtools</artifactId>            <scope>provided</scope>            <optional>true</optional>            <version>2.7.18</version>        </dependency>        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>            <version>8.0.33</version>        </dependency>        <dependency>            <groupId>org.projectlombok</groupId>            <artifactId>lombok</artifactId>            <optional>true</optional>            <version>1.18.30</version>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>            <version>2.7.1</version>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-thymeleaf</artifactId>            <version>2.7.18</version>        </dependency>        <dependency>            <groupId>org.junit.jupiter</groupId>            <artifactId>junit-jupiter-api</artifactId>            <version>5.8.1</version>            <scope>test</scope>        </dependency>        <dependency>            <groupId>org.apache.maven.plugins</groupId>            <artifactId>maven-site-plugin</artifactId>            <version>2.4</version>            <scope>provided</scope>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-data-jpa</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter</artifactId>            <version>2.7.18</version>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>                <version>2.6.0</version>            </plugin>        </plugins>    </build>    <packaging>war</packaging></project>

我的application.yml:

spring:  datasource:    driver-class-name: com.mysql.cj.jdbc.Driver    url: jdbc:mysql://47.93.153.170:3306/ren?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL    username: ren    password: 123456789  web:    resources:      static-locations: classpath:/templates/, classpath:/static/logging:  level:    org.springframework.data.jpa: DEBUG    com.zaxxer.hikari: DEBUG

我该怎么解决这个?是没有打包进来吗?还是全在另外三个文件里面了?还是我pom.xml配置有问题吗?

共有1个答案

夏朝
2024-06-15

根据你提供的信息和错误日志,你的Spring Boot应用在打包成JAR后运行时找不到MyBatis的映射文件。这通常是因为MyBatis在尝试扫描映射文件时没有找到预期的路径或文件。以下是一些可能的原因和解决方案:

  1. 检查映射文件的位置

    • 确保MyBatis的映射文件(.xml文件)位于正确的目录下。通常,它们应该放在与Mapper接口相同的路径下,或者在mybatis.configuration.mapperLocations配置中指定的路径下。
    • 当你运行IDEA时,可能IDE已经正确配置了资源路径,但在打包成JAR后,这些路径可能发生了变化。
  2. 检查pom.xml中的资源配置

    • 确保pom.xml中的build部分包含了正确的资源目录配置。Spring Boot Maven插件通常会自动包含src/main/resources目录下的所有资源,但如果你有特殊的需求,可能需要手动配置。
  3. 检查MyBatis配置

    • 查看你的MyBatis配置(可能是application.yml或其他配置文件),确保mapperLocations属性(或其他相关的路径配置)正确指向了映射文件的位置。
  4. 检查打包后的JAR

    • 使用jar tf your-app.jar命令来查看JAR文件的内容,确认映射文件是否确实被打包进去了。
  5. 检查MyBatis扫描路径

    • 根据你的错误日志,MyBatis尝试在com.example.demo包下扫描Mapper,但这可能不是你映射文件所在的位置。检查你的Mapper接口和映射文件是否位于同一包下,或者是否配置了正确的扫描路径。
  6. 清理和重新构建

    • 有时候,旧的构建文件或缓存可能会导致问题。尝试清理项目(例如,使用Maven的mvn clean命令)并重新构建。
  7. 检查Spring Boot版本兼容性

    • 你的pom.xml中包含了多个不同版本的Spring Boot依赖项,这可能会导致版本冲突或不一致的行为。尝试统一所有Spring Boot相关依赖的版本号。
  8. 使用Maven插件的正确版本

    • 你的pom.xmlspring-boot-maven-plugin的版本是2.6.0,而你的Spring Boot父POM版本是2.7.1。这可能会导致插件与你的Spring Boot版本不兼容。尝试将Maven插件的版本更新为与Spring Boot父POM相同的版本。

综上所述,你需要确保映射文件的位置和配置正确,并且它们确实被打包进了JAR文件中。同时,检查你的pom.xml配置和Spring Boot版本兼容性,确保没有版本冲突或不一致。最后,清理并重新构建项目,看看问题是否得到解决。

 类似资料:
  • 请帮我把它修好,怎么做。我完全是在Eclipse上做的。

  • 我有一个程序,必须从终端界面播放声音。 代码相当简单,如下所示: 该文件位于“我的源路径”中的“音乐”文件夹中。 当我在eclipse中运行程序时,所有工作都非常好。但是如果我将其导出到. jar文件中并在windows cmd中尝试它,我会收到这条消息 [编辑]音频文件确实已打包到中。jar,但它仍然不起作用。 是否有可能播放来自windows提示符的声音?如果没有,是否有这样的声音? 谢谢Si

  • 在一个项目中,我们将Hibernate与HikariCP结合使用,并且在Eclipse中一切都很好。但一旦我生成了一个jar文件(Maven),就再也找不到hikaricp了。我已经从各个可能的角度考虑了这个问题,但我无法找出问题所在。。。 坚持不懈xml 波姆。xml 如果我在Eclipse中运行此功能,则一切正常: 01:14:05,436信息HikariDataSource: 70-Hika

  • 问题内容: 我正在通过Java程序执行exe。该路径是用Java硬编码的。 我已经把我的exe包装在罐子里。 但是由于我在Java文件中硬编码了路径名而感到困惑,因此我无法将jar作为独立程序执行。 是否有任何包装此类jar的提示,即内部具有exe并能够作为独立程序运行? 问题答案: 这会将提取.exe到本地磁盘上的本地文件。当Java程序存在时,该文件将被删除。

  • 我试图在命令行中使用依赖项运行我的java类。我使用IntelliJ IDEA13从我的项目中构建工件。我有一个名为“main.java”的主类,在构建之后,我得到了一个类似MyTest.jar的jar文件。java的全限定名类似于“test.main” 但是当我试图使用“java mytest.jar”运行它时,它抱怨找不到主类mytest.jar。 ----Main.class --META-

  • 我在Eclipse中创建了一个项目,该项目空间中的文件夹中有图像文件。 我一整天都在用它创建. jars,但现在我不知何故把它搞砸了。 我正在测试如果我从项目中删除了图像文件会发生什么,但它显然不起作用(正如我预期的那样)。但当我把文件复制回来并试图重新打包项目时,它仍然像我没有图像文件时一样。。我可以清楚地看到,图像没有被打包到罐子里,因为。jar文件只有200kb,而带有图像的jar文件只有3

  • 我已经从spring入门指南下载了源代码(zip文件):http://spring.io/guides/gs/rest-service/ 在文件夹“Complete”中生成整个项目已成功。使用Gradle(Gradle run)运行生成的jar文件也是成功的。但当我尝试在Windows命令行中使用“java-jar build\libs\gs-rest-service-0.1.0.jar”手动运行

  • 问题内容: 我有一个我从未有过的问题,无法在网络上找到解决方案。我有一个小的Programm,它使用一些图像来打印菜单。 这是我用来打印图像的类: 我的图像都是类路径的一部分,我称之为: 正常工作,直到我将程序打包到jar文件中并从控制台使用以下命令运行: 我的程序启动正常,但是没有图像打印。没有例外,没有错误,什么都没有。 什么会导致这种行为? 问题答案: 首先,请确保您的资源已正确加载(例如,