Sql片断

来源:互联网 发布:环境破坏的事例和数据 编辑:程序博客网 时间:2024/05/16 14:59

将通用的sql语句抽取出来、单独定义,在其它的statement中可以引用sql片段。

通用的sql语句,常用:wher条件、查询列。

<!-- 用户查询条件定义为sql片断

       建议对单表的查询条件单独抽取sql片段,提高公用性

       注意:不要将where标签放在sql片断

    -->

    <sqlid="query_user_where">

       <!-- testuserCustom.username表示从userQueryVo读取属性值 -->

           <iftest="userCustom!=null">

              <iftest="userCustom.username!=nulland userCustom.username!=''">

                  and username like '%${userCustom.username}%'

              </if>

              <iftest="userCustom.sex!=nulland userCustom.sex!=''">

                  and sex = #{userCustom.sex}

              </if>     

           </if>

    </sql>

 

 

    <selectid="findUserCount"parameterType="cn.itcast.mybatis.po.UserQueryVo"resultType="int">

       select count(*) from user

       <!-- where标签相当于where关键字,可以自动去除第一个and -->

       <where>

       <!-- 引用sql片断,如果sql片断和引用不在同一个mapper必须前边加namespace -->

           <includerefid="query_user_where"></include>

       </where>

    </select>
0 0
原创粉丝点击