Java中@Param注解

时间:2025-03-19 11:40:34

@Param:用来在DAO层中声明参数,如:

List<News> selectByUserIdAndOffset(@Param("userId") int userId, @Param("offset") int offset, @Param("limit") int limit);


当使用了使用@Param注解来声明参数时,如果使用 #{} 或 ${} 的方式都可以。

   

@Select("select entity from table where userId = ${userId} ")
   public int selectEntity(@Param("userId") int userId);

当不使用@Param注解来声明参数时,必须使用使用 #{}方式。如果使用 ${} 的方式,会报错

 

#{}拿到值之后,拼装sql,会自动对值添加引号

${}则把拿到的值直接拼装进sql,如果需要加单引号,必须手动添加,一般用于动态传入表名或字段名使用,#{}传参能防止sql注入