2017-08-07 17 views
0

我想打一個PageRequestJdbcTemplate。什麼我不想做的就是使用JPA。我搜索了近4個小時,但沒有找到任何東西,如果可能的usefull.Please告訴我,我該怎麼辦呢如何使Pagerequest與JdbcTemplate的春季啓動

編輯 我想從Oracle重新加載數據,但不希望使用JPARepository 這裏是我的代碼。此是我的控制器

@RestController 
@RequestMapping(value = "/admin") 
public class OrderController { 

@Autowired 
private OrderService orderService; 

@RequestMapping(value = "/reload", method = RequestMethod.GET) 
public List<Order> reload(){ 
    return orderService.reload(); 
} 

這是我的服務

@Service 
public class OrderService { 

@Autowired 
private OrderRepository orderRepository; 

public List<Order> reload(){ 
    return orderRepository.reload(); 
} 

這是我的倉庫

@Repository 
public interface OrderRepository { 

public List<Order> reload(); 
} 

這是我repositoryImpl類

@Repository 
public class OrderRepositoryImpl implements OrderRepository{ 

@Autowired 
private JdbcTemplate jdbcTemplate; 
@Override 
public List<Order> reload(){ 

} 
+0

我編輯我question.Sorry給了較少的信息 – Gulshan

回答

0

使用Spring數據公地

import org.springframework.data.domain.Page; 
import org.springframework.data.domain.PageRequest; 
import org.springframework.data.domain.Pageable; 
import org.springframework.data.repository.support.PageableExecutionUtils; 
import org.springframework.data.repository.support.PageableExecutionUtils.TotalSupplier; 

/** 
* @param content list from jdbcTemplate 
* @param pageNum : zero-based, if null default to zero 
* @param pageSize : if null default list size 
* @return Page object (zero-based) 
*/ 
public static <T> Page<T> pagingList(List<T> content, Integer pageNum, Integer pageSize){ 
    Assert.notNull(content, "List content must not be null!"); 
    pageNum = (pageNum == null) ? 0 : pageNum; 
    pageSize = (pageSize == null) ? content.size() : pageSize; 

    PageRequest pageable = new PageRequest(pageNum, pageSize); 

    //System.out.println(" ======== PAGEABLE ========= "); 
    //System.out.println(" pageable.getOffset  : " + pageable.getOffset()); 
    //System.out.println(" pageable.getPageNumber : " + pageable.getPageNumber()); 
    //System.out.println(" pageable.getPageSize : " + pageable.getPageSize()); 

    TotalSupplier totalSupplier = new TotalSupplier() { 

     @Override 
     public long get() { 
      return content.size(); 
     } 
    }; 

    int fromOffset = pageable.getOffset(); 
    int toOffset = Math.min(content.size(), fromOffset + pageSize); 
    List<T> listPaging = new ArrayList<>(); 
    if(fromOffset < toOffset){ 
     listPaging = content.subList(fromOffset, toOffset); 
    } 
    return PageableExecutionUtils.getPage(listPaging, pageable, totalSupplier); 
} 

以上代碼確實:

  • 從JdbcTemplate的
  • 接受列表創建PageRequest對象(頁次和pageSize的)
  • 創建從元素fromOffset和toOffset
原始清單開始子列表

注意對名單的龐大規模,還沒有測試它。

參考:Github spring-data-jpa