Java MySQL 批量查詢數據,每次查詢10條


因為 數據量比較多, 比如每次

        /**
         * 批量查詢
         * @param sourList
         * @param batchCount
         * @param userMapper
         * @return
         */
        public  List<User> dealBySubList(Set<String> sourList,Integer batchCount,UserMapper userMapper){
            
            List<User> userList = new ArrayList<User>();
            List<String> list = new ArrayList<String>();
            for(String obj:sourList){
                
                list.add(obj);
            }
            int sourListSize = list.size();
            int subCount = sourListSize%batchCount==0 ? sourListSize/batchCount : sourListSize/batchCount+1;
            int startIndext = 0;
            int stopIndext = 0;
            for(int i=0;i<subCount;i++){
                stopIndext = (i==subCount-1) ? stopIndext + sourListSize%batchCount : stopIndext + batchCount;
                List<String> tempList = new ArrayList<String>(list.subList(startIndext, stopIndext));
                if(tempList.size()>0){
                 userList.addAll(userMapper.getUserListByToken(tempList));
                }
                startIndext = stopIndext;
            }
            return userList;
        }

 

查詢10000 條信息, 不可能 查10000次, 或者, 不可能用in 一次查詢, 所以使用 分頁批量查詢,一下是代碼


免責聲明!

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



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