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里面运行是没错的,这是我的项目结构:
之后我又检查了一下打包后的jar文件,发现目录demo-0.0.1-SNAPSHOT\BOOT-INF\classes\com\example\demo\demos\web的所有文件夹里面都没有我的代码Dao,Entity还有controller和service那些,只有三个其他文件,我不知道是不是全部整合进来了?
之后我打开那三个文件看了一下,发现好像也不是
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;
@Controller
public 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;
@Controller
public 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配置有问题吗?
原因应该是和楼上说的差不多,你使用IDEA的时候把源文件夹、资源文件夹和测试文件夹搞错了,你图中代码所在文件夹(绿色标识)是测试文件夹,当然不会被打包进war包里了,这是一个正确的目录示范:

蓝色的java目录是源文件夹,要打包的代码应该写在这下面
绿色的java目录是测试文件夹,本地编译启动的时候也能正常编译运行,但是不会被打包
点击
文件
->项目结构
->模块
->源
->点击文件夹
->选择标记为xxx类型文件夹
当然你也可以选择把你测试文件夹目录下的文件直接剪切到源目录下