java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > mybatis-plus mapper.xml路径配置

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

作者:一只倔强的码蚁

这篇文章主要介绍了详解mybatis-plus的 mapper.xml 路径配置的坑,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

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

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

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 目录


代码如下:

单元测试

@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 包

<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());
	}

}

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

您可能感兴趣的文章:
阅读全文