mybatis的靜態sql詳解(精)。本站提示廣大學習愛好者:(mybatis的靜態sql詳解(精))文章只能為提供參考,不一定能成為您想要的結果。以下是mybatis的靜態sql詳解(精)正文
MyBatis 的一個壯大的特征之一平日是它的靜態 SQL 才能。假如你有應用 JDBC 或其他 類似框架的經歷,你就明確前提地串連 SQL 字符串在一路是何等的苦楚,確保不克不及忘了空 格或在列表的最初省略逗號。靜態 SQL 可以完全處置這類苦楚。
平日應用靜態SQL弗成能是自力的一部門,MyBatis固然應用一種壯大的靜態SQL說話來改良這類情況,這類說話可以被用在隨意率性映照的SQL語句中。
靜態SQL元素和應用 JSTL或其他類似的基於XML的文本處置器類似。在MyBatis之前的版本中,有許多的元素須要來懂得。MyBatis3年夜年夜晉升了它們,如今用不到本來一半 的元素就可以任務了。MyBatis采取功效壯大的基於OGNL的表達式來清除其他元素。
MyBatis頂用於完成靜態SQL的元素重要有:
if
choose(when,otherwise)
trim
where
set
foreach
if就是簡略的前提斷定,應用if語句我們可以完成某些簡略的前提選擇。
先來看以下一個例子:
Xml代碼
<select id="dynamicIfTest" parameterType="Blog" resultType="Blog">
select * from t_blog where 11 = 1
<if test="title != null">
and title = #{title}
</if>
<if test="content != null">
and content = #{content}
</if>
<if test="owner != null">
and owner = #{owner}
</if>
</select>
這條語句的意思異常簡略,假如你供給了title參數,那末就要知足title=#{title},異樣假如你供給了Content和Owner的時刻,它們也須要知足響應的前提,以後就是前往知足這些前提的一切Blog,這長短常有效的一個功效,以往我們應用其他類型框架或許直接應用JDBC的時刻, 假如我們要到達異樣的選擇後果的時刻,我們就須要拼SQL語句,這是極端費事的,比起來,上述的靜態SQL就要簡略多了。
choose元素的感化就相當於JAVA中的switch語句,根本上跟JSTL中的choose的感化和用法是一樣的,平日都是與when和otherwise搭配的。
看以下一個例子:
Xml代碼
<select id="dynamicChooseTest" parameterType="Blog" resultType="Blog">
select * from t_blog where 11 = 1
<choose>
<when test="title != null">
and title = #{title}
</when>
<when test="content != null">
and content = #{content}
</when>
<otherwise>
and owner = "owner1"
</otherwise>
</choose>
</select>
when元素表現當when中的前提知足的時刻就輸入個中的內容,跟JAVA中的switch後果差不多的是依照前提的次序,當when中有前提知足的時刻,就會跳出choose,即一切的when和otherwise前提中,只要一個會輸入,當一切的我很前提都不知足的時刻就輸入otherwise中的內容。
所以上述語句的意思異常簡略, 當title!=null的時刻就輸入and titlte = #{title},不再往下斷定前提,當title為空且content!=null的時刻就輸入and content = #{content},當一切前提都不知足的時刻就輸入otherwise中的內容。
where語句的感化重要是簡化SQL語句中where中的前提斷定的,先看一個例子,再說明一下where的利益。
Xml代碼
<select id="dynamicWhereTest" parameterType="Blog" resultType="Blog">
select * from t_blog
<where>
<if test="title != null">
title = #{title}
</if>
<if test="content != null">
and content = #{content}
</if>
<if test="owner != null">
and owner = #{owner}
</if>
</where>
</select>
where元素的感化是會在寫入where元素的處所輸入一個where,別的一個利益是你不須要斟酌where元素外面的前提輸入是甚麼模樣的,MyBatis會智能的幫你處置,假如一切的前提都不知足那末MyBatis就會查出一切的記載,假如輸入後是and 開首的,MyBatis會把第一個and疏忽,固然假如是or開首的,MyBatis也會把它疏忽;另外,在where元素中你不須要斟酌空格的成績,MyBatis會智能的幫你加上。像上述例子中,假如title=null, 而content != null,那末輸入的全部語句會是select * from t_blog where content = #{content},而不是select * from t_blog where and content = #{content},由於MyBatis會智能的把首個and 或 or 給疏忽。
trim元素的重要功效是可以在本身包括的內容前加上某些前綴,也能夠在厥後加上某些後綴,與之對應的屬性是prefix和suffix;可以把包括內容的首部某些內容籠罩,即疏忽,也能夠把尾部的某些內容籠罩,對應的屬性是prefixOverrides和suffixOverrides;正由於trim有如許的功效,所以我們也能夠異常簡略的應用trim來取代where元素的功效,示例代碼以下:
Xml代碼
<select id="dynamicTrimTest" parameterType="Blog" resultType="Blog">
select * from t_blog
<trim prefix="where" prefixOverrides="and |or">
<if test="title != null">
title = #{title}
</if>
<if test="content != null">
and content = #{content}
</if>
<if test="owner != null">
or owner = #{owner}
</if>
</trim>
</select>
set元素重要是用在更新操作的時刻,它的重要功效和where元素實際上是差不多的,重要是在包括的語句前輸入一個set,然後假如包括的語句是以逗號停止的話將會把該逗號疏忽,假如set包括的內容為空的話則會失足。有了set元素我們便可以靜態的更新那些修正了的字段。上面是一段示例代碼:
Xml代碼
<update id="dynamicSetTest" parameterType="Blog">
update t_blog
<set>
<if test="title != null">
title = #{title},
</if>
<if test="content != null">
content = #{content},
</if>
<if test="owner != null">
owner = #{owner}
</if>
</set>
where id = #{id}
</update>
上述示例代碼中,假如set中一個前提都不知足,即set中包括的內容為空的時刻就會報錯。
foreach的重要用在構建in前提中,它可以在SQL語句中停止迭代一個聚集。foreach元素的屬性重要有item,index,collection,open,separator,close。item表現聚集中每個元素停止迭代時的別號,index指定一個名字,用於表現在迭代進程中,每次迭代到的地位,open表現該語句以甚麼開端,separator表現在每次停止迭代之間以甚麼符號作為分隔符,close表現以甚麼停止,在應用foreach的時刻最症結的也是最輕易失足的就是collection屬性,該屬性是必需指定的,然則在分歧情形下,該屬性的值是紛歧樣的,重要有一下3種情形:
1.假如傳入的是單參數且參數類型是一個List的時刻,collection屬性值為list
2.假如傳入的是單參數且參數類型是一個array數組的時刻,collection的屬性值為array
3.假如傳入的參數是多個的時刻,我們就須要把它們封裝成一個Map了,固然單參數也能夠封裝成map,現實上假如你在傳入參數的時刻,在MyBatis外面也是會把它封裝成一個Map的,map的key就是參數名,所以這個時刻collection屬性值就是傳入的List或array對象在本身封裝的map外面的key
上面分離來看看上述三種情形的示例代碼:
1.單參數List的類型:
Xml代碼
<select id="dynamicForeachTest" resultType="Blog">
select * from t_blog where id in
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
上述collection的值為list,對應的Mapper是如許的
Java代碼
public List<Blog> dynamicForeachTest(List<Integer> ids);
測試代碼:
Java代碼
@Test
public void dynamicForeachTest() {
SqlSession session = Util.getSqlSessionFactory().openSession();
BlogMapper blogMapper = session.getMapper(BlogMapper.class);
List<Integer> ids = new ArrayList<Integer>();
ids.add(1);
ids.add(3);
ids.add(6);
List<Blog> blogs = blogMapper.dynamicForeachTest(ids);
for (Blog blog : blogs)
System.out.println(blog);
session.close();
}
2.單參數array數組的類型:
Xml代碼
<select id="dynamicForeach2Test" resultType="Blog">
select * from t_blog where id in
<foreach collection="array" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
上述collection為array,對應的Mapper代碼:
Java代碼
public List<Blog> dynamicForeach2Test(int[] ids);
對應的測試代碼:
Java代碼
@Test
public void dynamicForeach2Test() {
SqlSession session = Util.getSqlSessionFactory().openSession();
BlogMapper blogMapper = session.getMapper(BlogMapper.class);
int[] ids = new int[] {1,3,6,9};
List<Blog> blogs = blogMapper.dynamicForeach2Test(ids);
for (Blog blog : blogs)
System.out.println(blog);
session.close();
}
3.本身把參數封裝成Map的類型
Xml代碼
<select id="dynamicForeach3Test" resultType="Blog">
select * from t_blog where title like "%"#{title}"%" and id in
<foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
上述collection的值為ids,是傳入的參數Map的key,對應的Mapper代碼:
Java代碼
public List<Blog> dynamicForeach3Test(Map<String, Object> params);
對應測試代碼:
Java代碼
@Test
public void dynamicForeach3Test() {
SqlSession session = Util.getSqlSessionFactory().openSession();
BlogMapper blogMapper = session.getMapper(BlogMapper.class);
final List<Integer> ids = new ArrayList<Integer>();
ids.add(1);
ids.add(2);
ids.add(3);
ids.add(6);
ids.add(7);
ids.add(9);
Map<String, Object> params = new HashMap<String, Object>();
params.put("ids", ids);
params.put("title", "中國");
List<Blog> blogs = blogMapper.dynamicForeach3Test(params);
for (Blog blog : blogs)
System.out.println(blog);
session.close();
}
以上內容是本文論述mybatis的靜態sql詳解,願望年夜家愛好。