mybatis之批量查詢


關於MyBatis批量更新和添加,參考我的如下文章即可:
MyBatis的批量更新實例

MyBatis的批量添加實例

另外不管是批量的新增、刪除、修改、查詢也好,還是單個新增、刪除、修改查詢也罷。都會用到動態SQL。

關於MyBatis的動態SQL可以參考我的這篇文章,如下:
MyBatis實戰之動態SQL

今天這篇文章主要是為了記錄,最近用MyBatis-Plus特別多,很多增、刪、改、查以及批量相關操作,拿來即用,戊戌時自己編寫。特輕松。

但是因為最近的一個需求不得不自己手寫批量查詢例子(主要涉及聯表之類的操作)。

正好以該例子進行講解,也給我,給大家做個小小參考。

關鍵XML:

<select id="getStudentSubmitHomeWorkListInfos" resultMap="BaseResultMap">

SELECT s.`solution_id`,s.`problem_id`,s.`user_id`,s.`nick`,s.`result`,p.title
FROM solution AS s left join problem as p ON(s.problem_id = p.problem_id) WHERE  
 s.`user_id` in
   <foreach collection="list" item="userId" open="(" close=")" separator=",">
   #{userId}
   </foreach>

</select>

foreach相關參數解釋:

collection配置的users是傳遞進來的參數名稱,它可以是一個數組或者List、Set等集合;

item配置的是循環中當前的元素;

index配置的是當前元素在集合的位置下標;

separator是各個元素的間隔符;

open和colose代表的是以什么符號將元素包裹起來;

關鍵DAO:

public List<Solution> getStudentSubmitHomeWorkListInfos(List<String> userId);

單元測試:

@Test
public void testCollectionRun() {
    
    List<String> userId = new ArrayList<String>();
    userId.add("admin");
    userId.add("student");


        
    List<Solution> solutionList = solutionDao.getStudentSubmitHomeWorkListInfos(userId);        
    for (Solution solution : solutionList) {
        
        System.out.println("solution:"+solution.getNick()+"||"+solution.getResult()+"||"+solution.getTitle());
    }
    
    
    
}

順便說說批量查詢的應用場景:
(1)特定的場景獲取用戶訂單列表數;
(2)獲取某一個題目許學生提交的結果(以用戶id作為查詢參數,該用戶id非主鍵);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM