關于Mybatis的@param注解及多個傳參
1. Mybatis的@param注解
自定義對象也用@param注解
注:使用@param注解,mapper.xml 不加parameterType。
2. Mybatis 傳入多個參數(shù),解決方案
(1) 順序傳參法
public User selectUser(String name, int deptId); <select id="selectUser" resultMap="UserResultMap"> select * from user where user_name = #{0} and dept_id=#{}; </select>
說明: #{} 里面的數(shù)字代表傳入?yún)?shù)的順序 注:這種方式不建議使用,sql層表達不夠直觀,并不清楚參數(shù)名稱,一旦順序調整就會容易出錯。
(2) @Param 注解傳參法
public User selectUser(@param("userName")String name, @param("userArea") String userArea); <select id="**" resultMap="**"> select * from user_user_t where username = #{userName} and userarea= #{userArea}; </select>
說明:#{} 里面的名稱對應的是注解@Param括號中里面修飾的名稱,這種方式比較直觀,在參數(shù)比較少的情況下還是可以推薦使用的。
(3) Map 傳參法
public User slectUser(Map<String, Object> param); <select id="selectUser" parameterType="java.util.Map", resultMap="UserResultMap"> select * from user where user_name=#{userName} and dept_id=#{deptId}; </select>
說明:#{} 里面名稱對應的是Map里面的key 名稱 這種方式適合傳遞多個參數(shù),且參數(shù)易便能靈活傳遞的情況。
(4) Java Bean 傳參數(shù)
public User slectUser(Map<String, Object> param); <select id="selectUser" parameterType="com.test.User", resultMap="UserResultMap"> select * from user where user_name=#{userName} and dept_id=#{deptId}; </select>
說明: #{} 里面的名稱對應的是User類里面的成員屬性,這種方式比較直觀,但需要一個實體類,需要加屬性。
到此這篇關于關于Mybatis的@param注解及多個傳參的文章就介紹到這了,更多相關Mybatis的@param注解傳參內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot實現(xiàn)短信發(fā)送及手機驗證碼登錄
本文主要介紹了SpringBoot實現(xiàn)短信發(fā)送及手機驗證碼登錄,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-07-07帶你了解Java數(shù)據(jù)結構和算法之無權無向圖
這篇文章主要為大家介紹了Java數(shù)據(jù)結構和算法之無權無向圖?,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-01-01