<MyBatis>入門六 動態sql
阿新 • • 發佈:2018-12-14
package org.maple.mapper; import org.apache.ibatis.annotations.Param; import org.maple.pojo.Employee; import java.util.List; import java.util.Map; /** * @author mapleins * @Date 2018-12-13 17:39 * @Desc 動態sql的介面 **/ public interface EmployeeMapperDynamicSQL { /** * 測試if和where */ List<Employee> getEmpByConditionIf(Employee employee); /** * 測試trim */ List<Employee> getEmpByConditionTrim(Employee employee); /** * 測試choose */ List<Employee> getEmpByConditionChoose(Employee employee); /** * 測試set */ void updateEmp(Employee employee);/** * 測試foreach迴圈list */ List<Employee> getEmpByConditionForeachList(@Param("ids") List ids); /** * 測試foreach迴圈map */ List<Employee> getEmpByConditionForeachMap(@Param("map") Map map); /** * 測試foreach插入資料 */ Integer addEmps(@Param("emps") List<Employee> emps); }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="org.maple.mapper.EmployeeMapperDynamicSQL"> <!-- =================================sql片段的使用====================================== --> <sql id="selectEmp"> SELECT id,last_name name,gender,email from tbl_employee </sql> <!-- =================================if標籤的使用====================================== --> <!--if test="" 進行判斷,如果為true,則拼裝,否則不拼裝--> <!--where 後可以跟 1=1 --> <!-- =================================where標籤的使用====================================== --> <!--3.ognl 1.where 1=1 2.使用where標籤,只會去掉第一個多出來的and或者or --> <!-- 查詢員工,要求:攜帶了哪個欄位查詢條件就帶上這個欄位的值 --> <select id="getEmpByConditionIf" resultType="org.maple.pojo.Employee"> <include refid="selectEmp"/> <where> <if test="id!=null"> id =#{id} </if> <if test="name!=null and name!="""> and last_name like concat('%',#{name},'%') </if> <if test="gender==0 or gender == 1"> and gender = #{gender} </if> <if test="email!=null and email!="""> and email = #{email} </if> </where> </select> <!-- =================================trim標籤的使用====================================== --> <!--4.trim標籤 prefix="" 給trim標籤拼串後的結果 最前面加一個字元 prefixOverrides="" 去掉整個字串前面多餘的字元 suffix="" 給trim標籤拼串後的結果 最後面加一個字元 prefixOverrides="" 去掉整個字串最後面多餘的字元 --> <select id="getEmpByConditionTrim" resultType="org.maple.pojo.Employee"> SELECT id,last_name name,gender,email from tbl_employee <trim prefix="where" prefixOverrides="" suffixOverrides="and"> <if test="id!=null"> id =#{id} and </if> <if test="name!=null and name!="""> last_name like concat('%',#{name},'%') and </if> <if test="gender==0 or gender == 1"> gender = #{gender} and </if> <if test="email!=null and email!="""> email = #{email} </if> </trim> </select> <!-- ==============================choose when,otherwise標籤的使用====================================== --> <!--如果帶了id,就用id查,如果帶了name,就用name查,只會進入其中一個--> <select id="getEmpByConditionChoose" resultType="org.maple.pojo.Employee"> SELECT id,last_name name,gender,email from tbl_employee <where> <choose> <when test="id!=null"> id = #{id} </when> <when test="name!=null and name!="""> last_name like concat('%',#{name},'%') </when> <when test="email!=null and email!="""> email = #{email} </when> <otherwise> gender = 0; </otherwise> </choose> </where> </select> <!-- =============================set標籤的使用====================================== --> <!--會把多餘的逗號去掉,也可以使用trim來做--> <update id="updateEmp"> update tbl_employee <set> <if test="name!=null and name!="""> last_name = #{name}, </if> <if test="gender==0 or gender == 1"> gender = #{gender}, </if> <if test="email!=null and email!="""> email = #{email}, </if> </set> <where> id = #{id} </where> </update> <!-- =============================foreach標籤的使用====================================== --> <!--list的遍歷 item是當前值,index是list的索引--> <select id="getEmpByConditionForeachList" resultType="org.maple.pojo.Employee"> SELECT id,last_name name,gender,email FROM tbl_employee WHERE id <foreach collection="ids" item="id" open="in (" close=")" separator="," index="i"> #{id} </foreach> </select> <!--map的遍歷 index是map的key,item是map的值--> <select id="getEmpByConditionForeachMap" resultType="org.maple.pojo.Employee"> SELECT id,last_name name,gender,email FROM tbl_employee WHERE id <foreach collection="map" item="id" open="in (" close=")" separator=","> #{id} </foreach> </select> <!--foreach 的批量插入--> <insert id="addEmps"> INSERT INTO tbl_employee(last_name, gender, email) VALUES <foreach collection="emps" item="emp" separator=","> (#{emp.name},#{emp.gender},#{emp.email}) </foreach> </insert> </mapper>