为什么mybatis需要@Param?
Why mybatis need @Param?
User selectUser(@param(“name”)String name,@param(“password”)String password);
<select id=" selectUser" resultMap="BaseResultMap">
select * from user where user_name = #{name} and user_password=#{password}
</select>
像这段代码,mybatis可以使用反射获取变量名,变量名不同,可以替换@param(“XXX”)
.
回答实际问题:
默认情况下,方法的参数名称不是字节码的一部分。所以你的提议默认是行不通的。
MyBatis 对无法确定名称的参数使用通用名称:
[...] 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.
因此,如果您在 SQL 代码中使用这些名称,应该可以正常工作。
如果您为 javac 指定 -parameters
编译器标志,它确实会包含方法参数的名称,并且您建议的方法原则上是可行的。我看到一个教程声称 MyBatis 将使用这些,尽管参考文档似乎没有提到它。你可以试试看。
User selectUser(@param(“name”)String name,@param(“password”)String password);
<select id=" selectUser" resultMap="BaseResultMap">
select * from user where user_name = #{name} and user_password=#{password}
</select>
像这段代码,mybatis可以使用反射获取变量名,变量名不同,可以替换@param(“XXX”)
.
回答实际问题:
默认情况下,方法的参数名称不是字节码的一部分。所以你的提议默认是行不通的。
MyBatis 对无法确定名称的参数使用通用名称:
[...] 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.
因此,如果您在 SQL 代码中使用这些名称,应该可以正常工作。
如果您为 javac 指定 -parameters
编译器标志,它确实会包含方法参数的名称,并且您建议的方法原则上是可行的。我看到一个教程声称 MyBatis 将使用这些,尽管参考文档似乎没有提到它。你可以试试看。