Let spirit guide you

          thinking in life.

          常用鏈接

          統計

          API

          Forum

          Site

          WiKi

          最新評論

          ibatis sqlmap dynamic multiply condition

          map.put("username", "ACID");
          // map.put("firstName", "XYX");
          map.put("firstName", "");
          // map.put("lastName", "bb");
          map.put("lastName", "");
          // map.put("status","OK");
          map.put("status","");
          LinkedList emaillist=new LinkedList();
          emaillist.add("yahoo");
          emaillist.add("acid");
          map.put("emaillist",emaillist);
          try {
          // return this.getSqlMapClient().queryForList("getAccountNameListByMultiplyConditionsAnd",map);
          // return this.getSqlMapClient().queryForList("getAccountNameListByMultiplyConditionsOR",map);
          return this.getSqlMapClient().queryForList("getAccountNameListByMultiplyConditionsAndOR",map);
          // return this.getSqlMapClient().queryForList("getAccountNameListByMultiplyConditionsNested",map);
          } catch (SQLException e) {
          e.printStackTrace();
          }
          return null;
          sqlmap:
              <!-- =========================================================== -->
              <!-- 條件語句嵌套-->
              <!-- =========================================================== -->
              <!--    如果都不是空的:那么構造出來的sql是:  select userid as value from account        where   firstname=?     and(   lastname=?    or status=? )    -->
              <!--    如果lastName和status都是空的:那么構造出來的sql是:  select userid as value from account        where           firstname=?            and   ( 1=1      )        -->
              <!--   如果status都是空的:那么構造出來的sql是:  select userid as value from account        where             firstname=?     and    ( lastname=?    )       -->
              <!--   如果lastName都是空的:那么構造出來的sql是: select userid as value from account        where        firstname=?        and   ( 1=1       or       status=?  )         -->
              
              <!--      如果firstName為空:那么構造出來的sql是: select userid as value from account        where         ( lastname=?    or     status=?  )           -->
              <!--      如果都是為空:那么構造出來的sql是:  select userid as value from account        where         1=1               and   ( 1=1     )             -->
              <select id="getAccountNameListByMultiplyConditionsAndOR" resultClass="string" parameterClass="java.util.Map">
              select userid as value from account  
               <dynamic prepend="where" >
                     <isNotEmpty prepend="and" property="firstName" >
                       <![CDATA[
                    firstname=#firstName#
                          ]]>
          </isNotEmpty>
          <isEmpty property="firstName">
                     <![CDATA[
                          1=1 
                          ]]>
                     </isEmpty>
                     <isNotEmpty prepend="and" property="lastName">
                     <![CDATA[
                          ( lastname=#lastName#
                          ]]>
                     </isNotEmpty>
                     <isEmpty property="lastName">
                     <![CDATA[
                          and   ( 1=1 
                          ]]>
                     </isEmpty>
                     <isNotEmpty prepend="or" property="status">
                     <![CDATA[
                          status=#status#  )
                          ]]>
                     </isNotEmpty>
                     <isEmpty property="status">
                     <![CDATA[
                           )
                          ]]>
                     </isEmpty>
                      
          <isNotEmpty   prepend="and"  property="emaillist">
                    <iterate prepend="and"   property="emaillist"    conjunction="or" >
          email=#emaillist[]#
                    </iterate>
                   
                    </isNotEmpty>
               </dynamic>
            </select>

          posted on 2008-12-08 20:01 cary 閱讀(871) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 周宁县| 潞城市| 武威市| 桑日县| 砀山县| 台山市| 京山县| 南昌县| 铁岭县| 红桥区| 南投市| 思茅市| 鹤壁市| 介休市| 县级市| 株洲县| 库尔勒市| 田东县| 武强县| 莱芜市| 镇原县| 天台县| 应城市| 镇雄县| 霍城县| 沈阳市| 台前县| 娄底市| 盐津县| 澄江县| 内丘县| 阜新| 汉沽区| 花莲县| 南开区| 芒康县| 岳池县| 淮北市| 辽宁省| 象山县| 开鲁县|