Advertisement

Layui+Springboot+Spring-Data-Jpa+PageRequest实现条件分页查询

阅读量:

在第一篇博文中阐述了Mybatis与PageHelper插件的应用,在此处将采用另一种orm技术来完成分页条件查询的任务。

复制代码
    #mysql
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/****?useUnicode=true&characterEncoding=UTF-8
    spring.datasource.username=root
    spring.datasource.password=root

在src/main/resources/application.properties文件配置完成后, 此项目即可启动. 在此基础上, 我们需要引入Spring-Data-Jpa相关的依赖项.

复制代码
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

当在前端执行查询操作时,默认情况下程序会传递一些必要的参数用于限定搜索范围和筛选条件。例如,在传递用户名字段的情况下,默认则会按照用户名来进行数据检索。例如,在查看实例时,默认情况下使用的便是Controller代码。

复制代码
    @ResponseBody
    @RequestMapping("/list")
    public Result<Staff> stafflist(Result<Staff> result,Staff example)
    {
        result.setExample(example);//设置前台条件,没有则为null
        staffservice.queryByPage(result);//调用service中的查询方法
        result.setCode("0");//设置状态码
        return result;
    }

Result被设计为整合所有返回数据的实体,并包含所需支持的字段信息。代码如下:

复制代码
    public class Result<T> implements Serializable {
    int page;//起始页
    int limit;//页数大小
    int count;//数据数量
    String code;//代码
    String msg;//信息
    List<T> data;//返回数据
    T example;//任何类型条件
    }

接下来是staffservice中的代码:

复制代码
    public Result<Staff> queryByPage(Result<Staff> result) {
        PageRequest request = new PageRequest(result.getPage() - 1, result.getLimit());//使用PageRequest 设置起始页与页数大小
        Example<Staff> ex = Example.of(result.getExample());//利用jpa中的方法将条件转为Example类型
        Page<Staff> all = staffdao.findAll(ex, request);//调用jpa中的实现方法返回数据
        result.setData(all.getContent());
        result.setCount((int) all.getTotalElements());
        return result;
    }

在我们的service层面可以看到调用findAll(ex, request),这是JPA为我们提供的功能。 DAO层的代码极为简单只需要继承JPA集合API就可以 因此,在编写代码时只需要继承JPA集合API即可

复制代码
    @Repository
    public interface StaffDao extends JpaRepository<Staff,Integer> {
    
    }

这些代码即为满足特定条件时的结果获取方案。
具体而言,
以下是基于Liyi前端框架实现的功能演示。

复制代码
    以上代码省略
      <table class="layui-hide" id="adminTable"></table>
      <script>
    layui.use('table', function () {
        var $ = layui.$;
        var table = layui.table, form = layui.form;
        table.render({
            elem: '#adminTable',
            url: '/staff/list',
            cellMinWidth: 80,
            cols: [
                [{
                    checkbox: true,
                    fixed: true
                }, {
                    field: 'staffId',
                    title: '员工编号',
                }, {
                    field: 'staffName',
                    title: '员工姓名',
                }, {
                    templet: '#switchTpl',
                    title: '员工性别',
                }, {
                    field: 'staffAge',
                    title: '员工年龄',
                }, {
                    field: 'staffCardId',
                    title: '身份证',
                }]
            ],
            id: 'reloadId',
            page: true
        });
        var active = {
            reload: function () {
                var s_name = $('#s_name');
                table.reload('reloadId', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    },
                    where: {
                        staffName: s_name.val(),
                    }
                });
            }
            };
          </script>

在上一篇文章中提到了使用PageHelper插件,在这种情况下两种插件的应用方法确实存在差异。其中Mybatis采用了其逆向出的方法来完成条件查询操作;而Spring-Data-Jpa则基于JPA框架帮我们实现了相应的类来处理条件查询功能。感谢各位专家赐教!

全部评论 (0)

还没有任何评论哟~