Mybatis学习笔记(旨在巩固自我学习)

Mybatis动态SQL语句:就是在编写SQL映射文件时,通过判断语句来动态执行SQL查询 如

<select id="getEmplistBymoreTJ" resultType="Emp">
    select eid,ename,age,sex,did from emp
    <where>
    <if test="eid!=null">
    and eid=#{eid} 
    </if>
    <if test="ename!=null">
    and ename=#{ename} and
    </if>
    <if test="age!=age">
    and age=#{age} and
    </if>
    <if test="sex=1 or sex=0">
    and sex=#{sex}
    </if>
  </where>
</select>
<!--
<if test=""></if>:判断语句来选择执行标签里面的语句
<where>:用来添加where关键字 并去除多余的and格式(前面的and格式)
-->

and在标签后面的时候:

<select id="getEmplistBymoreTJ" resultType="Emp">
    select eid,ename,age,sex,did from emp
    <trim prifix="where" suffixOverride="and">
    <if test="eid!=null">
     eid=#{eid}  and
    </if>
    <if test="ename!=null">
     ename=#{ename} and
    </if>
    <if test="age!=age">
     age=#{age} and
    </if>
    <if test="sex=1 or sex=0">
     sex=#{sex}
    </if>
  </where>

 </trim>
<!--
<trim prfix="" suffix="" prefixOverrides="" suffixOverrides=""></trim>
prfix="":在操作的SQL语句前加入某些内容
suffix="":在操作的SQL语句后加入某些内容
prefixOverrides=""把操作的SQL语句前的某些内容删除
suffixOverrides=""把操作的SQL语句前的某些内容删除

-->

猜你喜欢

转载自www.cnblogs.com/1combat/p/12657094.html