MyBatis3传递多个参数(Multiple Parameters)

所属分类: 软件编程 / java 阅读数: 79
收藏 0 赞 0 分享

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

版本信息:

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="dynamicForeach3Test" 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 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="dynamicForeach2Test" 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

更多精彩内容其他人还在看

JavaWeb项目部署到服务器详细步骤详解

这篇文章主要介绍了JavaWeb项目如何部署到服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA基于支付宝小程序搭建springboot项目的详细步骤

这篇文章主要介绍了IDEA基于支付宝小程序搭建springboot项目的详细步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解SpringBoot应用服务启动与安全终止

这篇文章主要介绍了SpringBoot应用服务启动与安全终止,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Boot启动及退出加载项的方法

这篇文章主要介绍了Spring Boot启动及退出加载项的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Data Jpa 自动生成表结构的方法示例

这篇文章主要介绍了Spring Data Jpa 自动生成表结构的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA中osgi的开发应用指南详解

这篇文章主要介绍了IDEA中osgi的开发应用指南详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解用maven将dubbo工程打成jar包运行

这篇文章主要介绍了详解用maven将dubbo工程打成jar包运行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解Java合并数组的两种实现方式

这篇文章主要介绍了Java合并数组的两种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

使用Jenkins Pipeline自动化构建发布Java项目的方法

这篇文章主要介绍了使用Jenkins Pipeline自动化构建发布Java项目的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

使用Maven配置Spring的方法步骤

这篇文章主要介绍了使用Maven配置Spring的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多