9

MyBatis 一个比较奇怪的参数绑定问题

 3 years ago
source link: https://www.v2ex.com/t/776265
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  Java

MyBatis 一个比较奇怪的参数绑定问题

  anzu · 20 小时 21 分钟前 · 643 次点击

众所周知如果 Mapper 需要多个参数时,需要在每个参数前加上 @ Param 注解,如:

User findByUserIdAndName(@Param("userId") Integer userId, @Param("name") String name);

但是我不加也可以,于是我没加,正常运行。 现在重构项目,拆分成父子项目,结果全部报错:org.apache.ibatis.binding.BindingException: Parameter 'xxx' not found. 网上都说是没加 @ Param 的原因,的确现在加上就不报错,但是为什么我之前没加也可以呢?

第 1 条附言  ·  19 小时 14 分钟前

我懂了! 根据这里的回答 https://stackoverflow.com/a/49316086

To allow the input to be mapped to the operation method’s parameters, code implementing an endpoint should be compiled with -parameters. This will happen automatically if you are using Spring Boot’s Gradle plugin or if you are using Maven and spring-boot-starter-parent.

查看 spring-boot-starter-parent pom 文件,可以看到maven-compiler-plugin设置了 -parameters

所以 spring 项目编译时会自动加上 -parameters

但是,现在我重构了项目,删除了 spring-boot-starter-parent,把spring-boot-dependencies加入父项目的dependencyManagement,也就是

    <dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-dependencies</artifactId>
	<type>pom</type>
	<version>2.4.5</version>
	<scope>import</scope>
    </dependency>

但是这家伙没有设置 -parameters,于是查询报错


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK