zoukankan      html  css  js  c++  java
  • MyBatis 常用标签用法

    标签

    <foreach collection="" item="" index=""  open="" separator="" close=""></foreach>
    
    collection为集合名
    item 表示集合中每一个元素 进行迭代时的别名,
    index 指定一个名字,用于表示在迭代过程中,每次迭代到的位置,
    open 表示该语句以什么开始,
    separator 表示在每次进行迭代之间以什么符号作为分隔 符,
    close 表示以什么结束。

    示例:
    dao层方法代码
    List<Customer>  selectByIds(@Param("ids") Integer[] ids);
    

     mapper使用foreach标签

        <select id="selectByIds" resultType="Customer">
            select *
              from WXBHPT_V_Customers
              <where>
                  <if test='ids!=null '>
                      customid  in
                      <foreach collection="ids" item="id" open="(" separator="," close=")" index="index">
                        #{id}
                      </foreach>
                  </if>
              </where>
           order by customid;
        </select>
    

      拼接结果为:

    select * from WXBHPT_V_Customers where customid in (?,?,?)
    

      ps:如果ids是某个bean对象的集合或数组,  #{id.xxx}   (xxx为属性名) 可直接取值

    bind标签

     <bind  name="" value="" />
    
     name为变量取一个名字,以便在后面使用它
    value的值

     用法举例

          dao代码

    Integer count(@Param("search") String search);
    

      mapper代码

    <select id="count" resultType="Integer"> 
      <bind name="pattern" value="'%' + _parameter.search + '%'" />
      select count(*) from WXBHPT_V_Customers where search like #{pattern} order by customid
    </select>

      拼接结果

    select count(*) from WXBHPT_V_Customers where search like %searchstr% order by customid 
    

      

  • 相关阅读:
    [Outlook] Outlook2013能收但无法发送邮件-0x800CCC13, 0x800CCC0B, 0x8004210B
    [Mobile] 手机浏览器输入框-数字输入框
    [Qcon] 百姓网开发总结
    [QCon] Scrum阅读随想
    [Spring] 事务级别定义
    [Monitor] 监控规则定义
    [Spring Batch] 图解Spring Batch原理
    [JavaCore] 微信手机浏览器版本判断
    Python 编码简单说
    矩阵或多维数组两种常用实现方法
  • 原文地址:https://www.cnblogs.com/zhumaoyu/p/9797283.html
Copyright © 2011-2022 走看看