亲宝软件园·资讯

展开

详解mybatis-plus配置找不到Mapper接口路径的坑

闭着眼睛写BUG 人气:0

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

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

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)org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.husy.mapper.SystemUserMapper.findUserByName

错误代码如下:

mapper.xml 目录

在这里插入图片描述

代码如下:

单元测试

@Test
public void findUser(){
	SystemUser systemUser= systemUserService.findUserByName("admin");
	System.out.println(systemUser.toString());
}

mybatis-puls 配置

@EnableTransactionManagement
@Configuration
@MapperScan("com.husy.mapper")
public class MybatisConfig {

  /**
   * mybatis-plus 分页插件
   */
  @Bean
  public PaginationInterceptor paginationInterceptor() {
    return new PaginationInterceptor();
  }
}

service实现

@Service
public class SystemUserServiceImpl implements SystemUserService {
	@Autowired
	private SystemUserMapper userMapper;

	@Override
	public SystemUser findUserByName(String name) {
		return userMapper.findUserByName(name);
	}
}

mapper 接口

@Component
public interface SystemUserMapper {
	SystemUser findUserByName(@Param("userAccount") String name);
}

mapper.xml

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

<dependency>
  <groupId>com.baomidou</groupId>
   <artifactId>mybatis-plus-boot-starter</artifactId>
   <version>3.1.0</version>
 </dependency>

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

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

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

经过查阅:

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

如下:

引用 mybatis-plus 包

<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>mybatis-plus</artifactId>
  <version>3.1.0</version>
</dependency>
mybatis.mapper-locations=classpath:mapper/*.xml

引用 mybatis-plus-boot-starter 包

<dependency>
   <groupId>com.baomidou</groupId>
   <artifactId>mybatis-plus-boot-starter</artifactId>
   <version>3.1.0</version>
 </dependency>
mybatis-plus.mapper-locations=classpath:mapper/*.xml

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

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

POM

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

# 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

@EnableTransactionManagement
@Configuration
@MapperScan("com.example.demo.mapper")
public class MybatisConfig {
	/**mybatis-plus 分页插件*/
	@Bean
	public PaginationInterceptor paginationInterceptor() {
		return new PaginationInterceptor();
	}
}

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

在这里插入图片描述

单元测试

@SpringBootTest
class DemoApplicationTests {
	@Autowired
	SystemUserService systemUserService;
	@Test
	public void findUser(){
		SystemUser systemUser= systemUserService.findUserByName("admin");
		System.out.println(systemUser.toString());
	}

}

在这里插入图片描述

加载全部内容

相关教程
猜你喜欢
用户评论