mybatis_06SQL片段

个人概要: SQL片段在使用if where的基础上,将if,where语句装到SQL标签内,在数据库操作元素内引用

Mybatis提供了SQL片段的功能,可以提高SQL的可重用性

<!--声明一个SQL片段-->
<sql id="sql_findUserByifwhere">
    <where>
    <if test="user!=null and user!=''">
        <if test="user.sex!=null and user.sex!=''">
            sex=#{user.sex}
        </if>
        <if test="user.username!=null and user.username!=''">
            and username LIKE "%${user.username}%"
        </if>
    </if>
    </where>
</sql>

<select id="findUserByifwhere" parameterType="userQueryVO" resultMap="userByresultmap">
  select * from USER
    /*使用一个SQL片段*/
  <include refid="sql_findUserByifwhere"></include>
</select>

猜你喜欢

转载自www.cnblogs.com/aihuadung/p/10467563.html