MyBatis3传递多个参数(Multiple Parameters)

这篇文章主要介绍了MyBatis3传递多个参数,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

传递多个参数一般用在查询上,比如多个条件组成的查询,有以下方式去实现:

版本信息:

MyBatis:3.4.4

1、自带方法

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}

public List getUserArticlesByLimit(int id,int start,int limit); List articles=userMapper.getUserArticlesByLimit(1,0,2);

说明,arg0...也可以写成param0...

2、直接传递对象

select * from article where 1 = 1 and title = #{title} and content = #{content} limit 1

public Article dynamicIfTest(Article article); Article inArticle = new Article(); inArticle.setTitle("test_title"); Article outArticle = userOperation.dynamicIfTest(inArticle);

3、使用@Praam标注

select * from article where 1 = 1 and title = #{title} and content = #{content} and tile = "test_title"

public Article dynamicChooseTest( @Param("title") String title, @Param("content") String content); Article outArticle2 = userOperation.dynamicChooseTest("test_title",null);

说明:这种方法同样可以用在一个参数的时候。

4、使用HashMap

select * from article where id = #{id} and name = #[code]

说明:parameterType="hashmap"可以不用写。

public List getArticleBeanList(HashMap map);

HashMap map = new HashMap(); map.put("id", 1); map.put("code", "123"); List articless3 = userOperation.getArticleBeanList(map);

特殊的HashMap示例,用在foreach节点:

select * from article where title like "%"#{title}"%" and id in #{item}

public List dynamicForeach3Test(Map params);

HashMap map = new HashMap(); map.put("title", "title"); map.put("ids", new int[]{1,3,6}); List articless3 = userOperation.dynamicForeach3Test(map);

5、List结合foreach节点一起使用

select * from article where id in #{item}

public List dynamicForeachTest(List ids);

List ids = new ArrayList(); ids.add(1); ids.add(3); ids.add(6); List articless = userOperation.dynamicForeachTest(ids);

6、数组结合foreach节点一起使用

select * from article where id in #{item}

public List dynamicForeach2Test(int[] ids);

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

参考:

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

相关文章

HashMap是Java中最常用的集合类框架,也是Java语言中非常典型...
在EffectiveJava中的第 36条中建议 用 EnumSet 替代位字段,...
介绍 注解是JDK1.5版本开始引入的一个特性,用于对代码进行说...
介绍 LinkedList同时实现了List接口和Deque接口,也就是说它...
介绍 TreeSet和TreeMap在Java里有着相同的实现,前者仅仅是对...
HashMap为什么线程不安全 put的不安全 由于多线程对HashMap进...