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

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

MyBatis3傳遞多個參數(Multiple Parameters)

瀏覽:10日期: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 數據庫
相關文章:
主站蜘蛛池模板: 南通市| 沅陵县| 阿拉善右旗| 竹溪县| 定兴县| 临猗县| 池州市| 英吉沙县| 普陀区| 马山县| 潼南县| 老河口市| 绥德县| 犍为县| 思茅市| 平泉县| 榆中县| 徐州市| 延庆县| 施秉县| 罗田县| 西青区| 兴山县| 苍梧县| 滨海县| 新源县| 晋中市| 册亨县| 桐城市| 文登市| 安化县| 芦溪县| 清丰县| 白城市| 淅川县| 射阳县| 广州市| 德惠市| 兴仁县| 榕江县| 关岭|