MyBatis @Param Usage: Complete Guide
In MyBatis, the @Param annotation is used to give aliases to parameters of Mapper methods, allowing them to be referenced in SQL statements. The @Param annotation can only be used on method parameters in Mapper interfaces, not on the methods themselves.
For example, suppose there is a Mapper interface as follows:
public interface UserMapper {
User getUserById(@Param("id") Long id);
}
In the example above, the @Param annotation assigns an alias “id” to the id parameter of the getUserById method. This alias can be used to reference the parameter in the corresponding XML mapping file, as shown below:
<select id="getUserById" resultType="User">
SELECT * FROM user WHERE id = #{id}
</select>
This allows for referencing parameters in SQL statements using the alias specified by the @Param annotation, making the SQL statement clearer and easier to read.