国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

MyBatis3傳遞多個參數(Multiple Parameters)

瀏覽:3日期:2023-10-23 15:54:37

傳遞多個參數一般用在查詢上,比如多個條件組成的查詢,有以下方式去實現:

版本信息:

MyBatis:3.4.4

1、自帶方法

<select 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 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 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 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 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> dynamicForeach3Test(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.dynamicForeach3Test(map);

5、List結合foreach節點一起使用

<select 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 resultType='Article'> select * from article where id in <foreach collection='array' index='index' item='item' open='(' separator=',' close=')'> #{item} </foreach> </select>

public List<Article> dynamicForeach2Test(int[] ids);

int[] ids2 = {1,3,6};List<Article> articless2 = userOperation.dynamicForeach2Test(ids2);

參考:

http://www.yihaomen.com/article/java/426.htm

到此這篇關于MyBatis3傳遞多個參數(Multiple Parameters)的文章就介紹到這了,更多相關MyBatis3傳遞多個參數內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Mybatis 數據庫
相關文章:
主站蜘蛛池模板: 玉山县| 金川县| 无为县| 临夏县| 洪湖市| 镶黄旗| 白水县| 尤溪县| 公主岭市| 西宁市| 永康市| 抚顺县| 渭源县| 福贡县| 嵊泗县| 泰顺县| 邻水| 五台县| 思茅市| 石河子市| 大竹县| 黄梅县| 全州县| 滦平县| 定兴县| 嘉兴市| 松阳县| 土默特右旗| 冕宁县| 合肥市| 应用必备| 五峰| 郎溪县| 蒙城县| 高淳县| 肇源县| 阿拉善左旗| 台东县| 黎川县| 子洲县| 西吉县|