zoukankan      html  css  js  c++  java
  • ibatis (mybatis) for循环拼接语句【转】

    使用 , 拼接

    查询条件dto

    public class queryCondition
    {
     private String[] stuIds;
     private String name;
    }


    查询sqlMap

    <select id="selectStu" parameterClass="cn.xy.queryCondition" resultClass="cn.xy.Student">
     select id,name from student
     <dynamic prepend="where">
      <isNotNull property="stuIds" prepend="and">
       <iterate property="stuIds" open="id in (" close=")" conjunction=",">
        #stuIds[]#
       </iterate>
      </isNotNull>
      <isNotNull property="name" prepend="and">
       name like '%$name$%'
      </isNotNull>
     </dynamic>
    </select>


    在查询条件中有一个数组stuIds,在动态标签中进行遍历,看每一个student的id是否在该数组中。

    发出的语句 select id,name from student where  id in ( ? , ?) ...  

    使用 or 拼接

    查询条件dto

    public class queryCondition
    {
     private List<Student> lst;
     private String name;
    }


    查询sqlMap

    <select id="selectStu" parameterClass="cn.xy.queryCondition" resultClass="cn.xy.Student">
     select id,name from student
     <dynamic prepend="where">
      <isNotNull property="lst" prepend="and">
       <iterate property="lst" open=" (" close=")" conjunction="or">
        id = #lst[].id#
       </iterate>
      </isNotNull>
      <isNotNull property="name" prepend="and">
       name like '%$name$%'
      </isNotNull>
     </dynamic>
    </select>

    发出的语句 select id,name from student where  (id = ?   or   id = ?)...

     引用自:http://www.cnblogs.com/Struts-pring/p/5127510.html

  • 相关阅读:
    iOS开发UI篇—CAlayer简介
    iOS开发UI篇—ios手势识别(双击、捏、旋转、拖动、划动、长按, 上下左右滑动)
    录屏专家
    加载Gif图片方法
    制作酸奶方法
    UITabBar小红点(适配iPad)
    那些著名或非著名的iOS面试题-后编
    iOS学习资源
    实用的Mac软件
    安装iOS企业包流程
  • 原文地址:https://www.cnblogs.com/whatlonelytear/p/6755090.html
Copyright © 2011-2022 走看看