思考:
運(yùn)行程序發(fā)現(xiàn)bug,找到原因是因?yàn)閐ao層沒(méi)加注解榛瓮。很不理解铺董,為什么傳一個(gè)實(shí)體類中包含的參數(shù),還需要加注解禀晓【總結(jié)了關(guān)于@Param的基本用法
1,使用@Param注解
當(dāng)以下面的方式進(jìn)行寫(xiě)SQL語(yǔ)句時(shí):
@Select("select column from table where userid = #{userid} ")
public int selectColumn(int userid);
當(dāng)你使用了使用@Param注解來(lái)聲明參數(shù)時(shí)粹懒,如果使用 #{} 或 ${} 的方式都可以重付。
@Select("select column from table where userid = ${userid} ")
public int selectColumn(@Param("userid") int userid);
當(dāng)你不使用@Param注解來(lái)聲明參數(shù)時(shí),必須使用使用 #{}方式凫乖。如果使用 ${} 的方式确垫,會(huì)報(bào)錯(cuò)。
@Select("select column from table where userid = ${userid} ")
public int selectColumn(@Param("userid") int userid);
2帽芽,不使用@Param注解
不使用@Param注解時(shí)删掀,參數(shù)只能有一個(gè),并且是Javabean导街。在SQL語(yǔ)句里可以引用JavaBean的屬性披泪,而且只能引用JavaBean的屬性。
// 這里id是user的屬性
@Select("SELECT * from Table where id = ${id}")
Enchashment selectUserById(User user);
作用
- @Param注解單一屬性
示例
`dao層`
Public User selectUser(@param(“userName”) String name,@param(“userpassword”) String password);
`XML文件`
<select id=" selectUser" resultMap="BaseResultMap">
select * from user_user_t where user_name = #{userName搬瑰,jdbcType=VARCHAR} and user_password=#{userPassword,jdbcType=VARCHAR}
</select>
- @Param注解JavaBean對(duì)象
示例
dao層
public List<user> getUserInformation(@Param("user") User user);
`XML文件`
<select id="getUserInformation" parameterType="com.github.demo.vo.User" resultMap="userMapper">
select
<include refid="User_Base_Column_List" />
from mo_user t where 1=1
<!-- 因?yàn)閭鬟M(jìn)來(lái)的是對(duì)象所以這樣寫(xiě)是取不到值得 -->
<if test="user.userName!=null and user.userName!=''"> and t.user_name = #{user.userName} </if>
<if test="user.userAge!=null and user.userAge!=''"> and t.user_age = #{user.userAge} </if>
</select>