java 后端使用简单的for循环实现后端分页功能

下面是使用java 后端的for循环实现的简单的分页,方法仅供参考,具体逻辑需要实际应用去做调整和修改,亲测改方式可行,需要根据具体的逻辑调整后实现。

代码只是核心的参考实例

pageParam是前段传的对应的分页信息,包含页数,每页显示条数等信息,在方法里面获取数据后按照前段参数进行循环截取后再return到前台。优点是实现简单,代码比较少,不需要借助其他包和工具,缺点是需要每次都要将数据循环一次,将指定页数和显示数据条数截取返回。

 

/**

* 使用for循环实现后端分页

* @param list 数据集合

* @param page 当前页数

* @param num 每页条数

* @return

*/

public Page<info> test(Map<String, Integer> pageParam){

//sql查询返回的数据

List<Object[]> result=pPolicyinsureRepository.test();

// obj转对象

List<info> list = objToDo(result);

 

// 若分页参数不为空,则绑定分页参数

//当前页码

int pageNum = pageParam.get(PAGE_NUM);

//查询的条数

int pageSize = pageParam.get(PAGESIZE);

// 总条数

int len =list.size();

// 总页数

int count = len%pageSize==0?(len/pageSize):(len/pageSize)+1;

for(int i=0;i <= count;i++){

int fromIndex = i*pageSize;

int toIndex=Math.min(len, (i+1)*pageSize);

if(pageNum == i+1 ){

List<info> subList=list.subList(fromIndex, toIndex);

Pageable pageable = new PageRequest(pageNum, pageSize);

Page<info> pdrPage = new PageImpl<>(subList, pageable, len);

return pdrPage;

}

if(toIndex >= len){

break;

}

}

return null;

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
前端 Vue.js 代码: ``` <template> <div> <table> <thead> <tr> <th>ID</th> <th>Name</th> <th>Price</th> </tr> </thead> <tbody> <tr v-for="product in products" :key="product.id"> <td>{{ product.id }}</td> <td>{{ product.name }}</td> <td>{{ product.price }}</td> </tr> </tbody> </table> <el-pagination v-if="total > 0" :page-size="pageSize" :total="total" :current-page="currentPage" @current-change="handlePageChange" /> </div> </template> <script> import axios from 'axios' export default { data() { return { products: [], currentPage: 1, pageSize: 10, total: 0 } }, mounted() { this.getProducts() }, methods: { getProducts() { axios.get('/api/products', { params: { page: this.currentPage, pageSize: this.pageSize } }).then(res => { this.products = res.data.list this.total = res.data.total }) }, handlePageChange(page) { this.currentPage = page this.getProducts() } } } </script> ``` 后端 Java Spring Boot 代码: ``` @RestController @RequestMapping("/api") public class ProductController { @Autowired private ProductService productService; @GetMapping("/products") public PageResult<Product> getProducts(@RequestParam Integer page, @RequestParam Integer pageSize) { return productService.getProducts(page, pageSize); } } ``` ``` @Service public class ProductServiceImpl implements ProductService { @Autowired private ProductRepository productRepository; @Override public PageResult<Product> getProducts(Integer page, Integer pageSize) { Sort sort = Sort.by(Sort.Direction.DESC, "id"); Pageable pageable = PageRequest.of(page - 1, pageSize, sort); Page<Product> productPage = productRepository.findAll(pageable); List<Product> productList = productPage.getContent(); long total = productPage.getTotalElements(); return new PageResult<>(productList, total); } } ``` ``` public interface ProductService { PageResult<Product> getProducts(Integer page, Integer pageSize); } ``` ``` public interface ProductRepository extends JpaRepository<Product, Long> { } ``` 以上是一个简单的Vue.js + Java Spring Boot实现分页功能的示例,具体实现还需要根据业务需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值