动态sql

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="set" suffixOverrides=",">
      <if test="ename!=null and ename!=''">
      ename=#{ename}
      </if>
      <if test="job!=null and job!=''">
      job=#{job},
      </if>
    </trim>
   <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>

 

添加

 <sql id="key">
     <trim suffixOverrides=",">
       <if test="ename!=null and ename!='">ename,</if>
       <if test="job!=null and job!=''">job,</if>
       <if test="mgr!=null">mgr,</if>
     </trim>
  </sql>
  <sql id="value">
     <trim suffixOverrides=",">
       <if test="ename!=null and ename!='">#{ename},</if>
       <if test="job!=null and job!=''">#{job},</if>
       <if test="mgr!=null">#{mgr},</if>
     </trim>
  </sql>
  <insert id="insetEmp" parameterType="emp">
     insert into emp(<include refid="key"/>) values(<include refid="value"/>)
  </insert>

posted on 2023-10-11 16:46  hellowworld!  阅读(18)  评论(0)    收藏  举报

导航