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[ ]]>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值