为什么Mybatis Interceptor会获取重复的参数

Why does Mybatis Interceptor get duplicate parameters

我想在sql执行之前获取一些params,但是当我通过Interceptor获取params时我发现它是重复的,参数类型有基本类型和映射并且当我在一个映射中修改数据时,另一张地图中的数据也发生了变化

Gets a list of parameters

Modified the data in one map, the data in the other map also changed

这是 doc 的节选。

You can pass multiple parameters to a mapper method. If you do, they will be named by the literal "param" followed by their position in the parameter list by default, for example: #{param1}, #{param2} etc. If you wish to change the name of the parameters (multiple only), then you can use the @Param("paramName") annotation on the parameter.

虽然文档没有提到,default/implicit 名称(即 "param1"、"param2"、...)仍然可用,即使您使用 @Param明确命名参数。

因此,例如,以下两个声明实际上是相同的。

@Select("select * from users where id = #{id} and name = #{name}")
User select(@Param("id")Integer id, @Param("name")String name);
@Select("select * from users where id = #{param1} and name = #{param2}")
User select(@Param("id")Integer id, @Param("name")String name);

ℹ️如果您使用 @Param,我不建议使用 default/implicit 名称,因为这是一种未记录的行为,将来可能会中断。