您好,登錄后才能下訂單哦!
這篇文章主要介紹MyBatis3傳遞多個參數的案例,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
傳遞多個參數一般用在查詢上,比如多個條件組成的查詢,有以下方式去實現:
版本信息:
MyBatis:3.4.4
1、自帶方法
<select id="getUserArticlesByLimit" resultMap="resultUserArticleList"> select user.id,user.userName,user.userAddress,article.id as aid,article.title,article.content from user,article where user.id=article.userid and user.id=#{arg0} limit #{arg1},#{arg2} </select>
public List<Article> getUserArticlesByLimit(int id,int start,int limit); List<Article> articles=userMapper.getUserArticlesByLimit(1,0,2);
說明,arg0...也可以寫成param0...
2、直接傳遞對象
<select id="dynamicIfTest" parameterType="Article" resultType="Article"> select * from article where 1 = 1 <if test="title != null"> and title = #{title} </if> <if test="content != null"> and content = #{content} </if> limit 1 </select>
public Article dynamicIfTest(Article article); Article inArticle = new Article(); inArticle.setTitle("test_title"); Article outArticle = userOperation.dynamicIfTest(inArticle);
3、使用@Praam標注
<select id="dynamicChooseTest" resultType="Article"> select * from article where 1 = 1 <choose> <when test="title != null"> and title = #{title} </when> <when test="content != null"> and content = #{content} </when> <otherwise> and tile = "test_title" </otherwise> </choose> </select>
public Article dynamicChooseTest( @Param("title") String title, @Param("content") String content); Article outArticle2 = userOperation.dynamicChooseTest("test_title",null);
說明:這種方法同樣可以用在一個參數的時候。
4、使用HashMap
<select id="getArticleBeanList" resultType="ArticleBean"> select * from article where id = #{id} and name = #[code] </select>
說明:parameterType="hashmap"可以不用寫。
public List<ArticleBean> getArticleBeanList(HashMap map);
HashMap<String, Object> map = new HashMap<String, Object>(); map.put("id", 1); map.put("code", "123"); List<Article> articless3 = userOperation.getArticleBeanList(map);
特殊的HashMap示例,用在foreach節點:
<select id="dynamicForeach4Test" resultType="Article"> select * from article where title like "%"#{title}"%" and id in <foreach collection="ids" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>
public List<Article> dynamicForeach4Test(Map<String, Object> params);
HashMap<String, Object> map = new HashMap<String, Object>(); map.put("title", "title"); map.put("ids", new int[]{1,3,6}); List<Article> articless3 = userOperation.dynamicForeach4Test(map);
5、List結合foreach節點一起使用
<select id="dynamicForeachTest" resultType="Article"> select * from article where id in <foreach collection="list" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>
public List<Article> dynamicForeachTest(List<Integer> ids);
List<Integer> ids = new ArrayList<Integer>(); ids.add(1); ids.add(3); ids.add(6); List<Article> articless = userOperation.dynamicForeachTest(ids);
6、數組結合foreach節點一起使用
<select id="dynamicForeach3Test" resultType="Article"> select * from article where id in <foreach collection="array" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>
public List<Article> dynamicForeach3Test(int[] ids);
int[] ids2 = {1,3,6}; List<Article> articless2 = userOperation.dynamicForeach3Test(ids2);
以上是MyBatis3傳遞多個參數的案例的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。