Mybatis動(dòng)態(tài)SQL之IF語(yǔ)句詳解
Mysql 5.0 以后,支持了動(dòng)態(tài)sql語(yǔ)句,我們可以通過(guò)傳遞不同的參數(shù)得到我們想要的值.
1. Mybatis–動(dòng)態(tài)SQL之IF語(yǔ)句
沒(méi)有搭建環(huán)境的請(qǐng)點(diǎn)擊
1.1 BlogMapper.java
// 查詢博客
List<Blog> queryBlogIf(Map map);
1.2 BlogMapper.xml
<select id="queryBlogIf" parameterType="map" resultType="Blog">
select * from mybatis.blog where 1=1
<if test="title != null">
and title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</select>
1.3 Test.java
1.3.1 第一種情況,不加任何查詢條件,默認(rèn)會(huì)把所有數(shù)據(jù)查出來(lái)
// 第一種情況,不加任何查詢條件,默認(rèn)會(huì)把所有數(shù)據(jù)查出來(lái)
@org.junit.Test
public void test01() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
Map map = new HashMap();
List<Blog> blogs = mapper.queryBlogIf(map);
for (Blog blog : blogs) {
System.out.println(blog);
}
}
運(yùn)行結(jié)果:
查詢出了所有記錄

1.3.2 第二種情況,添加參數(shù)title
@org.junit.Test
public void test01() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
Map map = new HashMap();
map.put("title", "Spring");
List<Blog> blogs = mapper.queryBlogIf(map);
for (Blog blog : blogs) {
System.out.println(blog);
}
}
查詢出了一條記錄

1.3.3 第三種情況,添加2個(gè)參數(shù)
@org.junit.Test
public void test01() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
Map map = new HashMap();
map.put("title", "微服務(wù)");
map.put("author", "天天天");
List<Blog> blogs = mapper.queryBlogIf(map);
for (Blog blog : blogs) {
System.out.println(blog);
}
}

總結(jié)
到此這篇關(guān)于Mybatis動(dòng)態(tài)SQL之IF語(yǔ)句的文章就介紹到這了,更多相關(guān)Mybatis動(dòng)態(tài)SQL IF語(yǔ)句內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis配置文件解析與MyBatis實(shí)例演示
這篇文章主要介紹了MyBatis配置文件解析與MyBatis實(shí)例演示以及怎樣編譯安裝MyBatis,需要的朋友可以參考下2022-04-04
Java開發(fā)深入分析講解二叉樹的遞歸和非遞歸遍歷方法
樹是一種重要的非線性數(shù)據(jù)結(jié)構(gòu),直觀地看,它是數(shù)據(jù)元素(在樹中稱為結(jié)點(diǎn))按分支關(guān)系組織起來(lái)的結(jié)構(gòu),很象自然界中的樹那樣。樹結(jié)構(gòu)在客觀世界中廣泛存在,如人類社會(huì)的族譜和各種社會(huì)組織機(jī)構(gòu)都可用樹形象表示,本篇介紹二叉樹的遞歸與非遞歸遍歷的方法2022-05-05
Java基于Semaphore構(gòu)建阻塞對(duì)象池
這篇文章主要介紹了Java基于Semaphore構(gòu)建阻塞對(duì)象池,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
Springboot實(shí)現(xiàn)添加本地模塊依賴方式
這篇文章主要介紹了Springboot實(shí)現(xiàn)添加本地模塊依賴方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
Java時(shí)間類Date類和Calendar類的使用詳解
這篇文章主要介紹了Java時(shí)間類Date類和Calendar類的使用詳解,需要的朋友可以參考下2017-08-08
springmvc HttpServletRequest 如何獲取c:forEach的值
這篇文章主要介紹了springmvc HttpServletRequest 如何獲取c:forEach的值方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
關(guān)于SpringBoot自定義條件注解與自動(dòng)配置
這篇文章主要介紹了關(guān)于SpringBoot自定義條件注解與自動(dòng)配置,Spring Boot的核心功能就是為整合第三方框架提供自動(dòng)配置,而本文則帶著大家實(shí)現(xiàn)了自己的自動(dòng)配置和Starter,需要的朋友可以參考下2023-07-07
詳解spring集成mina實(shí)現(xiàn)服務(wù)端主動(dòng)推送(包含心跳檢測(cè))
本篇文章主要介紹了詳解spring集成mina實(shí)現(xiàn)服務(wù)端主動(dòng)推送(包含心跳檢測(cè)),具有一定的參考價(jià)值,與興趣的可以了解一下2017-09-09

