zoukankan      html  css  js  c++  java
  • mybatis ForEach Collection集合等规范解析(转)

    转自:http://blog.csdn.net/wj3319/article/details/9025349

    在SQL开发过程中,动态构建In集合条件查询是比较常见的用法,在Mybatis中提供了foreach功能,该功能比较强大,它允许你指定一个集合,声明集合项和索引变量,它们可以用在元素体内。它也允许你指定开放和关闭的字符串,在迭代之间放置分隔符。这个元素是很智能的,它不会偶然地附加多余的分隔符。下面是一个演示示例:

      <select id="findByIdsMap" resultMap="BaseResultMap">
        Select
        <include refid="Base_Column_List" />
         from jria where ID in
         <foreach item="item" index="index" collection="list" 
                        open="(" separator="," close=")">
                       #{item}
                </foreach>
     </select> 

    但由于官方文档对这块的使用,描述的比较简短,细节上也被忽略掉了(可能是开源项目文档一贯的问题吧),也使用不少同学在使用中遇到了问题。特别是foreach这个函数中,collection属性做什么用,有什么注意事项。由于文档不全,这块只能通过源代码剖析的方式来分析一下各个属性的相关要求。

    collection属性的用途是接收输入的数组或是List接口实现。但对于其名称的要求,Mybatis在实现中还是有点不好理解的,所以需要特别注意这一点。

    下面开始分析源代码(笔记使用的是Mybatis 3.0.5版本)

    先找到Mybatis执行SQL配置解析的入口

    MapperMethod.Java类中 public Object execute(Object[] args) 该方法是执行的入口.

    针对in集合查询,对应用就是 selectForList或SelctForMap方法。

    但不管调用哪个方法,都会对原来JDK传入的参数 Object[]类型,通过 getParam方法转换成一个Object,那这个方法是做什么的呢?分析源码如下:

    上图中标红的两处,很惊讶的发现,一个参数与多个参数的处理方式是不同的(后续很多同学遇到的问题,就有一大部分出自这个地方)。如果参数个数大于一个,则会被封装成Map, key值如果使用了Mybatis的 Param注解,则会使用该key值,否则默认统一使用数据序号,从1开始。这个问题先记下,继续分析代码,接下来如果是selectForList操作(其它操作就对应用相应方法),会调用DefaultSqlSession的public List selectList(String statement, Object parameter, RowBounds rowBounds) 方法

    又一个发现,见源代码如下:

    上图标红部分,对参数又做了一次封装,我们看一下代码

    现在有点清楚了,如果参数类型是List,则必须在collecion中指定为list, 如果是数据组,则必须在collection属性中指定为 array.

    现在就问题就比较清楚了,如果是一个参数的话,collection的值取决于你的参数类型。

    如果是多个值的话,除非使用注解Param指定,否则都是数字开头,所以在collection中指定什么值都是无用的。下图是debug显示结果。

    针对上面分析的结果,下面给出了一个使用的解决方案,希望对大家对帮助。

    在使用这个功能是需要特别注意以下规则:
    1. 当查询的参数只有一个时 
      findByIds(List<Long> ids)
     1.a 如果参数的类型是List, 则在使用时,collection属性要必须指定为 list
     <select id="findByIdsMap" resultMap="BaseResultMap">
             Select
             <include refid="Base_Column_List" />
             from jria where ID in
                      <foreach item="item" index="index" collection="list" 
                             open="(" separator="," close=")">
                            #{item}
                    </foreach>
      </select> 
     
     findByIds(Long[] ids)
     1.b 如果参数的类型是Array,则在使用时,collection属性要必须指定为 array
      <select id="findByIdsMap" resultMap="BaseResultMap">
                     select
                     <include refid="Base_Column_List" />
              from jria where ID in
                      <foreach item="item" index="index" collection="array" 
                             open="(" separator="," close=")">
                            #{item}
                    </foreach>
      </select> 
     
    2. 当查询的参数有多个时,例如 findByIds(String name, Long[] ids)
     这种情况需要特别注意,在传参数时,一定要改用Map方式, 这样在collection属性可以指定名称
             下面是一个示例
             Map<String, Object> params = new HashMap<String, Object>(2);
            params.put("name", name);
             params.put("ids", ids);
            mapper.findByIdsMap(params);
     
     <select id="findByIdsMap" resultMap="BaseResultMap">
                     select
                     <include refid="Base_Column_List" />
              from jria where ID in
                      <foreach item="item" index="index" collection="ids" 
                             open="(" separator="," close=")">
                            #{item}
                    </foreach>
       </select> 
     
     
    完整的示例如下:
    例如有一个查询功能,Mapper接口文件定义如下方法:
    List<Jria> findByIds(Long... ids);
    使用 in 查询的sql拼装方法如下:
     <select id="findbyIds" resultMap="BaseResultMap">
                     select
                     <include refid="Base_Column_List" />
              from jria where ID in
                      <foreach item="item" index="index" collection="array" 
                             open="(" separator="," close=")">
                            #{item}
                    </foreach>
      </select> 
  • 相关阅读:
    LeetCode 242. Valid Anagram (验证变位词)
    LeetCode 205. Isomorphic Strings (同构字符串)
    LeetCode 204. Count Primes (质数的个数)
    LeetCode 202. Happy Number (快乐数字)
    LeetCode 170. Two Sum III
    LeetCode 136. Single Number (落单的数)
    LeetCode 697. Degree of an Array (数组的度)
    LeetCode 695. Max Area of Island (岛的最大区域)
    Spark中的键值对操作
    各种排序算法总结
  • 原文地址:https://www.cnblogs.com/aigeileshei/p/6109355.html
Copyright © 2011-2022 走看看