详解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-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()); } }
到此这篇关于详解mybatis-plus的 mapper.xml 路径配置的坑的文章就介绍到这了,更多相关mybatis-plus mapper.xml路径配置内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: Hive的DDL--外部表&like复制(第七讲)
下一篇: AI怎么设计桃花镂空图案的标志?