首页 > 学院 > 开发设计 > 正文

Sql片断

2019-11-09 13:36:14
字体:
来源:转载
供稿:网友

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

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

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

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

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

    -->

    <sqlid="query_user_where">

       <!-- test中userCustom.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>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表