用注解來簡(jiǎn)化xml配置的時(shí)候,@Param注解的作用是給參數(shù)命名,參數(shù)命名后就能根據(jù)名字得到參數(shù)值,正確的將參數(shù)傳入sql語句中
Mapper示例
public interface SysRoleMapper extends BaseMapper<SysRole> {
List<SysRole> getRoleIdentity(@Param("roleType") String roleType);
}
xml映射對(duì)應(yīng)示例
<!--查詢用戶身份-->
<select id="getRoleIdentity" resultType="com.wm.adminbackend.entity.role.SysRole">
SELECT id,name FROM sys_role WHERE role_type =#{roleType}
</select>
采用#{}的方式把@Param注解括號(hào)內(nèi)的參數(shù)進(jìn)行引用
使用@Param注解
當(dāng)以下面的方式進(jìn)行寫SQL語句時(shí):
@Select("select column from table where userid = #{userid} ")
public int selectColumn(int userid);
當(dāng)你使用了使用@Param注解來聲明參數(shù)時(shí),如果使用 #{} 或 ${} 的方式都可以。
@Select("select column from table where userid = ${userid} ")
public int selectColumn(@Param("userid") int userid);
當(dāng)你不使用@Param注解來聲明參數(shù)時(shí)俯树,必須使用使用 #{}方式那婉。如果使用 ${} 的方式,會(huì)報(bào)錯(cuò)遇革。
@Select("select column from table where userid = ${userid} ")
public int selectColumn(@Param("userid") int userid);
不使用@Param注解
不使用@Param注解時(shí),參數(shù)只能有一個(gè),并且是Javabean烘嘱。在SQL語句里可以引用JavaBean的屬性昆禽,而且只能引用JavaBean的屬性。
// 這里id是user的屬性
@Select("SELECT * from Table where id = ${id}")
Enchashment selectUserById(User user);