MyBatis的使用(三):动态语句与转义字符

动态语句

  • if动态语句
        1.if 按条件是test="" zai在条件中 组装多个条件要使用英文and or
        2.test中直接使用key不需要使用el表达式 sql语句中的参数一定要使用el表达式

  • where语句 针对条件专用
        1.where用于动态指定查询条件,可以没有条件
        2.where一般配合if使用,条件前面一定要加and或者or 关键

  • set语句 针对更新语句专用
        1.set用于动态更新sql,在更新语句后一定要加,
        2.set配合if使用 一般基本类型都不配合if使用 一般都会使用包装类和null比较

  • sql语句 重复封装语句
        1.引用相同的代码放到sql标签中,给他指定ID
        2.在需要引用的地方配合 include标签设置refid为上面指定的sql的ID
        3.where做分页查询,查询的列

  • trim语句(了解)
    1.prefix 代表最前面需要加上的字符串 prefixOverrides代表最前面需要去掉的字符串
    2.suffix 代表最后面需要加上的字符串 suffixOverrides代表最后面需要去掉的字符串

  • foreach 遍历(实现 in语句)(了解)
    1.collections 代表需要遍历的参数 index 代表每次循环的索引 item代表循环的数据体 使用的时候要用el表达式
    2.open 以…开头 close以…结尾 separator分隔符

  • choose 和switch类似(了解)
    1.choose 配合when和otherwise一起使用
    2.当所有的when条件都不满足的时候会执行otherwise

示例

<sql id="whereCondition">
    <where>
        <if test="ename!=null and ename!='' ">
            and ename like concat('%',#{ename},'%')
        </if>
        <if test="job!= null and job!=''  ">
            and  job like concat('%',#{job},'%')
        </if>
    </where>
    <!--<trim prefix="where" prefixOverrides="and | or">
       <if test="ename!=null and ename!='' ">
           and ename like concat('%',#{ename},'%')
       </if>
       <if test="job!= null and job!=''  ">
           and  job like concat('%',#{job},'%')
       </if>
   </trim>-->
   <!--where 1=1
   <choose>
       <when test="ename!=null and ename!='' ">
           and ename like concat('%',#{ename},'%')
       </when>
       <when test="job!= null and job!=''  ">
           and  job like concat('%',#{job},'%')
       </when>
       <otherwise>
           <![CDATA[ and hiredate >= #{hiredate} ]]>
       </otherwise>
   </choose>-->
</sql>


<select id="findEmpByCondition" parameterType="Emp" resultMap="empFKMap">
    select * from emp <include refid="whereCondition"></include>
    limit #{from},#{rows}
</select>

<select id="findCountByCondition" parameterType="Emp" resultType="int">
    select count(0) from emp <include refid="whereCondition"></include>
</select>

<update id="updateEmp" parameterType="Emp">
  UPDATE emp
  <!--<trim prefix="set" suffixOverrides="," suffix="WHERE EMPNO=#{empno}">
      <if test="ename!=null">ENAME=#{ename},</if>
      <if test="job!=null">job=#{job},</if>
      <if test="hiredate!=null">HIREDATE=#{hiredate},</if>
      <if test="password!=null">password=#{password},</if>
  </trim>-->
  <set>
      <if test="ename!=null">ENAME=#{ename},</if>
      <if test="job!=null">job=#{job},</if>
      <if test="hiredate!=null">HIREDATE=#{hiredate},</if>
      <if test="password!=null">password=#{password},</if>
  </set>
  WHERE EMPNO=#{empno}
</update>

<select id="findEmpByJob" parameterType="list" resultMap="empIDMap">
    select  * from  emp where job  in
    <foreach collection="jobs" index="index" item="item" open="(" close=")" separator=",">
        #{item}
    </foreach>
</select>

转义字符

  1. 在mybatis中不能直接使用 > < …相关的字符 要使用转义字符

  2. CDATA区域可以批量忽视转义字符 <![CDATA[ ]]>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章