Spring boot 如何在spring boot中使用JdbcTemplate进行Pagerequest
我想在Spring boot 如何在spring boot中使用JdbcTemplate进行Pagerequest,spring-boot,jdbctemplate,Spring Boot,Jdbctemplate,我想在JdbcTemplate中做一个PageRequest。我不想做的是使用JPA。我搜索了将近4个小时,但找不到任何有用的东西。如果可能的话,请告诉我怎么做 编辑 我想从Oracle重新加载数据,但不想使用JPARepository 这是我的代码,这是我的控制器 @RestController @RequestMapping(value = "/admin") public class OrderController { @Autowired private OrderService or
JdbcTemplate
中做一个PageRequest
。我不想做的是使用JPA
。我搜索了将近4个小时,但找不到任何有用的东西。如果可能的话,请告诉我怎么做
编辑
我想从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();
}
@RestController
@请求映射(value=“/admin”)
公共类OrderController{
@自动连线
私人订单服务;
@RequestMapping(value=“/reload”,method=RequestMethod.GET)
公共列表重新加载(){
return orderService.reload();
}
这是我的服务
@Service
public class OrderService {
@Autowired
private OrderRepository orderRepository;
public List<Order> reload(){
return orderRepository.reload();
}
@服务
公共类订购服务{
@自动连线
专用医嘱存储库医嘱存储库;
公共列表重新加载(){
返回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(){
}
@存储库
公共类OrderRepositoryImpl实现OrderRepository{
@自动连线
私有JdbcTemplate JdbcTemplate;
@凌驾
公共列表重新加载(){
}
使用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);
}
import org.springframework.data.domain.Page;
导入org.springframework.data.domain.PageRequest;
导入org.springframework.data.domain.Pageable;
导入org.springframework.data.repository.support.pageableexecutions;
导入org.springframework.data.repository.support.pageableexecutionils.TotalSupplier;
/**
*来自jdbcTemplate的@param内容列表
*@param pageNum:zero-based,如果null默认为零
*@param pageSize:if null默认列表大小
*@返回页对象(从零开始)
*/
公共静态页面分页列表(列表内容、整数pageNum、整数pageSize){
Assert.notNull(内容,“列表内容不能为null!”);
pageNum=(pageNum==null)?0:pageNum;
pageSize=(pageSize==null)?content.size():pageSize;
PageRequest pageable=新的PageRequest(pageNum,pageSize);
//System.out.println(“=============可分页========”);
//System.out.println(“pageable.getOffset:+pageable.getOffset());
//System.out.println(“pageable.getPageNumber:+pageable.getPageNumber());
//System.out.println(“pageable.getPageSize:+pageable.getPageSize());
TotalSupplier TotalSupplier=新的TotalSupplier(){
@凌驾
公共长跑{
返回content.size();
}
};
int fromOffset=pageable.getOffset();
int toOffset=Math.min(content.size(),fromOffset+pageSize);
List listPaging=new ArrayList();
如果(从偏移量<到偏移量){
listPaging=content.subList(fromOffset,toOffset);
}
返回PageableExecutionUtils.getPage(listPaging,pageable,totalSupplier);
}
上述代码:
- 从jdbcTemplate接受列表
- 创建PageRequest对象(具有pageNum和pageSize)
- 从原始列表创建子列表从元素开始从偏移量和偏移量开始
参考:使用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);
}
import org.springframework.data.domain.Page;
导入org.springframework.data.domain.PageRequest;
导入org.springframework.data.domain.Pageable;
导入org.springframework.data.repository.support.pageableexecutions;
导入org.springframework.data.repository.support.pageableexecutionils.TotalSupplier;
/**
*来自jdbcTemplate的@param内容列表
*@param pageNum:zero-based,如果null默认为零
*@param pageSize:if null默认列表大小
*@返回页对象(从零开始)
*/
公共静态页面分页列表(列表内容、整数pageNum、整数pageSize){
Assert.notNull(内容,“列表内容不能为null!”);
pageNum=(pageNum==null)?0:pageNum;
pageSize=(pageSize==null)?content.size():pageSize;
PageRequest pageable=新的PageRequest(pageNum,pageSize);
//System.out.println(“=============可分页========”);
//System.out.println(“pageable.getOffset:+pageable.getOffset());
//System.out.println(“pageable.getPageNumber:+pageable.getPageNumber());
//System.out.println(“pageable.getPageSize:+pageable.getPageSize());
TotalSupplier TotalSupplier=新的TotalSupplier(){
@凌驾
公共长跑{
返回content.size();
}
};
int fromOffset=pageable.getOffset();
int toOffset=Math.min(content.size(),fromOffset+pageSize);
List listPaging=new ArrayList();
如果(从偏移量<到偏移量){
listPaging=content.subList(fromOffset,toOffset);
}
返回PageableExecutionUtils.getPage(listPaging,pageable,totalSupplier);
}
上述代码:
- 从jdbcTemplate接受列表
- 创建PageRequest对象(具有pageNum和pageSize)
- 从原始列表创建子列表从元素开始从偏移量和偏移量开始
参考资料:我编辑了我的问题。抱歉提供的信息较少。我编辑了我的问题。抱歉提供的信息较少