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

在第一篇博文中介绍了Mybatis与PageHelper插件的使用,在本文中,将会使用另一种orm来实现分页条件查询。
使用Springboot最大的好处就是减少繁琐的配置,Spring-Data-Jpa是基于hibernate框架做的,因此数据源的配置必不可少。但是!但是!配置文件只需如下代码:

#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下配置完毕后该boot项目即可运行。
接下来我们引入Spring-Data-Jpa的依赖:

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-jpaartifactId>
        dependency>

在前台进行查询的时候,会传一些条件,比如传来用户名字段,则是要求按用户名查询,看个例子:此为controller代码

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

Result为封装所有返回数据的实体,包含layui框架所需要的字段,代码如下:

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

接下来是staffservice中的代码:

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

在service层中可以看到,调用的findAll(ex, request)是jpa帮我们实现的,dao层代码极为简单,只需继承JpaRepository即可,代码如下:

@Repository
public interface StaffDao extends JpaRepository<Staff,Integer> {

}

以上代码即可根据条件查询到想要的结果,接下来,是layui前端代码的展示:

以上代码省略
  <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帮我们实现的类进行条件查询。望广大高手指正。

你可能感兴趣的:(Layui+Springboot+Spring-Data-Jpa+PageRequest实现条件分页查询)