mybatis動態(tài)SQL常用的標(biāo)簽使用及說明
1. <sql>標(biāo)簽
也叫<sql>片段,在使用sql片段時(shí)使用include標(biāo)簽通過sql片段的id進(jìn)行引用,sql片段的id在當(dāng)前空間是唯一的,sql片段中也可以寫其他的內(nèi)容,只要符合語法規(guī)范都是可以的。
示例:
<!-- 通用查詢結(jié)果列 -->
<sql id="Base_Column_List">
id, name, age, hobby, del_flag, create_time, update_time
</sql>
<select id="listAnimals" resultType="com.zhang.entity.Animal">
select
<include refid="Base_Column_List"></include>
from animal
</select>(注:<include>可以是單標(biāo)簽的,效果是一樣的,我這里使用了雙標(biāo)簽~)
2.<where>標(biāo)簽
根據(jù)where其后是否有sql,判斷拼接 where,滿足條件就拼接,否則不拼接。示例:
<select id="getAnimalByName" resultType="com.zhang.entity.Animal">
select * from animal
<where>
<if test="name!=null and name!=''">
and name = #{name}
</if>
</where>
</select>3.<choose> 標(biāo)簽
類似于Java中的switch分支。只進(jìn)入一個滿足when的條件,如果所有when都不滿足,則進(jìn)入otherwise。
示例:
<select id="getAnimalsByNameOrHobby" resultType="com.zhang.entity.Animal">
select * from animal
<choose>
<when test="hobby!=null and hobby!='' and name!=null and name!=''">
hobby = #{hobby} and name = #{name}
</when>
<when test="hobby!=null and hobby!=''">
hobby = #{hobby}
</when>
<otherwise>
name = #{name}
</otherwise>
</choose>
</select>4.<set>標(biāo)簽
與where有相似,其后如果存在條件,則拼接set。
<set>標(biāo)簽會動態(tài)地在行首插入SET關(guān)鍵字,并且自動幫我們?nèi)サ舳嘤嗟亩禾枺m用于update,示例:
<update id="update">
update animal
<set>
<if test="age!=null and age!=''">
age = #{age}
</if>
<if test="name!=null and name!=''">
name = #{name},
</if>
<if test="hobby!=null and hobby!=''">
hobby = #{hobby}
</if>
</set>
where id = #{id}
</update> <update id="updateById" parameterType="com.zhang.entity.Animal">
update animal
<set>
<if test="name != null">
`name` = #{name,jdbcType=VARCHAR},
</if>
<if test="age != null">
`age` = #{age,jdbcType=VARCHAR},
</if>
<if test="hobby != null">
hobby = #{hobby,jdbcType=VARCHAR},
</if>
<if test="create_time != null">
create_time = #{create_time,jdbcType=TIMESTAMP},
</if>
<if test="update_time != null">
update_time = #{update_time,jdbcType=TIMESTAMP},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>5.<foreach>標(biāo)簽
用于遍歷List、Map、Array , 屬性如下:
- collection:指定需要遍歷的元素
- item:遍歷之后的每一項(xiàng)
- separator:定義foreach里面語句的分隔符
- index:map中代表key,數(shù)組中代表數(shù)組下標(biāo)
<select id="listAnimals" resultType="com.zhang.entity.Animal">
SELECT * FROM animal
WHERE id in
<foreach collection="ids" item="id" index="index"
open="(" close=")" separator=",">
#{id}
</foreach>
</select>6.<bind>標(biāo)簽,用來定義變量
示例:
- mapper層:
List<Animal> getByName(@Param("animalName") String name);- xml映射層:
<select id="getByName" resultType="com.zhang.entity.Animal">
<!--animalName為傳過來的參數(shù)-->
<!--根據(jù)動物名字進(jìn)行模糊查詢-->
<bind name="animalNameLike" value="'%'+ animalName +'%'"/>
select * from animal
<where>
<if test="animalName != null and animalName != ''">
and `name` like #{animalNameLike}
</if>
</where>
</select>總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
- 一文詳解MyBatis中動態(tài)SQL的封裝原理與常用標(biāo)簽實(shí)戰(zhàn)應(yīng)用
- MyBatis CRUD 常用動態(tài) SQL 標(biāo)簽整理
- MyBatis 動態(tài) SQL 優(yōu)化之標(biāo)簽的實(shí)戰(zhàn)與技巧(常見用法)
- MyBatis中實(shí)現(xiàn)動態(tài)SQL標(biāo)簽
- Mybatis動態(tài)Sql標(biāo)簽使用小結(jié)
- MyBatis 動態(tài)SQL之where標(biāo)簽的使用
- MyBatis動態(tài)SQL之<choose><when><o(jì)therwise>標(biāo)簽的使用
相關(guān)文章
在SpringBoot項(xiàng)目中的使用Swagger的方法示例
這篇文章主要介紹了在SpringBoot項(xiàng)目中的使用Swagger的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
深入分析Spring Cloud 負(fù)載均衡器架構(gòu)選型
文章詳細(xì)介紹了OpenFeign的初始化和執(zhí)行流程,包括FeignClient注解、FeignClientsRegistrar類、FeignClientFactoryBean工廠類、Targeter接口及其實(shí)現(xiàn)等,感興趣的朋友一起看看吧2024-12-12
基于Redis分布式鎖Redisson及SpringBoot集成Redisson
這篇文章主要介紹了基于Redis分布式鎖Redisson及SpringBoot集成Redisson,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小小伙伴可以參考一下2022-09-09
Springboot居然可以設(shè)置動態(tài)的Banner(推薦)
這篇文章主要介紹了Springboot居然可以設(shè)置動態(tài)的Banner,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03

