详解mybatis-plus的 mapper.xml 路径配置的坑

时间:2022-06-01 21:40:45

mybatis-plus今天遇到一个问题,就是mybatis 没有读取到mapper.xml 文件。

特此记录一下,问题如下:

?
1
2
3
4
5
6
7
8
9
10
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.husy.mapper.SystemUserMapper.findUserByName
 
    at com.baomidou.mybatisplus.core.override.MybatisMapperMethod$SqlCommand.<init>(MybatisMapperMethod.java:242)
    at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.<init>(MybatisMapperMethod.java:54)
    at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.lambda$cachedMapperMethod$0(MybatisMapperProxy.java:65)
    at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660)
    at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.cachedMapperMethod(MybatisMapperProxy.java:65)
    at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:60)
    at com.sun.proxy.$Proxy72.findUserByName(Unknown Source)
    at com.husy.service.impl.SystemUserServiceImpl.findUserByName(SystemUserServiceImpl.java:23)

错误代码如下:

mapper.xml 目录

详解mybatis-plus的 mapper.xml 路径配置的坑

代码如下:

单元测试

?
1
2
3
4
5
@Test
public void findUser(){
    SystemUser systemUser= systemUserService.findUserByName("admin");
    System.out.println(systemUser.toString());
}

mybatis-puls 配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
@EnableTransactionManagement
@Configuration
@MapperScan("com.husy.mapper")
public class MybatisConfig {
 
  /**
   * mybatis-plus 分页插件
   */
  @Bean
  public PaginationInterceptor paginationInterceptor() {
    return new PaginationInterceptor();
  }
}

service实现

?
1
2
3
4
5
6
7
8
9
10
@Service
public class SystemUserServiceImpl implements SystemUserService {
    @Autowired
    private SystemUserMapper userMapper;
 
    @Override
    public SystemUser findUserByName(String name) {
        return userMapper.findUserByName(name);
    }
}

mapper 接口

?
1
2
3
4
@Component
public interface SystemUserMapper {
    SystemUser findUserByName(@Param("userAccount") String name);
}

mapper.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.husy.mapper.SystemUserMapper">
  <resultMap id="systemUserMap" type="com.husy.domain.SystemUser" >
    <id column="user_id" property="userId" />
    <result column="user_account" property="userAccount" />
    <result column="user_password" property="userPassword" />
    <result column="user_phone" property="userPhone" />
  </resultMap>
  <select id="findUserByName" resultMap="systemUserMap">
     SELECT
      user_id,
      user_account,
      user_password,
      user_phone
     FROM t_system_user
     where user_account = #{userAccount}
  </select>
</mapper>

通过上面的代码可以看出。mapper接口中的方法和映射文件中的方法名称是一样的。不存在名称错误导致的情况,返回值,参数类型等你都正确。如果找不到方法,那一定是映射文件配置问题,只有没有读取到,才会出现找不到的情况。
我的配置如下:

问题出错的关键位置

我这里引用的是 mybatis-plus-boot-starte 依赖

?
1
2
3
4
5
<dependency>
  <groupId>com.baomidou</groupId>
   <artifactId>mybatis-plus-boot-starter</artifactId>
   <version>3.1.0</version>
 </dependency>

mapper.xml 的文件引用路径配置如下:

?
1
mybatis.mapper-locations=classpath:mapper/*.xml

这就导致,mybatis 读取不到 mapper映射文件。

经过查阅:

  • 如果引用mybatis-plus-boot-starter 依赖,需要配置 mybatis-plus.mapper-locations
  • 如果引用mybatis-plus 依赖,需要配置 mybatis.mapper-locations

如下:

引用 mybatis-plus 包

?
1
2
3
4
5
<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>mybatis-plus</artifactId>
  <version>3.1.0</version>
</dependency>
?
1
mybatis.mapper-locations=classpath:mapper/*.xml

引用 mybatis-plus-boot-starter 包

?
1
2
3
4
5
<dependency>
   <groupId>com.baomidou</groupId>
   <artifactId>mybatis-plus-boot-starter</artifactId>
   <version>3.1.0</version>
 </dependency>
?
1
mybatis-plus.mapper-locations=classpath:mapper/*.xml

只要选用其中一种方式,就没有问题了。

今天看到评论区有小伙伴说没有作用,这里给了一份Demo ,有问题的小伙伴可以比对一下。

POM

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?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.2.2.RELEASE</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.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>2.1.1</version>
    </dependency>
 
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-boot-starter</artifactId>
      <version>3.3.0</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>org.junit.vintage</groupId>
          <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>
 
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
 
</project>

application.properties

?
1
2
3
4
5
6
7
# DataSource Config
spring.datasource.url=jdbc:mysql://localhost:3306/db_husy?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8&zeroDateTimeBehavior=CONVERT_TO_NULL
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
 
mybatis-plus.mapper-locations=classpath:/mapper/*.xml

MybatisConfig

?
1
2
3
4
5
6
7
8
9
10
@EnableTransactionManagement
@Configuration
@MapperScan("com.example.demo.mapper")
public class MybatisConfig {
    /**mybatis-plus 分页插件*/
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

其他文件基本没变化。附上目录

详解mybatis-plus的 mapper.xml 路径配置的坑

单元测试

?
1
2
3
4
5
6
7
8
9
10
11
@SpringBootTest
class DemoApplicationTests {
    @Autowired
    SystemUserService systemUserService;
    @Test
    public void findUser(){
        SystemUser systemUser= systemUserService.findUserByName("admin");
        System.out.println(systemUser.toString());
    }
 
}

详解mybatis-plus的 mapper.xml 路径配置的坑

到此这篇关于详解mybatis-plus的 mapper.xml 路径配置的坑的文章就介绍到这了,更多相关mybatis-plus mapper.xml路径配置内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/u013234928/article/details/94060733