mybatis入門基礎(五)----動態SQL

阿赫瓦里發表於2015-07-01

一:動態SQL

  1.1.定義

    mybatis核心對sql語句進行靈活操作,通過表示式進行判斷,對sql進行靈活拼接、組裝。

  1.2.案例需求

    使用者資訊綜合查詢列表這個statement的定義使用動態sql,對查詢條件進行判斷,如果輸入引數不為空才進行查詢拼接。

  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         <!-- 動態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     }

二:SQL片段

  2.1.需求

    將上邊的動態sql判斷程式碼抽取出來,組成一個sql片段,其它的statement中就可以引用sql片段,方便開發。

  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片段

 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>

三:foreach

  作用:向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     }

 

相關文章