ibatis动态sql例子

本文详细介绍了SQL查询的基本语法,包括计数查询、全字段查询及使用动态前置和条件判断实现复杂查询的方法。同时,阐述了如何利用`isNotEmpty`、`isNotEmptyprepend`等标签进行条件过滤,以及`isNotEmptyprepend`的强大功能。此外,还讨论了`isNotEmptyprepend`和`isNotEmpty`的区别,并提供了用于判断不为空和非空字符串的功能。最后,通过实例展示了如何结合这些标签和SQL语句进行高级查询操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<sql id="sql_count">                  select count(*)          </sql>          

<sql id="sql_select">                  select *          </sql>          

 

<sql id="sql_where">                 

    from icp                  

            <dynamic prepend="where">                          

                   <isNotEmpty prepend="and" property="name">                                 

                                    name like '%$name$%'                         

                   </isNotEmpty>                          

                  <isNotEmpty prepend="and" property="area_id">                                 

                                    area_id = #area_id#                         

                  </isNotEmpty>                          

              </dynamic>

 

              <dynamic prepend="">   

                       <!--这里相当于if的and条件判断了,也即start和size都不能为空-->                       

                      <isNotNull property="_start">                                 

                            <isNotNull property="_size">                                         

                                    limit #_start#, #_size#                                 

                            </isNotNull> 

                      </isNotNull> 

              </dynamic>         

</sql>          

 

<select id="findByParamsForCount" parameterClass="map" resultClass="int">                  

     <include refid="sql_count"/>                 

     <include refid="sql_where"/>         

</select>

 

<!--这个判断了not null 和空串 功能比not null 标签强大-->

<isNotEmpty prepend="and" property="_img_size_ge">                                 

<![CDATA[                                  

           img_size >= #_img_size_ge#                         

]]>

 

 

<isEqual prepend="and" property="_exeable" compareValue="Y">                                  

              <![CDATA[                                  

                        t.finished in ('10','19') and t.failure<3                         

                   ]]>                         

 </isEqual>    

 

<isNotEmpty prepend="" property="exprogramcode">                                 

              <isNotEmpty prepend="" property="isRational">                                          

                    <isEqual prepend="and" property="isRational" compareValue="N">                                                  

                      code not in                                                  

                                 (select t.contentcode   from cms_ccm_programcontent t  where                        t.contenttype='MZNRLX_MA'                                                 

and t.programcode = #exprogramcode#)                                         

                    </isEqual>                                  

               </isNotEmpty>                         

 </isNotEmpty>

 

 <select id="idtet" parameterClass="map" resultClass="java.util.HashMap">                                <include refid="sql_select"/>                 

<include refid="sql_where"/>         

</select>

 

<insert id="insert" parameterClass="RuleMaster">                 

         insert into rulemaster(                  name,                  createtime,                  updatetime,                  remark                  ) values (                  #name#,                  now(),                  now(),                  #remark#                  )                  

          <selectKey keyProperty="id" resultClass="long">                         

                  select LAST_INSERT_ID()                 

           </selectKey>         

</insert>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值