Mybatis的Mapper接口传递多个参数的时候必须要加@Param注解吗?

答案是:不一定,取决于mybatis的版本、jdk的版本和javac的编译选项。

测试代码

Maven依赖:

xml 复制代码
<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>2.3.1</version>
</dependency>

依赖的mybatis的版本是3.5.13

text 复制代码
[INFO] +- org.mybatis.spring.boot:mybatis-spring-boot-starter:jar:2.3.1:compile
[INFO] |  +- org.mybatis.spring.boot:mybatis-spring-boot-autoconfigure:jar:2.3.1:compile
[INFO] |  +- org.mybatis:mybatis:jar:3.5.13:compile
[INFO] |  \- org.mybatis:mybatis-spring:jar:2.1.1:compile

Mapper接口:

java 复制代码
@Mapper
public interface DeptMapper {
    @Select("select * from dept where name = #{name} and code = #{code}")
    List<Dept> selectByNameAndCode(String name, String code);
}

junit test:

java 复制代码
@Test
   public void testSelectByNameAndCode() {
       List<Dept> depts = deptMapper.selectByNameAndCode("学工部", "XUEGONG");
       System.out.println(depts);
   }

能正常输出。

原因解析

查看mybatis的官方文档

翻译一下:3.4.1之后提供了useActualParamName这个boolean的选项(默认是true),允许把接口方法的形参名作为mybatis语句的参数名,但是前提是项目需要用jdk8以上去编译并且需要添加编译选项-paramters

测试1)关闭useActualParamName

yml 复制代码
mybatis:
  configuration:
    use-actual-param-name: false

再运行就会报错:

text 复制代码
org.mybatis.spring.MyBatisSystemException: 
nested exception is org.apache.ibatis.binding.BindingException: 
Parameter 'name' not found. Available parameters are [0, 1, param1, param2]

测试2)关闭编译选项-parameters

idea->file->settings->compiler->java compiler

重新build项目,这样会重新编译class文件,再运行,依然报同样的错误。

看下org.apache.ibatis.reflection.ParamNameResolver#ParamNameResolver这个方法的源代码就足够了。

测试代码下载

结论

1)3.4.1之前,mapper接口传递多个参数的时候,必须要加@Param,不加就会报错。

2)3.4.1之后不一定,如果是开启了useActualParamName参数(默认就是开启的),并且在源代码编译的时候加上了-parameters参数(idea默认就添加了这个参数)就不需要加@Param了,否则还是需要加@Param的。

3)综上,建议还是加上吧。

相关推荐
x***44011 小时前
Spring-boot3.4最新版整合swagger和Mybatis-plus
mybatis
哈库纳玛塔塔11 小时前
放弃 MyBatis,拥抱新一代 Java 数据访问库
java·开发语言·数据库·mybatis·orm·dbvisitor
小马爱打代码20 小时前
MyBatis:插件模块详解
mybatis
Codeking__21 小时前
Redis初识——什么是Redis
数据库·redis·mybatis
y***n6141 天前
Spring Boot中集成MyBatis操作数据库详细教程
数据库·spring boot·mybatis
hqp1 天前
SQLite 不支持 LocalDateTime
sqlite·mybatis
用户8307196840821 天前
秒杀面试!MyBatis-Spring-Boot 初始化流程深度拆解
spring boot·mybatis
8***f3951 天前
Spring 中使用Mybatis,超详细
spring·tomcat·mybatis
w***76551 天前
Spring Boot 集成 MyBatis 全面讲解
spring boot·后端·mybatis
张较瘦_2 天前
SpringBoot3 | MyBatis-Plus 搞定宠物管理:从0到1实现增删改查
mybatis·宠物