Mybatis動態(tài)SQL之IF語句詳解
Mysql 5.0 以后,支持了動態(tài)sql語句,我們可以通過傳遞不同的參數(shù)得到我們想要的值.
1. Mybatis–動態(tài)SQL之IF語句
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)會把所有數(shù)據(jù)查出來
// 第一種情況,不加任何查詢條件,默認(rèn)會把所有數(shù)據(jù)查出來 @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個參數(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動態(tài)SQL之IF語句的文章就介紹到這了,更多相關(guān)Mybatis動態(tài)SQL IF語句內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringData @Query和@Modifying注解原理解析
這篇文章主要介紹了SpringData @Query和@Modifying注解原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08Java中Connection timed out和Connection refused的區(qū)別講解
今天小編就為大家分享一篇關(guān)于Java中Connection timed out和Connection refused的區(qū)別講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-04-04詳解JAVA抓取網(wǎng)頁的圖片,JAVA利用正則表達(dá)式抓取網(wǎng)站圖片
這篇文章主要介紹了詳解JAVA抓取網(wǎng)頁的圖片,JAVA利用正則表達(dá)式抓取網(wǎng)站圖片,非常具有實用價值,需要的朋友可以參考下。2016-12-12在windows下揪出java程序占用cpu很高的線程并完美解決
這篇文章主要介紹了在windows下揪出java程序占用cpu很高的線程并完美解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01Java設(shè)計模式之工廠模式(Factory模式)介紹
這篇文章主要介紹了Java設(shè)計模式之工廠模式(Factory模式)介紹,本文講解了為何使用工廠模式、工廠方法、抽象工廠、Java工廠模式舉例等內(nèi)容,需要的朋友可以參考下2015-03-03Mybatis-plus selectByMap條件查詢方式
這篇文章主要介紹了Mybatis-plus selectByMap條件查詢方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06