MyBatis的官方文檔中是這樣介紹的?java
動態 SQL 是 MyBatis 的強大特性之一。若是你使用過 JDBC 或其它相似的框架,你應該能理解根據不一樣條件拼接 SQL 語句有多痛苦,例如拼接時要確保不能忘記添加必要的空格,還要注意去掉列表最後一個列名的逗號。利用動態 SQL,能夠完全擺脫這種痛苦。sql
使用動態 SQL 並不是一件易事,但藉助可用於任何 SQL 映射語句中的強大的動態 SQL 語言,MyBatis 顯著地提高了這一特性的易用性。apache
若是你以前用過 JSTL 或任何基於類 XML 語言的文本處理器,你對動態 SQL 元素可能會感受似曾相識。在 MyBatis 以前的版本中,須要花時間瞭解大量的元素。藉助功能強大的基於 OGNL 的表達式,MyBatis 3 替換了以前的大部分元素,大大精簡了元素種類,如今要學習的元素種類比原來的一半還要少。數組
換句話說,咱們能夠根據傳入參數的不一樣,來執行不一樣的查詢條件。session
咱們首先建立一個Mapper接口,起名爲:UserMapper
,並增長一個方法mybatis
public interface UserMapper { public List<User> findByCondition(User user); }
同時建立一個xml文件,起名爲UserMapper.xml
而後編寫SQLapp
<mapper namespace="com.dxh.dao.UserMapper"> <select id="findByCondition" parameterType="com.dxh.pojo.User" resultType="com.dxh.pojo.User"> SELECT * FROM user where 1=1 <where> <if test="id != null"> id = #{id} </if> </where> </select> </mapper>
這個SQL的意思是:框架
SELECT * FROM user where id = #{id}
SELECT * FROM user where 1=1
很明顯咱們能夠看到where 1=1
是多餘的,所以咱們能夠這樣寫:學習
<select id="findByCondition" parameterType="com.dxh.pojo.User" resultType="com.dxh.pojo.User"> SELECT * FROM user where 1=1 <where> <if test="id != null"> id = #{id} </if> </where> </select>
編寫一個測試類:測試
package com.dxh.test; import com.dxh.dao.UserMapper; import com.dxh.pojo.User; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Test; import java.io.IOException; import java.io.InputStream; import java.util.List; public class TestMain { @Test public void test1() throws IOException { InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = build.openSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); User user = new User(); user.setId(1); List<User> byCondition = mapper.findByCondition(user); for (User user1 : byCondition) { System.out.println(user1); } System.out.println("======"); User user2 = new User(); List<User> byCondition2 = mapper.findByCondition(user2); for (User user3 : byCondition2) { System.out.println(user3); } } }
咱們執行兩次mapper.findByCondition()
,分別傳入user和user2,一個的id有被賦值,一個沒有,最後的結果爲:
User{id=1, username='lucy'} ====== User{id=1, username='lucy'} User{id=2, username='李四'} User{id=3, username='zhaowu'}
當咱們須要查詢出 id爲一、二、3時應該怎麼作? SQL應該這樣寫:SELECT * FROM user where id in (1,2,3)
。那麼使用mybatis的foreach標籤應該如何使用?
在UserMapper
接口中增長一個方法: List<User> findByIds(int[] arr);
public List<User> findByIds(int[] arr);
在UserMapper.xml
中編寫:
<select id="findByIds" parameterType="list" resultType="com.dxh.pojo.User"> SELECT * FROM user <where> <foreach collection="array" open="id in (" close=")" item="id" separator=","> #{id} </foreach> </where> </select>
咱們能夠看到,foreach中咱們使用到了5個值:
array
,若是是集合就是list
SELECT * FROM user where id in (1,2,3)
正確的SQL應該這樣寫,那麼open就是填寫咱們須要拼接的前半部分......where id in (1,2,3)
1,2,3之間用,
分割。@Test public void test2() throws IOException { InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = build.openSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); int[] arr={1,3}; List<User> byCondition = mapper.findByIds(arr); for (User user1 : byCondition) { System.out.println(user1); } }
輸出結果:
User{id=1, username='lucy'} User{id=3, username='zhaowu'}
正確~
這裏只是介紹了兩個常常使用的標籤,mybatis中還有不少標籤,好比choose、when、otherwise、trim、set
等等
值得一說的是Mybatis的官方網站中已經支持中文了,母語看着更舒服~