@Param注解在Mybatis中的使用 以及傳遞參數的三種方式


第一種:

Dao層的方法

public User selectUser(String name,String password);

對應的Mapper.xml

<select id="selectUser" resultMap="BaseResultMap"> select * from t_user where user_name = #{0} and user_password=#{1} </select>  

第二種:

該方法采用Map傳多參數

Dao層的方法

public User selectUser(Map paramMap);

對應的Mapper.xml

<select id="selectUser" resultMap="BaseResultMap"> select * from t_user where user_name = #{userName,jdbcType=VARCHAR} and user_password=#{userPassword,jdbcType=VARCHAR} </select>

Service層調用

public User xxxSelectUser(){ Map paramMap=new hashMap(); paramMap.put("userName","對應具體的參數值"); paramMap.put("userPassword","對應具體的參數值"); User user=xxx.selectUser(paramMap); }

個人認為此方法不夠直觀,見到接口方法不能直接的知道要傳的參數是什么。

第三種:@Param注解

Dao層的方法

public User selectUser(@Param("name")String name, @Param("password")String password);

對應的Mapper.xml

<select id=" selectUser" resultMap="BaseResultMap"> select * from t_user where user_name = #{name,jdbcType=VARCHAR} and user_password=#{password,jdbcType=VARCHAR} </select>

個人覺得三種之中這種可讀性最好,建議雙引號中的值和變量名保持一致

第一種占位符不夠直觀!


免責聲明!

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



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