分页查询(简单实现)

简单分页查询的实现!

一、工具:

idea(2.4.1)、springboot(3.0.0+)、mybatis、thymeleaf

二、导入相关依赖:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>

三、 分页的服务层Service

@Service
public class FindService {
    @Autowired
    private PageMapper pageMapper;   //数据库查询Mapper接口
    public Page<User> getFindPage(int pageNo,int pageSize,String find) { //'find'为你要查询的字段,'pageNo'为查询到的相关数据的第几页
        pageNo= Math.min(pageNo, pageSize-1); //'pageNo','pageSize'为前端处理数据,防止无限自增,'pageZise-1'为查询到相关数据一页展示多少条
        int offset = (pageNo - 1) * pageSize; //'offset'表示从第几页开始查询
        int size = pageSize; 
        List<User> FindUser = pageMapper.findSomeBody(offset,size,find); //数据库查询
        int totalCount = pageMapper.countSomeBody(find); //数据库查询到相关数据的总数
        return new Page<>(FindUser, pageNo, pageSize, totalCount);
    }
}

四、分页的数据查询Mapper接口

@Mapper
public interface PageMapper {
        @Select("SELECT * FROM “你要查询的表名” LIMIT #{offset}, #{size}")
        public List<User> selectPage(@Param("offset") int offset, @Param("size") int size);
        @Select("SELECT COUNT(*) FROM “你要查询的表名” ")
        public int countAll();
        @Select("select * from “你要查询的表名” where find like concat('%',#{dind},'%') limit #{offset},#{size}")
        public List<User> findSomeBody(@Param("offset") int offset, @Param("size") int size, String find);
        @Select("select count(id) from “你要查询的表名” where find like concat('%',#{find},'%')")
        public int countSomeBody(String find);
    }

五、分页的控制层Controller 

@Controller
public class FindController {
    @Autowired
    private PageMapper pageMapper; //上述接口
    @Autowired
    private FindService findService;  //上述接口
    @RequestMapping("/Find")   //访问该方法的路径为'Find'
    public String find(Model model, @RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "5") int pageSize,
                       HttpServletRequest request) {
        String find = "";
        if(request.getParameter("cx")!=null){
            find=request.getParameter("cx");
        }  //判断字段为空?
        int i = pageMapper.countSomeBody(find); //查询'find'字段的条数
        if(i==0){
            model.addAttribute("title", Result.error1("没有查询到有关\""+起始地点+"\"的信息"));
            return "tishi"; 
        }  //判断i为空?
        pageNo=pageNo-1<0 ? 1:pageNo; //'pageNo'为前端处理数据,防止无限自减
        Page<User> page = findService.getFindPage(pageNo, pageSize, find); //调用方法
        if (page.getContent().size() == 0) { //由于开始默认pageNo,查询结果条数少而导致有些页面可能为空
            Page<User> page1 = findService.getFindPage(pageNo-1, pageSize, find);
            model.addAttribute("entities", page1.getContent()); //查询到的内容
            model.addAttribute("pageNo", Math.min(page1.getPageNo(), pageNo - 1));//与上述的三目运算符有异曲同工之妙
            model.addAttribute("pageSize", page1.getPageSize()); //一页多少条数据
            model.addAttribute("totalCount", page1.getTotalCount()); //查询到的总数据
            model.addAttribute("value",find); //模糊查询的值
            model.addAttribute("opt","/Find"); //对模糊查询后数据进行分页
            return "display";
        }else {
            model.addAttribute("entities", page.getContent());
            model.addAttribute("pageNo", page.getPageNo());
            model.addAttribute("pageSize", page.getPageSize());
            model.addAttribute("totalCount", page.getTotalCount());
            model.addAttribute("value",find);
            model.addAttribute("opt","/Find");
            return "display";
        }
    }
}

六、前端页面

<!doctype html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<meta content="text/html;charset=UTF-8">
<head>
    <title>display</title>
    <meta name="viewport" content="width=device-width,initial-scale=1"/>
</head>
<body th:id="ss">
<div th:align="center"><B>分页</B></div>
<form th:align="center" th:action="@{/Find}" th:method="post">
    <input th:name="cx"  type="text" placeholder="查询" required="" autofocus="" th:value="${value}">
    <button type="submit">查询</button>
</form>
<table th:border="1px" th:cellspacing="0" width="99%" th:align="center">
    <tr>
        <td th:align="center">age</td>
        <td th:align="center">time</td>
        <td th:align="center">name</td>
        <td th:align="center">id</td>
        <td th:align="center">find</td>
    </tr>
    <tr th:each="user:${entities}">
        <td th:id="cl" th:align="center" th:text="${user.getAge()}"></td>
        <td th:align="center" th:text="${user.getTime()}"></td>
        <td th:align="center" th:text="${user.getName()}"></td>
        <td th:align="center" th:text="${user.getId()}"></td>
        <td th:align="center" th:text="${user.getFind()}"></td>
        <caption th:align="center" th:if="${totalCount > 0}">
            <button style="background-color: #b3d7ff"><a th:href="@{${opt}(cx=${value},pageNo=${pageNo}-1,pageSize=${pageSize})}">上一页</a></button>
            <button style="background-color: #b3d7ff"><a th:href="@{${opt}(cx=${value},pageNo=${pageNo}+1,pageSize=${pageSize})}">下一页</a></button>
            <span th:text="'第' + ${pageNo} + '页, 总计' + ${totalCount} + '条记录'"></span>
            <div style="color: black"><hr></div>
        </caption>
</table>
</body>
</html>
    

 文章到此结束!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值