动态sql

发布时间 2023-10-11 16:55:12作者: abandon11

if

 <select id="getEmpById" parameterType="emp" resultType="emp">
    select *  from emp 
   <where> 
    <if test="ename!=null and ename!=''">
      ename=#{ename}
    </if>
    <if test="job!=null and job!=''">
      and job=#{job}
    </if>
    </where>
   
  </select>

 

where

 

set

 <update id="upadatEmp" parameterType="emp">
   update emp
   <set> 
    <if test="ename!=null and ename!=''">
     ename=#{ename},
    </if>
    <if test="job!=null and job!=''">
    job=#{job}
    </if>
    <where>
       <if test="empno!=null">
          empno=#{empno}
       </if>
    </where>
   </set>
  </update>

trim

prefix:要增加的前缀

prefixOverrides:要去除的前缀

suffix:要增加的后缀

suffixOverrides:要去除的后缀

 <select id="getEmptrim" parameterType="emp" resultType="emp">
    select *  from emp 
   <trim prefix="where" prefixOverrides="and | or">
      <if test="ename!=null and ename!=''">
        and ename=#{ename}
      </if>
      <if test="job!=null and job!=''">
      and job=#{job}
      </if>
   </trim>
  </select>