CRUD(13):查询-分页后台代码

一、功能详解

①访问index.jsp页面

②index.jsp页面发出查询员工列表请求

③EmployeeController接受请求,查出员工数量

④来到list.jsp页面进行展示

二、具体实现

①定义查询的 URI:/emps

②在index.jsp中配置:


<jsp:forward page="/emps"></jsp:forward>



③在src/main/webapp/WEB-INF/views下创建list.jsp文件。

④在com.amel.crud.service下创建EmployeeService类,查询所有员工。


@Service

public class EmployeeService {



       



       @Autowired



       EmployeeMapper employeeMapper;



       



       //查询所有员工



       public List<Employee> getAll() {







             return employeeMapper.selectByExampleWithDept(null);



       }







       



}



⑤引入PageHelper分页插件。

PageHelper官方文档

在pom.xml引入依赖包:


<dependency>

    <groupId>com.github.pagehelper</groupId>

    <artifactId>pagehelper</artifactId>

    <version>5.0.0</version>

</dependency>

这个包我也在之前引入过了。

在mybatis-config.xml中注册

④写相应的处理器

在com.amel.crud.controller包下创建EmployeeController类。


package com.amel.crud.controller;







import java.util.List;







import org.springframework.beans.factory.annotation.Autowired;



import org.springframework.stereotype.Controller;



import org.springframework.ui.Model;



import org.springframework.web.bind.annotation.RequestMapping;



import org.springframework.web.bind.annotation.RequestParam;







import com.amel.crud.bean.Employee;



import com.amel.crud.service.EmployeeService;



import com.github.pagehelper.PageHelper;



import com.github.pagehelper.PageInfo;







/**



 * 处理员工的CRUD请求



 * @author lenovo



 *



 */



@Controller



public class EmployeeController {



       



       @Autowired



       EmployeeService employeeService;



       /*



        * 显示员工数据(分页查询)



        */



       @RequestMapping("/emps")



       public String getEmps(@RequestParam(value="pn",defaultValue="1") Integer 

pn,Model model) {



             



             //引入pageHelper分页插件



             //传入页码和每页的大小



             PageHelper.startPage(pn, 5);



             //后面紧跟的查询就是分页查询



             List<Employee> emps = employeeService.getAll();



             //使用PageInfo包装查询后的结果,只需要将PageInfo交给页面



             //封装了详细的分页信息,包括我们查询出来的数据



             //连续传入5页



             PageInfo page = new PageInfo(emps,5);



             model.addAttribute("pageInfo",page);



             return "list";



       }



       



}



posted on 2021-02-16 00:00  northwest  阅读(52)  评论(0)    收藏  举报

导航