Mybatis使用XML實現(xiàn)動態(tài)sql的示例代碼
前言
當編寫 MyBatis 中復雜動態(tài) SQL 語句時,使用 XML 格式是一種非常靈活的方式。這樣做可以根據(jù)不同條件動態(tài)生成 SQL 查詢,更新或刪除語句。以下是一篇簡要的教程,詳細介紹如何使用 MyBatis XML 來編寫動態(tài) SQL。
1. 動態(tài)條件查詢
假設(shè)有一個 User 實體,有 id、username 和 email 字段,我們希望根據(jù)不同條件查詢用戶信息。
<!-- 在 Mapper XML 文件中編寫動態(tài) SQL 查詢 --> <select id="selectUsers" resultType="User"> SELECT * FROM users <where> <if test="id != null"> AND id = #{id} </if> <if test="username != null and username != ''"> AND username = #{username} </if> <if test="email != null and email != ''"> AND email = #{email} </if> </where> </select>
<where> 標簽用于將動態(tài)生成的條件組合到 WHERE 子句中。
<if> 標簽根據(jù)條件的存在與否來動態(tài)生成查詢條件。
上面的方法可以根據(jù)id、username、email進行條件查詢,當test后面的語句為true的時候,會將if標簽內(nèi)的語句拼接。
2. 動態(tài)更新語句
假設(shè)我們想根據(jù)不同的條件更新用戶信息。
<!-- 在 Mapper XML 文件中編寫動態(tài) SQL 更新 --> <update id="updateUser" parameterType="User"> UPDATE users <set> <if test="username != null"> username = #{username}, </if> <if test="email != null"> email = #{email}, </if> </set> WHERE id = #{id} </update>
<set> 標簽用于指定要更新的字段。
<if> 標簽根據(jù)條件動態(tài)設(shè)置要更新的字段。
3. 動態(tài)插入語句
如果要根據(jù)不同情況插入不同的字段,也可以使用動態(tài) SQL。
<!-- 在 Mapper XML 文件中編寫動態(tài) SQL 插入 --> <insert id="insertUser" parameterType="User"> INSERT INTO users <trim prefix="(" suffix=")" suffixOverrides=","> <if test="id != null"> id, </if> <if test="username != null and username != ''"> username, </if> <if test="email != null and email != ''"> email, </if> </trim> <trim prefix="VALUES (" suffix=")" suffixOverrides=","> <if test="id != null"> #{id}, </if> <if test="username != null and username != ''"> #{username}, </if> <if test="email != null and email != ''"> #{email}, </if> </trim> </insert>
<trim> 標簽用于動態(tài)設(shè)置插入的字段和對應(yīng)的值,當trim標簽內(nèi)的內(nèi)容為空時,不會添加前綴。
prefix 和 suffix 屬性用于指定插入語句的前綴和后綴。
suffixOverrides 屬性用于去除最后一個不必要的逗號。
4、其他標簽的使用
基礎(chǔ)的語法使用如下所示。choose、when、otherwise 有點像if-else if -else的感覺
<!-- 使用 choose、when、otherwise 標簽實現(xiàn)條件選擇 --> <select id="getUserByIdOrUsername" resultType="User"> SELECT * FROM users <where> <choose> <when test="id != null"> AND id = #{id} </when> <when test="username != null and username != ''"> AND username = #{username} </when> <otherwise> AND 1=1 </otherwise> </choose> </where> </select> ??????? <!-- 使用 foreach 標簽進行遍歷操作 --> <select id="getUsersByIdList" resultType="User"> SELECT * FROM users WHERE id IN <foreach collection="ids" item="id" open="(" separator="," close=")"> #{id} </foreach> </select>
到此這篇關(guān)于Mybatis使用XML實現(xiàn)動態(tài)sql的示例代碼的文章就介紹到這了,更多相關(guān)Mybatis動態(tài)sql內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中Integer的parseInt和valueOf的區(qū)別詳解
這篇文章主要介紹了Java中Integer的parseInt和valueOf的區(qū)別詳解,nteger.parseInt(s)是把字符串解析成int基本類型,Integer.valueOf(s)是把字符串解析成Integer對象類型,其實int就是Integer解包裝,Integer就是int的包裝,需要的朋友可以參考下2023-11-11SpringBoot?@RestControllerAdvice注解對返回值統(tǒng)一封裝的處理方法
這篇文章主要介紹了SpringBoot?@RestControllerAdvice注解對返回值統(tǒng)一封裝,使用@RestControllerAdvice對響應(yīng)進行增強,本文結(jié)合實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09java跳出循環(huán)的三種方式總結(jié)(break語句、continue語句和return語句)
在實際編程中,有時需要在條件語句匹配的時候跳出循環(huán),下面這篇文章主要給大家介紹了關(guān)于java跳出循環(huán)的三種方式,其中包括break語句、continue語句和return語句的相關(guān)資料,需要的朋友可以參考下2023-03-03spring?security需求分析與基礎(chǔ)環(huán)境準備教程
這篇文章主要為大家介紹了spring?security需求分析與基礎(chǔ)環(huán)境準備教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-03-03使用SpringBoot+nmap4j獲取端口信息的代碼詳解
這篇文章主要介紹了使用 SpringBoot + nmap4j 獲取端口信息,包括需求背景、nmap4j 的相關(guān)介紹、代碼說明(含測試代碼、改造后的代碼及參數(shù)說明),還提到了文件讀取方式和依賴引入方式,最終請求能獲取到數(shù)據(jù),需要的朋友可以參考下2025-01-01spring boot開發(fā)遇到坑之spring-boot-starter-web配置文件使用教程
Spring Boot支持容器的自動配置,默認是Tomcat,當然我們也是可以進行修改的。這篇文章給大家介紹了spring boot開發(fā)遇到坑之spring-boot-starter-web配置文件使用教程,需要的朋友參考下吧2018-01-01