1.1.定義
mybatis核心對sql語句進(jìn)行靈活操作,通過表達(dá)式進(jìn)行判斷,對sql進(jìn)行靈活拼接、組裝。
1.2.案例需求
用戶信息綜合查詢列表這個(gè)statement的定義使用動(dòng)態(tài)sql,對查詢條件進(jìn)行判斷,如果輸入?yún)?shù)不為空才進(jìn)行查詢拼接。
1.3.UserMapper.xml
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 <!-- 動(dòng)態(tài)sql查詢:where可以自動(dòng)去掉第一個(gè)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 //創(chuàng)造查詢條件 5 UserQueryVo userQueryVo = new UserQueryVo(); 6 UserCustom userCustom = new UserCustom(); 7 // userCustom.setSex("2"); 8 //這里使用動(dòng)態(tài)sql,如果不設(shè)置某個(gè)值,條件不會(huì)拼接sql中 9 userCustom.setUsername("小");10 userQueryVo.setUserCustom(userCustom);11 // 創(chuàng)建Usermapper對象,mybatis自動(dòng)生成mapper代理對象12 UserMapper mapper = sqlSession.getMapper(UserMapper.class);13 List<UserCustom>list=mapper.findUserList(userQueryVo);14 //測試動(dòng)態(tài)sql,屬性的非空判斷測試15 // List<UserCustom>list=mapper.findUserList(null);16 System.out.PRintln(list);17 sqlSession.commit();18 sqlSession.close();19 }二:SQL片段2.1.需求
將上邊的動(dòng)態(tài)sql判斷代碼抽取出來,組成一個(gè)sql片段,其它的statement中就可以引用sql片段,方便開發(fā)。
2.2.定義sql片段
1 <!-- 定義sql片段,Id是唯一標(biāo)識(shí) 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片段
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 <!-- 動(dòng)態(tài)sql查詢:where可以自動(dòng)去掉第一個(gè)and --> 9 <where>10 <!-- 引用sql片段的id,如果refid指定的不在本mapper.xml中,需要前邊加namespace -->11 <include refid="query_user_where"></include>12 <!-- 這里可以引用其它的sql片段 -->13 </where>14 </select>三:foreach作用:向sql傳遞數(shù)組或者list,mybatis使用foreach解析
在用戶查詢列表和查詢總數(shù)的statement中增加多個(gè)id輸入查詢。
3.1.需求
sql語句如下:
兩種方法:
SELECT*FROMt_user WHEREid=1ORid=10ORid=16
SELECT*FROMt_user WHEREidIN(1,10,16)
3.2.在輸入?yún)?shù)包裝類型中添加List<Integer> ids 傳入多個(gè)id
1 package com.mybatis.entity; 2 3 import java.util.List; 4 5 /** 6 * 7 * @ClassName: UserQueryVo 8 * @Description: TODO(包裝類型) 9 * @author warcaft10 * 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 <!-- 實(shí)現(xiàn)下邊的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遍歷ids10 collection:指定輸入對象的集合屬性11 item:每個(gè)遍歷生成對象中12 open:開始遍歷時(shí)拼接的串13 close:技術(shù)遍歷時(shí)拼接的串14 separator:遍歷的兩個(gè)對象中需要拼接的串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:每個(gè)遍歷生成對象中10 open:開始遍歷時(shí)拼接的串11 close:技術(shù)遍歷時(shí)拼接的串12 separator:遍歷的兩個(gè)對象中需要拼接的串13 -->14 <!-- 實(shí)現(xiàn)“ 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查詢用戶數(shù)據(jù)3 public List<User> findUserByIds(UserQueryVo userQueryVo);4 }Junit測試代碼
1 @Test 2 public void findUserByIdsTest() { 3 SqlSession sqlSession = sqlSessionFactory.openSession(); 4 // 創(chuàng)建Usermapper對象,mybatis自動(dòng)生成mapper代理對象 5 UserMapper mapper = sqlSession.getMapper(UserMapper.class); 6 //創(chuàng)造查詢條件 7 UserQueryVo userQueryVo = new UserQueryVo(); 8 //傳入多個(gè)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 //調(diào)用userMapper的代碼16 List<UserCustom> userList= mapper.findUserList(userQueryVo);17 System.out.println(userList);18 sqlSession.close();19 }新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注