tlias管理系统--部门管理--分页查询实现

This commit is contained in:
2025-11-16 15:09:39 +08:00
parent b2f59aa56c
commit a38443bd2e
6 changed files with 112 additions and 0 deletions

View File

@@ -62,6 +62,9 @@
<source>11</source>
<target>11</target>
<encoding>UTF-8</encoding>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>

View File

@@ -1,7 +1,38 @@
package com.inmind.controller;
import com.inmind.pojo.PageBean;
import com.inmind.pojo.Result;
import com.inmind.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/emps")
@RestController
@Slf4j
public class EmpController {
@Autowired
private EmpService empService;
@GetMapping
public Result page(
@RequestParam(defaultValue = "1") Integer page
,@RequestParam(defaultValue = "10") Integer pageSize){
/*if (page == null) {
page = 1;
}
if (pageSize == null) {
pageSize = 10;
}*/
log.info("分页查询:{}{}",page,pageSize);
//调用员工业务层的分页查询功能
PageBean pageBean = empService.page(page,pageSize);
return Result.success(pageBean);
}
}

View File

@@ -1,7 +1,21 @@
package com.inmind.mapper;
import com.inmind.pojo.Emp;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface EmpMapper {
/**
* 查询总记录数
* @return
*/
@Select("select count(*) from emp")
Long getTotal();
@Select("select * from emp limit #{startIndex},#{pageSize}")
List<Emp> getPageList(int startIndex,int pageSize);
}

View File

@@ -0,0 +1,20 @@
package com.inmind.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
/**
* 分页查询结果封装类
* 分页的总记录数
* 当前页的数据列表数据
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageBean {
private Long total;
private List rows;
}

View File

@@ -1,4 +1,13 @@
package com.inmind.service;
import com.inmind.pojo.PageBean;
public interface EmpService {
/**
* 分页查询
* @param page
* @param pageSize
* @return
*/
PageBean page(Integer page, Integer pageSize);
}

View File

@@ -1,8 +1,43 @@
package com.inmind.service.impl;
import com.inmind.mapper.EmpMapper;
import com.inmind.pojo.Emp;
import com.inmind.pojo.PageBean;
import com.inmind.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class EmpServiceImpl implements EmpService {
@Autowired
private EmpMapper empMapper;
/**
* 分页查询
* @param page 页码
* @param pageSize 每页显示条数
* @return
*/
@Override
public PageBean page(Integer page, Integer pageSize) {
//1.获取总记录数
Long total = empMapper.getTotal();
//2.获取当前页的数据列表
/*
-- 分页查询 select * from emp limit 参数1 参数2
-- 参数1起始索引 就是(页码-1*每页显示的记录数
-- 参数2查询返回的记录数 就是每页显示的记录数
*/
int startIndex = (page - 1)* pageSize;
List<Emp> rows = empMapper.getPageList(startIndex,pageSize);
//3.封装成分页查询结果,并返回
/*PageBean pageBean = new PageBean();
pageBean.setRows(rows);
pageBean.setTotal(total);
return pageBean;*/
return new PageBean(total, rows);
}
}