文章目录
1. 复现问题
今天在测试项目时,如下代码所示:
@SpringBootTest
class LowCodeApplicationTests {
@Autowired private Userservice userservice;
@Test
void contextLoads() {
List<User> users = userservice.queryByParam("测试");
for (User user : users) {
System.out.println(user);
}
}
出现如下错误信息:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.cloud.lowcode.mapper.UserMapper.queryByParam
at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:235)
at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:53)
at org.apache.ibatis.binding.MapperProxy.lambda$cachedInvoker$0(MapperProxy.java:108)
at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660)
at org.apache.ibatis.util.MapUtil.computeIfAbsent(MapUtil.java:35)
at org.apache.ibatis.binding.MapperProxy.cachedInvoker(MapperProxy.java:95)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:86)
at com.sun.proxy.$Proxy109.queryByParam(Unknown Source)
at com.cloud.lowcode.service.impl.userServiceImpl.queryByParam(userServiceImpl.java:25)
at com.cloud.lowcode.service.impl.userServiceImpl$$FastClassBySpringCGLIB$$298dc132.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
at com.cloud.lowcode.service.impl.userServiceImpl$$EnhancerBySpringCGLIB$$7566cc.queryByParam(<generated>)
at com.cloud.lowcode.LowCodeApplicationTests.contextLoads(LowCodeApplicationTests.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:688)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
....
即错误信息为:nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.cloud.lowcode.mapper.UserMapper.queryByParam
。
2. 分析问题
将该错误信息翻译成中文为嵌套异常是 org.apache.ibatis.binding.BindingException: 无法绑定当前com.cloud.lowcode.mapper包下的UserMapper类的queryByParam方法
。
2.1 检查启动类的配置
检查启动类是否加上MapperScan
,如下已加上该注解:
@SpringBootApplication
@MapperScan("com.**.mapper")
public class LowCodeApplication {
public static void main(String[] args) {
SpringApplication.run(LowCodeApplication.class, args);
}
}
2.2 检查xml文件对应java类的配置是否有误
如下是userMapper.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.cloud.lowcode.mapper.UserMapper">
<select id="queryByParam" parameterType="string" resultType="com.cloud.lowcode.entity.po.User">
SELECT * FROM `user`
<where>
<if test="username != null || username.length > 0">
name = #{username}
</if>
</where>
</select>
</mapper>
再看对应的UserMapper.java
类,如下所示:
package com.cloud.lowcode.mapper;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.cloud.lowcode.entity.po.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* @author 念兮为美
* @datetime 2022/11/28 13:51
* @desc 用户实体类
*/
@Mapper
public interface UserMapper extends BaseMapper<User> {
List<User> queryByParam(String username);
}
userMapper.xml
和userMapper.java
对应如下:
-
userMapper.xml
和UserMapper.java
的名字一样(不包括后缀名) -
userMapper.xml
中的namespace
为com.cloud.lowcode.mapper.UserMapper
对应UserMapper.java
中的包名+类名,没问题 -
userMapper.xml
中的id
为queryByParam
对应UserMapper.java
中的方法名,没问题 -
userMapper.xml
中的参数类型parameterType
为string
对应UserMapper.java
中的queryByParam
字符串类型,没问题 -
经检查
resultType
也没问题
2.3 检查application.yml文件的mybatis配置是否有误
如下是application.yml
配置的mybatis
信息:
#mybatis配置
mybatis:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath*:/mapper/*Mapper.xml
type-aliases-package: com.cloud.lowcode.entity.po
该配置信息没有问题。
2.4 检查pon.xml文件
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.yml</include>
</includes>
</resource>
</resources>
</build>
pom.xml
中已配置了resource
信息,也没有问题。
以上四种情况,都没有问题,那么,问题出现在哪里呢?
于是,再去看mybatis
的依赖,如下所示:
<properties>
<java.version>1.8</java.version>
<mybatisStarter.version>2.2.2</mybatisStarter.version>
<baomidouStarter.version>1.0.5</baomidouStarter.version>
<baomidouMybatisPlus.version>2.1.9</baomidouMybatisPlus.version>
</properties>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatisStarter.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatisplus-spring-boot-starter</artifactId>
<version>${baomidouStarter.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>${baomidouMybatisPlus.version}</version>
</dependency>
忽然发现,我引入了baomidou
包,因而,配置文件application.yml
需要修改。
3. 解决问题
由于引入baomidou
包,,配置文件application.yml
中的mybatis
便不能这样配置:
#mybatis配置
mybatis:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath*:/mapper/*Mapper.xml
type-aliases-package: com.cloud.lowcode.entity.po
而应该如是配置:
#mybatis配置
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath*:/mapper/*Mapper.xml
type-aliases-package: com.cloud.lowcode.entity.po
修改后,重新启动测试类,如下代码所示:
4. 总结问题
-
检查启动类是否加上
MapperScan
注解 -
检查
xml
文件对应java
类的配置是否有误 -
检查
application.yml
文件的mybatis
配置是否有误 -
检查
pom.xml
文件中是否引入baomidou
包
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/99115.html