mybatis入門基礎----動態SQL

來源:互聯網
上載者:User

標籤:遍曆   封裝   result   輸入參數   test   它的   目錄   指定   基礎   

原文:http://www.cnblogs.com/selene/p/4613035.html

 

閱讀目錄

  • 一:動態SQL
  • 二:SQL片段
  • 三:foreach
回到頂部一:動態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 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     <!-- 實現下邊的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:每個遍曆產生對象中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     }

 

mybatis入門基礎----動態SQL

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.