1. 程式人生 > >Mybatis的Mapper檔案中用註解方式寫動態Sql語句演示

Mybatis的Mapper檔案中用註解方式寫動態Sql語句演示

引言

正如我們所知在mapper檔案中用註解的方式寫一些普通的查詢,刪除sql語句格式都相對簡單,而且一般我們如果有動態sql需要的話,可以採用把sql寫在xml檔案,然後根據Mapper內方法id進行匹配,實現我們複雜的查詢或者迴圈新增等操作。
但是,本人經歷過公司同意要求把sql用註解的方式進行編寫,當中不乏一些動態sql的編寫,在此做個小總結,希望對大家有幫助。
首先如果註解寫動態sql的話會用到 <script> </scrpit>標籤 ! 大家切記 !

程式碼展示

多條件查詢以及分頁低配版(返回List集合)

@Select
({"<script> SELECT * FROM user WHERE " + "<if test= "mobile != null and mobile != ''"> mobile = #{mobile} </if>" + "<if test= "star != null and star != ''"> AND star=#{star} </if>" + "<if test ='startTime != null '> AND UNIX_TIMESTAMP(update_time) >= #{startTime} </if>"
+ "<if test ='endTime != null '> AND UNIX_TIMESTAMP(update_time) &lt;= #{endTime} </if>" + "<if test="count > 0"> LIMIT #{offset}, #{count} </if> </script>"}) //引數是物件,攜帶多引數進行模糊分頁查詢 List<User> getUserList(Request request); /** * request物件攜帶mobile(電話String),star(分數Byte),startTime(開始時間Long),endTime(結束時間Long),offset(分頁引數,偏移量int),count(分頁引數,每頁返回資料條數) * 因為前臺傳參時startTime和endTime是Long型別引數,所以sql查詢是用UNIX_TIMESTAMP()函式處理表內資料使其變為時間戳型別,以此來方便兩者比較 */

多條件查詢以及分頁升級版(返回List集合)

//把條件語句單獨提出來,方便其他語句使用,比如查詢資料集合資訊,查詢資料條數的sql語句
String QUERY_CODE_SQL = "<if  test= \"mobile != null and mobile != ''\">  mobile = #{mobile} </if> " +
            "<if  test= \"star != null and star != ''\">  AND star=#{star} </if>" +
            "<if test ='startTime != null '>  AND UNIX_TIMESTAMP(update_time) >= #{startTime} </if>" +
            "<if test ='endTime != null '>   AND UNIX_TIMESTAMP(update_time) &lt;= #{endTime} </if>" +
            "<if test=\"count > 0\">  LIMIT #{offset}, #{count} </if>";

@Select({"<script> SELECT * FROM user WHERE" + QUERY_CODE_SQL + " </script>"})
//引數是物件,攜帶多引數進行模糊分頁查詢集合資訊
List<User> getUserList(Request request);

@Select({"<script> SELECT COUNT(*) FROM user WHERE" + QUERY_CODE_SQL + " </script>"})
//引數是物件,攜帶多引數進行模糊查詢資料條數
List<User> getUserList(Request request);

根據某條件迴圈查詢相關資訊(返回List集合)

 @Select({"<script> ",
            "SELECT * FROM user ",
            "WHERE id IN ",
            "<foreach collection = 'userIds' separator = ',' open = '(' close = ')' item = 'id'>  ",
            "#{id} ",
            "</foreach> ",
            "</script>"})
List<User> getUserListByUserIds(@Param("userIds") Set<Long> userIdSet);
/**
 * 引數為Set集合,集合內攜帶條件(多個使用者的id==userIds)
 * 	collection :collection屬性的值有三個分別是List、Array、Map三種,分別對應的引數型別為:List、陣列、map集合,我在上面傳的引數為Set集合(set內沒有重複資料,比List集合實用,避免反覆查詢),所以值為別名userIds,引數沒有別名的話此處用collection;
 *	item : 表示在迭代過程中每一個元素的別名
 *	open :字首
 *	close :字尾
 */

根據某條件迴圈查詢相關資訊(返回Map集合)

@Select("<script>" +
        "SELECT  * FROM user_info WHERE" + 
        " fellow_id=#{fellow_id} " + 
        " AND user_id IN " + 
        "<foreach collection = 'user_ids' separator = ',' open = '(' close = ')' item = 'user_id'>" + 
        " #{user_id}" + 
        "</foreach>" +
        "</script>")
@MapKey("userId")
Map<String, CalendarTask> getCalendarTaskMap(
        @Param("fellow_id") Long fellowId,
        @Param("user_ids") List<Long> userIds);
/**
 * 引數1是fellowId;引數2是List集合,集合內攜帶條件(多個使用者的user_id==userIds);
 *	@MapKey()註解,我理解的是規定Map集合的key,本map的key就是每一個userId,所以引數是該sql語句查詢結果(*查詢出的資料有userId欄位)的每條資料中的欄位值==>userId;
 */