MyBatis動態(tài)SQL實現(xiàn)配置過程解析
動態(tài)SQL
什么是動態(tài)SQL:
動態(tài)SQL就是根據(jù)不同的條件生成不同的SQL語句
- if
- choose(when,otherwise)
- trim(where,set)
- foreach
1、搭建環(huán)境
建表
CREATE TABLE `bolg`( `id` VARCHAR(50) NOT NULL COMMENT '博客id', `title` VARCHAR(100) not null comment '博客標題', `author` VARCHAR(30) not null comment '博客作者', `creat_time` datetime not null comment '創(chuàng)建時間', `views` int(30) not null comment '瀏覽量' )ENGINE=InnoDB DEFAULT CHARSET=utf8
創(chuàng)建一個基礎工程
導包
編寫配置文件
編寫實體類
@Data
public class Blog {
private int id;
private String title;
private String author;
private Date creatTime;
private int views;
}
編寫實體類對應的Mapper接口和Mapper.xm
2、IF
<select id="queryBlogIF" parameterType="map" resultType="com.rui.pojo.Blog">
select * from mybatis.bolg where 1=1
<if test="title != null">
and title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</select>
@Test
public void queryBlogIF(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
HashMap map = new HashMap();
map.put("author","尹銳");
List<Blog> blogs = mapper.queryBlogIF(map);
for (Blog blog : blogs) {
System.out.println(blog);
}
sqlSession.close();
}
3、choose(when,otherwise)
<select id="queryBlogChoose" parameterType="map" resultType="com.rui.pojo.Blog">
select * from mybatis.bolg
<where>
<choose>
<when test="title != null">
title=#{title}
</when>
<when test="author!=null">
and author = #{author}
</when>
<otherwise>
and views = #{views}
</otherwise>
</choose>
</where>
</select>
4、trim(where,set)
select * from mybatis.bolg
<where>
<if test="title != null">
title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</where>
<update id="updateBlog" parameterType="map">
update mybatis.bolg
<set>
<if test="title != null">
title = #{title},
</if>
<if test="author != null">
author = #{author},
</if>
</set>
where id = #{id}
</update>
所謂的動態(tài)SQL,本質還是SQL語句,只是我們可以在SQL層面,去執(zhí)行一些邏輯代碼
5、Foreach
select * from user where 1=1 and
<foreach item="id" index="index" collection="ids"
open="(" separator="or" close=")">
#{id}
</foreach>
(id=1 or id=2 or id=3)

<!--
select * from mybatis.bolg where 1=1 and (id=1 or id=2 or id=3)
我們現(xiàn)在傳遞一個萬能的map,這個map中可以存在一個map
-->
<select id="queryBlogForeach" parameterType="map" resultType="com.rui.pojo.Blog">
select * from mybatis.bolg
<where>
<foreach collection="ids" item="id" open="(" close=")" separator="or">
id = #{id}
</foreach>
</where>
</select>
動態(tài)SQL就是在拼接SQL語句,我們只要保證SQL的正確性,按照SQL的格式,去排列組合就可以了
建議:
先在MySQL中寫出完整的SQL,再對應的去修改成為我們的動態(tài)SQL
SQL片段
有的時候,我們可能會將一些公共的部分抽取處理,方便復用
使用SQL標簽抽取公共的部分
<sql id="if-title-author">
<if test="title != null">
title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</sql>
在需要使用的地方使用Include標簽引用即可
<select id="queryBlogIF" parameterType="map" resultType="com.rui.pojo.Blog">
select * from mybatis.bolg
<where>
<include refid="if-title-author"></include>
</where>
</select>
注意事項:
最好基于單表來定義SQL片段
不要存在where或者set標簽,片段里盡量只有if就好了
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Netty分布式FastThreadLocal的set方法實現(xiàn)邏輯剖析
這篇文章主要為大家介紹了Netty分布式FastThreadLocal的set方法實現(xiàn)邏輯剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-03-03
Java 實戰(zhàn)練手項目之校園超市管理系統(tǒng)的實現(xiàn)流程
讀萬卷書不如行萬里路,只學書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+Mysql+Maven+Bootstrap實現(xiàn)一個校園超市管理系統(tǒng),大家可以在過程中查缺補漏,提升水平2021-11-11
mybatis中@Param注解總是報取不到參數(shù)問題及解決
這篇文章主要介紹了mybatis中@Param注解總是報取不到參數(shù)問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
java8 stream的多字段排序實現(xiàn)(踩坑)
這篇文章主要介紹了java8 stream的多字段排序實現(xiàn)(踩坑),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03
Spring MVC學習教程之RequestMappingHandlerAdapter詳解
這篇文章主要給大家介紹了關于Spring MVC學習教程之RequestMappingHandlerAdapter的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧2018-11-11

