在 MyBatis 中,您可以使用動態(tài) SQL 標(biāo)簽來構(gòu)建靈活的 SQL 查詢,以根據(jù)不同的條件生成不同的查詢語句。以下是這些標(biāo)簽的作用和用法:
1. **`<if>` 標(biāo)簽:** 用于根據(jù)某個條件動態(tài)地包含或排除 SQL 片段,test:可以寫條件。
? ?示例用法:
? ?
? ?```xml
? ?<select id="getUserList" parameterType="Map" resultMap="UserResultMap">
? ? ? ?SELECT * FROM users
? ? ? ?<where>
? ? ? ? ? ?<if test="username != null">AND username = #{username}</if>
? ? ? ? ? ?<if test="email != null">AND email = #{email}</if>
? ? ? ?</where>
? ?</select>
? ?```
? ?
2. **`<where>` 標(biāo)簽:** 用于在 SQL 查詢中生成 `WHERE` 子句,并自動處理不必要的 `AND` 或 `OR`。
? ?示例用法:
? ?
? ?```xml
? ?<select id="getUserList" parameterType="Map" resultMap="UserResultMap">
? ? ? ?SELECT * FROM users
? ? ? ?<where>
? ? ? ? ? ?<if test="username != null">AND username = #{username}</if>
? ? ? ? ? ?<if test="email != null">AND email = #{email}</if>
? ? ? ?</where>
? ?</select>
? ?```
? ?
3. **`<set>` 標(biāo)簽:** 用于在更新語句中動態(tài)生成 `SET` 子句,根據(jù)傳入的參數(shù)來更新特定的列。
? ?示例用法:
? ?
? ?```xml
? ?<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>
? ?```
4. **`<trim>` 標(biāo)簽:** 用于在 SQL 語句中修剪多余的空白字符和逗號,并可以根據(jù)條件自定義修剪的內(nèi)容。
prefix:在sql動態(tài)條件加前綴
suffix:在sql動態(tài)條件加后綴
prefixOverrides:刪除sql動態(tài)條件前綴內(nèi)容
suffixOverrides:刪除sql動態(tài)條件后綴內(nèi)容
? ?示例用法:
? ?
? ?```xml
? ?<select id="getUserList" parameterType="Map" resultMap="UserResultMap">
? ? ? ?SELECT * FROM users
? ? ? ?<where>
? ? ? ? ? ?<trim prefix="AND" prefixOverrides="OR">
? ? ? ? ? ? ? ?<if test="username != null">OR username = #{username}</if>
? ? ? ? ? ? ? ?<if test="email != null">OR email = #{email}</if>
? ? ? ? ? ?</trim>
? ? ? ?</where>
? ?</select>
? ?```
5. **`<foreach>` 標(biāo)簽:** 用于循環(huán)遍歷集合或數(shù)組,生成多個 SQL 片段。
? ?示例用法:
? ?
? ?```xml
? ?<select id="getUserListByIds" parameterType="List" resultMap="UserResultMap">
? ? ? ?SELECT * FROM users
? ? ? ?WHERE id IN
? ? ? ?<foreach collection="list" item = "listParam" open = "(" separator = "," close = ")">
? ? ? ? ? ?#{listParam}
? ? ? ?</foreach>
? ?</select>
? ?```
6. **`<choose>` 標(biāo)簽:** 類似于 Java 中的 `switch` 語句,根據(jù)條件選擇其中一個分支進(jìn)行處理。
?choose(類似switch)
when標(biāo)簽:? ?類似switch的case分支?,? ?需要寫test屬性
otherwise(否則):? ?類似switch的default分支
? ?示例用法:
? ?```xml
? ?<select id="getUserList" parameterType="Map" resultMap="UserResultMap">
? ? ? ?SELECT * FROM users
? ? ? ?<where>
? ? ? ? ? ?<choose>
? ? ? ? ? ? ? ?<when test="username != null">AND username = #{username}</when>
? ? ? ? ? ? ? ?<when test="email != null">AND email = #{email}</when>
? ? ? ? ? ? ? ?<otherwise>AND status = 1</otherwise>
? ? ? ? ? ?</choose>
? ? ? ?</where>
? ?</select>
? ?```文章來源:http://www.zghlxwxcb.cn/news/detail-675097.html
這些動態(tài) SQL 標(biāo)簽允許您在 MyBatis 中根據(jù)不同的條件構(gòu)建動態(tài)的查詢語句,以實(shí)現(xiàn)更靈活的數(shù)據(jù)庫操作。您可以根據(jù)具體的業(yè)務(wù)需求選擇適當(dāng)?shù)臉?biāo)簽來創(chuàng)建定制的查詢邏輯。文章來源地址http://www.zghlxwxcb.cn/news/detail-675097.html
到了這里,關(guān)于在Mybatis中寫動態(tài)sql這些標(biāo)簽:if、where、set、trim、foreach、choose的作用是什么,怎么用?的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!