Mybatis動態(tài)SQL的示例代碼
什么是動態(tài)SQL:動態(tài)SQL就是根據(jù)不同的條件生成不同的SQL語句
基本流程
1,數(shù)據(jù)庫準(zhǔn)備一張表
2,導(dǎo)包
3,編寫核心配置文件
4,編寫實體類
5,編寫實體類對應(yīng)的Mapper和Mapper.xml文件
6,在核心配置文件中注冊Mapper.xml
7,測試
開啟自動駝峰命名規(guī)則映射
<!--開啟駝峰命名映射-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
即在數(shù)據(jù)庫中為create_time對應(yīng)Java實體類屬性createTime
IF,Where
<select id="queryListIf" parameterType="map" resultType="Blog">
select * from blog
<where>
<if test="title != null">
title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</where>
</select>
Where的作用:當(dāng)至少有一個滿足條件時添加Where,且會判斷后面加的第一條語句,若是and開頭,則會自動將這個and刪除
本質(zhì)上還是在拼接SQL,上述當(dāng)沒有滿足條件時查詢blog表中的所有,當(dāng)滿足條件時,則拼接SQL
Set
<update id="updateBlog" parameterType="map">
update blog
<set>
<if test="title != null">
title = #{title},
</if>
<if test="author != null">
author = #{author}
</if>
</set>
where id = #{id}
</update>
Set的作用:至少有一個滿足條件時添加Set,且會判斷后面加的最后的語句,若是",“結(jié)尾,則會自動將這個”,"刪除
Choose(when,otherwise)
<select id="queryNoLimit" parameterType="map" resultType="Blog">
select * from blog
<where>
<choose>
<when test="title != null">
title = #{title}
</when>
<when test="author != null">
and author = #{author}
</when>
<otherwise>
and `view` = #{view}
</otherwise>
</choose>
</where>
</select>
choose(when,otherwise)類似與Java中的switch(case,default),choose進(jìn)入選擇,when當(dāng)什么什么時,進(jìn)行條件判斷,若滿足條件,則執(zhí)行條件中的內(nèi)容,后面的when,otherwise將不再執(zhí)行,otherwise當(dāng)所有when都不滿足條件時執(zhí)行
ForEach
<select id="queryBlogById" parameterType="map" resultType="blog">
select * from blog
<where>
<foreach collection="ids" item="id" open="(" close=")" separator="or">
id = #{id}
</foreach>
</where>
</select>
上述為,一個集合ids存儲id的內(nèi)容,根據(jù)這個集合查詢所包含的id,open為開始,close為結(jié)束,separator為分隔符
才用map.put(“ids”,list)的方式導(dǎo)入集合
建議:現(xiàn)在Mysql中寫出完整的sql,再對應(yīng)的去修改即可
SQL片段
將一些功能的部分抽取出來方便復(fù)用
使用SQL標(biāo)簽抽取公共的部分
<sql id="titleAuthor">
<if test="title != null">
title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</sql>
在需要的地方使用include標(biāo)簽引用即可
<select id="queryListIf" parameterType="map" resultType="Blog">
select * from blog
<where>
<include refid="titleAuthor"></include>
</where>
</select>
注意事項:
1.最好基于單表來定義SQL片段
2.不要存在where標(biāo)簽
總結(jié)
所謂的動態(tài)SQL就是在拼接SQL語句,我們只要保證SQL的正確性,按照SQL的格式去排列組合就可以了
到此這篇關(guān)于Mybatis動態(tài)SQL的示例代碼的文章就介紹到這了,更多相關(guān)Mybatis動態(tài)SQL內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解SpringBoot 創(chuàng)建定時任務(wù)(配合數(shù)據(jù)庫動態(tài)執(zhí)行)
本篇文章主要介紹了SpringBoot 創(chuàng)建定時任務(wù)(配合數(shù)據(jù)庫動態(tài)執(zhí)行),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
MyBatis-Plus不使用數(shù)據(jù)庫默認(rèn)值的問題及解決
這篇文章主要介紹了MyBatis-Plus不使用數(shù)據(jù)庫默認(rèn)值的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
Java動態(tài)代理Proxy應(yīng)用和底層源碼詳細(xì)分析
Java動態(tài)代理是一種在運行時生成代理類的機制,用于代替手動編寫代理類的過程,這篇文章主要給大家介紹了關(guān)于Java動態(tài)代理Proxy應(yīng)用和底層源碼詳細(xì)分析的相關(guān)資料,需要的朋友可以參考下2024-03-03
servlet之ServletContext簡介_動力節(jié)點Java學(xué)院整理
這篇文章主要介紹了servlet之ServletContext簡介,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07

