MyBatis-动态SQL

一、if判断

场景:查询员工,要求携带了哪个字段查询条件就带上这个字段的值。即从参数中取值进行判断

接口方法:
public List<Employee> getEmpsByConditionIf(Employee employee);
<select id="getEmpsByConditionIf" resultType="com.atguigu.mybatis.bean.Employee"> select * from tbl_employee <where> <if test="id!=null"> id=#{id} </if> <if test="lastName!=null &amp;&amp; lastName!=&quot;&quot;"> //遇见特殊符号应该去写转义字符 and last_name like #{lastName} </if> <if test="email!=null and email.trim()!=&quot;&quot;"> and email=#{email} </if> <if test="gender==0 or gender==1"> and gender=#{gender} </if> </where> </select>

二、trim截取

trim可用于截取sql后面多出and或者or的场景,where标签无法解决。

自定义字符串的截取规则:
  prefix="":前缀:trim标签体中是整个字符串拼串后的结果。
     prefix给拼串后的整个字符串加一个前缀 
  prefixOverrides="":
     前缀覆盖:去掉整个字符串前面多余的字符
  suffix="":后缀
     suffix给拼串后的整个字符串加一个后缀 
  suffixOverrides="":
     后缀覆盖:去掉整个字符串后面多余的字符

场景1:查询
接口方法:
public List<Employee> getEmpsByConditionTrim(Employee employee);
<select id="getEmpsByConditionTrim" resultType="com.atguigu.mybatis.bean.Employee">
    select * from tbl_employee
         <trim prefix="where" suffixOverrides="and">
             <if test="id!=null">
                 id=#{id} and
             </if>
             <if test="lastName!=null &amp;&amp; lastName!=&quot;&quot;">
                 last_name like #{lastName} and
             </if>
             <if test="email!=null and email.trim()!=&quot;&quot;">
                 email=#{email} and
             </if> 
             <if test="gender==0 or gender==1">
                  gender=#{gender}
             </if>
         </trim>
     </select>

场景2:Trim更新拼串

接口方法:
public void updateEmp(Employee employee);
<update id="updateEmp">       
  update tbl_employee 
    <trim prefix="set" suffixOverrides=",">
         <if test="lastName!=null">
             last_name=#{lastName},
         </if>
         <if test="email!=null">
             email=#{email},
         </if>
         <if test="gender!=null">
             gender=#{gender}
         </if>
    </trim>
    where id=#{id}
</update>

三、set标签

接口方法:
public void updateEmp(Employee employee); <update id="updateEmp"> update tbl_employee <set> <if test="lastName!=null"> last_name=#{lastName}, </if> <if test="email!=null"> email=#{email}, </if> <if test="gender!=null"> gender=#{gender} </if> </set> where id=#{id}
</update>

四、choose标签

场景:如果带了id就用id查,如果带了lastName就用lastName查;只会进入其中一个

接口方法:
public List<Employee> getEmpsByConditionChoose(Employee employee); <select id="getEmpsByConditionChoose" resultType="com.atguigu.mybatis.bean.Employee"> select * from tbl_employee <where> <choose> <when test="id!=null"> id=#{id} </when> <when test="lastName!=null"> last_name like #{lastName} </when> <when test="email!=null"> email = #{email} </when> <otherwise> gender = 0 </otherwise> </choose> </where> </select>

五:foreach标签

用于遍历集合。
collection:指定要遍历的集合:
       list类型的参数会特殊处理封装在map中,map的key就叫list
       item:将当前遍历出的元素赋值给指定的变量
       separator:每个元素之间的分隔符
       open:遍历出所有结果拼接一个开始的字符
       close:遍历出所有结果拼接一个结束的字符
       index:索引。遍历list的时候是index就是索引,item就是当前值
                  遍历map的时候index表示的就是map的key,item就是map的值
       #{变量名}就能取出变量的值也就是当前遍历出的元素

场景1:查询
接口方法:
public List<Employee> getEmpsByConditionForeach(List<Integer> ids); <select id="getEmpsByConditionForeach" resultType="com.atguigu.mybatis.bean.Employee"> select * from tbl_employee
   where id in
<foreach collection="ids" item="item_id" separator="," open="(" close=")" > #{item_id} </foreach> </select>
场景2:批量保存。MySQL下批量保存:可以foreach遍历 mysql支持values(),(),()语法 接口方法: public void addEmps(@Param("emps")List<Employee> emps); <insert id="addEmps"> insert into tbl_employee( last_name,email,gender,d_id values <foreach collection="emps" item="emp" separator=","> (#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id}) </foreach> </insert>
场景3:批量操作。这种方式需要数据库连接属性allowMultiQueries=true(添加到url后面);这种分号分隔多个sql可以用于其他的批量操作(删除,修改) <insert id="addEmps"> <foreach collection="emps" item="emp" separator=";"> insert into tbl_employee(last_name,email,gender,d_id)    values(#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id}) </foreach> </insert>

 

posted @ 2021-04-18 22:33  放牛的猩猩灬  阅读(50)  评论(0编辑  收藏  举报