1.1.定義java
mybatis核心對sql語句進行靈活操做,經過表達式進行判斷,對sql進行靈活拼接、組裝。sql
1.2.案例需求數組
用戶信息綜合查詢列表這個statement的定義使用動態sql,對查詢條件進行判斷,若是輸入參數不爲空才進行查詢拼接。mybatis
1.3.UserMapper.xmlapp
1 <!-- 用戶信息綜合查詢 2 #{userCustom.sex}:取出pojo包裝對象中性別值 3 ${userCustom.username}:取出pojo對象中用戶名稱 4 --> 5 <select id="findUserList" parameterType="com.mybatis.entity.UserQueryVo" 6 resultType="com.mybatis.entity.UserCustom"> 7 select * from t_user 8 <!-- 動態sql查詢:where能夠自動去掉第一個and --> 9 <where> 10 <if test="userCustom!=null"> 11 <if test="userCustom.sex!=null and userCustom.sex!='' "> 12 and sex=#{userCustom.sex} 13 </if> 14 <if test="userCustom.username!=null and userCustom.username!='' "> 15 and username=#{userCustom.username} 16 </if> 17 </if> 18 </where> 19 <!-- where sex=#{userCustom.sex} and username LIKE '%${userCustom.username}%' --> 20 </select>
1.4.測試代碼測試
1 @Test 2 public void testFindUserList() { 3 SqlSession sqlSession = sqlSessionFactory.openSession(); 4 //創造查詢條件 5 UserQueryVo userQueryVo = new UserQueryVo(); 6 UserCustom userCustom = new UserCustom(); 7 // userCustom.setSex("2"); 8 //這裏使用動態sql,若是不設置某個值,條件不會拼接sql中 9 userCustom.setUsername("小"); 10 userQueryVo.setUserCustom(userCustom); 11 // 建立Usermapper對象,mybatis自動生成mapper代理對象 12 UserMapper mapper = sqlSession.getMapper(UserMapper.class); 13 List<UserCustom>list=mapper.findUserList(userQueryVo); 14 //測試動態sql,屬性的非空判斷測試 15 // List<UserCustom>list=mapper.findUserList(null); 16 System.out.println(list); 17 sqlSession.commit(); 18 sqlSession.close(); 19 }
2.1.需求this
將上邊的動態sql判斷代碼抽取出來,組成一個sql片斷,其它的statement中就能夠引用sql片斷,方便開發。spa
2.2.定義sql片斷 代理
1 <!-- 定義sql片斷,Id是惟一標識 2 建議:是基於單表來定義sql片斷,這樣的話sql片斷的可重用性才高,在sql片斷中不要包含where 3 --> 4 <sql id="query_user_where" > 5 <if test="userCustom!=null"> 6 <if test="userCustom.sex!=null and userCustom.sex!='' "> 7 and sex=#{userCustom.sex} 8 </if> 9 <if test="userCustom.username!=null and userCustom.username!='' "> 10 and username=#{userCustom.username} 11 </if> 12 </if> 13 </sql>
2.3.在mapper.xml中定義的statement中引用sql片斷code
1 <!-- 用戶信息綜合查詢 2 #{userCustom.sex}:取出pojo包裝對象中性別值 3 ${userCustom.username}:取出pojo對象中用戶名稱 4 --> 5 <select id="findUserList" parameterType="com.mybatis.entity.UserQueryVo" 6 resultType="com.mybatis.entity.UserCustom"> 7 select * from t_user 8 <!-- 動態sql查詢:where能夠自動去掉第一個and --> 9 <where> 10 <!-- 引用sql片斷的id,若是refid指定的不在本mapper.xml中,須要前邊加namespace --> 11 <include refid="query_user_where"></include> 12 <!-- 這裏能夠引用其它的sql片斷 --> 13 </where> 14 </select>
做用:向sql傳遞數組或者list,mybatis使用foreach解析
在用戶查詢列表和查詢總數的statement中增長多個id輸入查詢。
3.1.需求
sql語句以下:
兩種方法:
SELECT * FROM t_user WHERE id=1 OR id=10 OR id=16
SELECT * FROM t_user WHERE id IN(1,10,16)
3.2.在輸入參數包裝類型中添加List<Integer> ids 傳入多個id
1 package com.mybatis.entity; 2 3 import java.util.List; 4 5 /** 6 * 7 * @ClassName: UserQueryVo 8 * @Description: TODO(包裝類型) 9 * @author warcaft 10 * 11 */ 12 public class UserQueryVo { 13 14 public List<Integer> ids; 15 16 public List<Integer> getIds() { 17 return ids; 18 } 19 20 public void setIds(List<Integer> ids) { 21 this.ids = ids; 22 } 23 }
3.3.mapper.xml代碼
1 <!-- 實現下邊的sql拼接 2 select * from t_user where id=1 OR id=2 OR id=3 3 --> 4 <select id="findUserByIds" parameterType="com.mybatis.entity.UserQueryVo" 5 resultType="com.mybatis.entity.User"> 6 select * from t_user 7 <where> 8 <if test="ids!=null"> 9 <!-- 使用foreach遍歷ids 10 collection:指定輸入對象的集合屬性 11 item:每一個遍歷生成對象中 12 open:開始遍歷時拼接的串 13 close:技術遍歷時拼接的串 14 separator:遍歷的兩個對象中須要拼接的串 15 --> 16 <foreach collection="ids" item="user_id" open="AND (" close=")" separator="or"> 17 id=#{user_id} 18 </foreach> 19 </if> 20 </where> 21 </select>
select * from t_user where id in(1,2,3)的mapper.xml配置
1 <select id="findUserByIds" parameterType="com.mybatis.entity.UserQueryVo" 2 resultType="com.mybatis.entity.User"> 3 select * from t_user 4 <where> 5 <if test="ids!=null"> 6 <!-- 7 使用foreach遍歷ids 8 collection:指定輸入對象的集合屬性 9 item:每一個遍歷生成對象中 10 open:開始遍歷時拼接的串 11 close:技術遍歷時拼接的串 12 separator:遍歷的兩個對象中須要拼接的串 13 --> 14 <!-- 實現「 select * from t_user where id in(1,2,3)」拼接 --> 15 <foreach collection="ids" item="user_id" open="AND id in (" close=")" separator=","> 16 id=#{user_id} 17 </foreach> 18 </if> 19 </where> 20 </select>
userMapper.java代碼
1 public interface UserMapper { 2 //ids查詢用戶數據 3 public List<User> findUserByIds(UserQueryVo userQueryVo); 4 }
Junit測試代碼
1 @Test 2 public void findUserByIdsTest() { 3 SqlSession sqlSession = sqlSessionFactory.openSession(); 4 // 建立Usermapper對象,mybatis自動生成mapper代理對象 5 UserMapper mapper = sqlSession.getMapper(UserMapper.class); 6 //創造查詢條件 7 UserQueryVo userQueryVo = new UserQueryVo(); 8 //傳入多個id 9 List<Integer> ids=new ArrayList<Integer>(); 10 ids.add(1); 11 ids.add(2); 12 ids.add(3); 13 //將ids經過userQueryVo傳入statement中 14 userQueryVo.setIds(ids); 15 //調用userMapper的代碼 16 List<UserCustom> userList= mapper.findUserList(userQueryVo); 17 System.out.println(userList); 18 sqlSession.close(); 19 }