MyBatis總結(二十九)--where、trim、choose、set、foreach使用
阿新 • • 發佈:2018-12-10
本文內容來自山矽谷
詳細情況可參考
下面給出xml配置。請注意該xml檔案中的註釋資訊。這就是各種標籤的使用例項
<?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="com.atguigu.mybatis.dao.EmployeeMapperDynamicSQL"> <!-- • if:判斷 • choose (when, otherwise):分支選擇;帶了break的swtich-case 如果帶了id就用id查,如果帶了lastName就用lastName查;只會進入其中一個 • trim 字串擷取(where(封裝查詢條件), set(封裝修改條件)) • foreach 遍歷集合 --> <!-- 查詢員工,要求,攜帶了哪個欄位查詢條件就帶上這個欄位的值 --> <!-- public List<Employee> getEmpsByConditionIf(Employee employee); --> <select id="getEmpsByConditionIf" resultType="com.atguigu.mybatis.bean.Employee"> select * from tbl_employee <!-- where --> <where> <!-- test:判斷表示式(OGNL) OGNL參照PPT或者官方文件。 c:if test 從引數中取值進行判斷 遇見特殊符號應該去寫轉義字元: &&: --> <if test="id!=null"> id=#{id} </if> <if test="lastName!=null && lastName!="""> and last_name like #{lastName} </if> <if test="email!=null and email.trim()!="""> and email=#{email} </if> <!-- ognl會進行字串與數字的轉換判斷 "0"==0 --> <if test="gender==0 or gender==1"> and gender=#{gender} </if> </where> </select> <!--public List<Employee> getEmpsByConditionTrim(Employee employee); --> <select id="getEmpsByConditionTrim" resultType="com.atguigu.mybatis.bean.Employee"> select * from tbl_employee <!-- 後面多出的and或者or where標籤不能解決 prefix="":字首:trim標籤體中是整個字串拼串 後的結果。 prefix給拼串後的整個字串加一個字首 prefixOverrides="": 字首覆蓋: 去掉整個字串前面多餘的字元 suffix="":字尾 suffix給拼串後的整個字串加一個字尾 suffixOverrides="" 字尾覆蓋:去掉整個字串後面多餘的字元 --> <!-- 自定義字串的擷取規則 --> <trim prefix="where" suffixOverrides="and"> <if test="id!=null"> id=#{id} and </if> <if test="lastName!=null && lastName!="""> last_name like #{lastName} and </if> <if test="email!=null and email.trim()!="""> email=#{email} and </if> <!-- ognl會進行字串與數字的轉換判斷 "0"==0 --> <if test="gender==0 or gender==1"> gender=#{gender} </if> </trim> </select> <!-- public List<Employee> getEmpsByConditionChoose(Employee employee); --> <select id="getEmpsByConditionChoose" resultType="com.atguigu.mybatis.bean.Employee"> select * from tbl_employee <where> <!-- 如果帶了id就用id查,如果帶了lastName就用lastName查;只會進入其中一個 --> <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> <!--public void updateEmp(Employee employee); --> <update id="updateEmp"> <!-- Set標籤的使用 --> 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} <!-- Trim:更新拼串 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> <!--public List<Employee> getEmpsByConditionForeach(List<Integer> ids); --> <select id="getEmpsByConditionForeach" resultType="com.atguigu.mybatis.bean.Employee"> select * from tbl_employee <!-- collection:指定要遍歷的集合: list型別的引數會特殊處理封裝在map中,map的key就叫list item:將當前遍歷出的元素賦值給指定的變數 separator:每個元素之間的分隔符 open:遍歷出所有結果拼接一個開始的字元 close:遍歷出所有結果拼接一個結束的字元 index:索引。遍歷list的時候是index就是索引,item就是當前值 遍歷map的時候index表示的就是map的key,item就是map的值 #{變數名}就能取出變數的值也就是當前遍歷出的元素 --> <foreach collection="ids" item="item_id" separator="," open="where id in(" close=")"> #{item_id} </foreach> </select> </mapper>