mybatis中@Param的使用


@Param:當映射器方法需要多個參數時,這個注解可以被用於:給映射器方法中的每個參數來取一個名字。否則,多參數將會以它們的順序位置和SQL語句中的表達式進行映射,這是默認的。
       語法要求:若使用@Param("id"),則SQL中參數應該被命名為:#{id}。

用代碼說明:

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.game.domain.User;

/**
 * UserMapper接口
 */
public interface UserMapper {
    
    //根據用戶的用戶名、密碼判斷登錄
    @Select("select * from user where userName = #{UserName} and userPwd = #{UserPwd}")
    User findWithLoginnameAndPassword(@Param("UserName")String Name,
            @Param("UserPwd") String Pwd);
    
    //根據id刪除用戶
    @Delete(" delete from user where userID = #{id}")
    void deleteById(@Param("id") Integer ID);

 

這里:@Param("UserName")注解表示給該注解后面的參數(String Name)取一個參數名稱(命名為UserName),對應@Select注解中的#{UserName}。
   如果沒有使用@Param注解,則參數將會以它們的順序位置來和SQL語句中的表達式進行映射。
   然后sql語句:select * from user where userName = #{UserName} and userPwd = #{UserPwd}中,就可以根據"UserName"和"UserPwd"得到參數值了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM