Spring MVC结合Spring Data JPA实现按条件查询和分页

时间:2022-05-02 03:34:57

本文实例为大家分享了android九宫格图片展示的具体代码,供大家参考,具体内容如下

推荐视频:尚硅谷spring data jpa视频教程,一学就会,百度一下就有。

后台代码:在dao层继承spring data jpa的pagingandsortingrepository接口实现的 (实现方法主要在sbglserviceimpl.java类中)

前台表现:用kkpaper表现出来

实现效果:

Spring MVC结合Spring Data JPA实现按条件查询和分页

1、实体类

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.jinhetech.yogurt.sbgl.entity;
 
import java.io.serializable;
 
import javax.persistence.entity;
import javax.persistence.generatedvalue;
import javax.persistence.id;
import javax.persistence.primarykeyjoincolumn;
import javax.persistence.sequencegenerator;
import javax.persistence.table;
 
import org.hibernate.annotations.genericgenerator;
import org.springframework.cache.annotation.cacheable;
@entity
@table(name="sys_sbgl")
@primarykeyjoincolumn(name = "sbbm")
@cacheable(value = "basecache")
public class sbgl implements serializable{
 
 
  private static final long serialversionuid = -1423899016746755470l;
 
  @id
  private string sbbm;
  
  private string sbmc;
  private string sblx;
  private string sssx;
  private string ssjd;
  
  private string azsj;
  private string azry;
  private string sbzt;
  private string sbjd;
  private string sbwd;
  
  private string wxlxr;
  private string wxlxdh;
  private string sbywxcs;
  private string jzpylyz;
  private string mqsbcyr;
  private string bzsm;
  
    
  //setter、getter方法省略
  
}

2、jsp页面,看最下面的分页组件(kkpaper)

resultmap集合是下面controller中最后查询和分页后获得的所有数据信息,resultmap.resultlist:resultlist是map集合的属性,里面存着<key,value>数据

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!doctype html>
 
<c:set var="ctx" value="${pagecontext.request.contextpath}" scope="session"/>
 
<html>
<head>
  <title>设备管理</title>
  <!-- ztree需要的样式和js -->
  <link rel="stylesheet" href="${ctx }/yogurtresource/assembly/ztree/css/ztreestyle.css" rel="external nofollow" type="text/css">
  <script type="text/javascript" src="${ctx }/yogurtresource/assembly/ztree/jquery.ztree.core-3.5.js"></script>
  <script type="text/javascript" src="${ctx }/yogurtresource/assembly/ztree/jquery.ztree.excheck-3.5.js"></script>
  
  <!-- 验证框架 -->
  <script type="text/javascript" src="${ctx }/yogurtresource/assembly/yogurt/user/formvalidate.js"></script>
  
  <!-- 修改表单验证错误提示样式 -->
  <style type="text/css">
    .table-bordered-ul li{ margin:0 5px; float:left}
    label.error {
     padding-left: 16px;
    
     padding-bottom: 2px;
    
     font-weight: bold;
    
     color: #f00;
    }
  </style>
  
  <script type="text/javascript">
  function console(consoletag, userid, statetag) {
      // 添加用户。
    if("add" == consoletag) {
      $('#formid').prop("action", "${ctx}/sbgl/sbgl_add_list");
    }
 
  }
        
  /**
   * 搜索。
   */
  function search() {
    $('#searchform').submit();
  }     
  
  </script>
  
</head>
 
<body>
  <div class="clear">
    <div class="panel panel-default yogurt_route" id="yogurt_route">
      <a href="${ctx }/home/home_list" rel="external nofollow" >首页</a> > <a
        href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="navigate();">设备管理</a> > 设备列表
    </div>
  </div>
  
  <ul id="bqnum">
    <li><a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="navigate();">导航</a>
    </li>
    <li><a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="orgtree();">机构</a>
    </li>
  </ul>
  
  <!-- 根据条件查询 -->
  <div class="clear ppd10" style="line-height:32px">
    <form action="${ctx }/sbgl/sbgl_list" method="post" id="searchform">
      <div style="float:left; width:200px">
        设备名称:
        <input type="text" name="sersbmc" id="sersbmc" >
      </div>
      <div class="floatleft">
          设备类型:
        <input type="text" name="sersblx" id="sersblx" >
      </div>
      <div class="floatleft">
          所属市县:
        <input type="text" name="sersssx" id="sersssx" >
      </div>
      <div class="floatleft">
          基地名称:
        <input type="text" name="serjdmc" id="serjdmc" >
      </div>
      <div class="floatleft">
          设备状态:
        <select class="form-control" style="display:inline-block; width:80px" name="sersbzt" id="sersbzt">
          <option value="">全部</option>
          <option value="y">正常</option>
          <option value="n">异常</option>
        </select>
      </div>    
      <button type="submit" class="btn btn-success yogurt_search_button" onclick="search();">
        <img src="${ctx }/yogurtresource/images/panel/search.png" width="18" height="18" />
      </button>
    </form>
  </div>
  
  <!-- 新增、删除 -->
  <div class="panel panel-default" style="margin-top:10px">
    <div class="yogurt_panel_title_core">
      <span class="state_bold">共查询出数据结果:<span class="state_red">${resultmap.totalnum}</span>条</span>
    </div>
    <div class="more_core3">
      <!-- <button type="button" class="btn btn-success yogurt_search_button2" onclick="console('add','');">新增设备</button> -->
      <button type="button" class="btn btn-success yogurt_search_button2" onclick="window.location.href='${ctx }/sbgl/sbgl_add_list' ">新增设备</button>
      <button type="button" class="btn btn-success yogurt_search_button2" onclick="console('delete','');">删除设备</button>
    </div>
  </div>
  <!-- 列表 -->
  <div class="yogurt_elementsbox_core clear">
    <div class="yogurt_elementsbox_table_article" id="yogurt_elementsbox_table_article">
      <table id="tablebody" border="0" cellspacing="0" cellpadding="0" class="table yogurt_elementsbox_table">
        <thead>
          <tr class="yogurt_tab_back" onmouseover="this.classname='yogurt_tab_trhover'" onmouseout="this.classname='yogurt_tab_back'">
            <th width="3%"><input type="checkbox" onclick="allchecked(this)"></th>
            <th width="10%">设备名称</th>
            <th width="10%">设备类型</th>
            <th width="10%">所属市县</th>
            <th width="10%">基地名称</th>
            <th width="10%">设备状态</th>
            <th width="10%">维修联系人</th>
            <th width="10%">联系电话</th>
            <th width="10%">设备持有人</th>
            <th width="17%" style="text-align:center;">操作</th>
          </tr>
        </thead>
        <tbody id="usertb">
          <c:foreach var="sbgl" items="${resultmap.resultlist}" varstatus="status">
            <tr>
              <td><input type="checkbox" ></td>
              <td>${sbgl.sbmc }</td>
              <td>${sbgl.sblx }</td>
              <td>${sbgl.sssx }</td>
              <td>${sbgl.ssjd }</td>
              <td>${sbgl.sbzt }</td>
              <td>${sbgl.wxlxr }</td>
              <td>${sbgl.wxlxdh }</td>
              <td>${sbgl.mqsbcyr }</td>
              <td style="text-align:center">
                <a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="console('update', '${user.userid}')"> 修改</a>
                <a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="showroles('${user.userid}')">| 查看</a>
              </td>
            </tr>
          </c:foreach>
        </tbody>
      
      </table>
      
      
    </div>
  </div>
  
  
    <!--分页-->
  <div style="clear:both; height:50px">
    <!--加载分页组件-->
    <div id="kkpager"></div>
    <script type="text/javascript">
      var param = "";
 
      $(function() {
        var totalpage = "${resultmap.totalpage}";
        var totalrecords = "${resultmap.totalnum}";
        var pagesize = "${resultmap.pagesize}";
 
        var pagenum = parseint("${resultmap.pagenum}") + 1;
        //初始化分页控件
        //有些参数是可选的,比如lang,若不传有默认值
        kkpager.init({
          pno: pagenum,
          //总页码
          total: "${resultmap.totalpage}",
          //总数据条数
          totalrecords: totalrecords,
          //链接前部
          hrefformer: 'sbgl_list',
          //链接尾部
          hreflatter: '',
          getlink: function(n) {
            return getinitparam() + "&pagenum=" + n + "&pagesize=" + pagesize;
          },
          lang: {
            prepagetext: '上一页',
            nextpagetext: '下一页',
            totalpagebeforetext: '共',
            totalpageaftertext: '页',
            totalrecordsaftertext: '条数据',
            gopagebeforetext: '转到',
            gopagebuttonoktext: '确定',
            gopageaftertext: '页',
            buttontipbeforetext: '第',
            buttontipaftertext: '页'
          }
        });
        //生成
        kkpager.generpagehtml();
 
        $('#mykkpagerselect').val(pagesize);
      });
 
      function returnoption(pagesize) {
        window.location.href = getinitparam() + "&pagenum=1&pagesize=" + pagesize;
      }
 
      function getinitparam() {
        var sersbmc = $('#sersbmc').val();
        var sersblx = $('#sersblx').val();
        var sersssx = $('#sersssx').val();
        var serjdmc = $('#serjdmc').val();
        var sersbzt = $('#sersbzt').val();
 
        var attr = "?sersbmc=" + encodeuri(encodeuri(sersbmc))
            + "&sersblx=" + sersblx + "&sersssx=" + sersssx + "&serjdmc=" + serjdmc+ "&sersbzt=" + sersbzt;
        return "${ctx}/sbgl/sbgl_list" + attr;
      }
    </script>
    <!--分页结束-->
  </div>
  <!--分页end-->
 
</body>
</html>

3、controller(看红色字体下面那个处理方法)

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.jinhetech.yogurt.sbgl.controller;
 
import java.util.arraylist;
import java.util.hashmap;
import java.util.linkedhashset;
import java.util.list;
import java.util.map;
import java.util.set;
 
import javax.annotation.resource;
 
import org.apache.shiro.authz.annotation.requirespermissions;
import org.apache.shiro.authz.annotation.requiresroles;
 
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.controller;
import org.springframework.ui.model;
import org.springframework.web.bind.annotation.modelattribute;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestmethod;
import org.springframework.web.bind.annotation.requestparam;
import org.springframework.web.bind.annotation.responsebody;
import org.springframework.web.servlet.modelandview;
import org.springframework.web.servlet.mvc.support.redirectattributes;
import org.springframework.web.servlet.view.redirectview;
 
import com.jinhetech.yogurt.dictionary.crop.service.impl.cropserviceimpl;
import com.jinhetech.yogurt.func.entity.func;
import com.jinhetech.yogurt.func.service.funcservice;
import com.jinhetech.yogurt.menu.entity.menu;
import com.jinhetech.yogurt.menu.service.menuservice;
import com.jinhetech.yogurt.menu.util.menuutil;
import com.jinhetech.yogurt.organization.entity.orgtable;
import com.jinhetech.yogurt.organization.service.orgservice;
import com.jinhetech.yogurt.organization.util.orgutil;
import com.jinhetech.yogurt.role.entity.role;
import com.jinhetech.yogurt.role.service.roleservice;
import com.jinhetech.yogurt.sbgl.dao.sbgldao;
import com.jinhetech.yogurt.sbgl.entity.sbgl;
import com.jinhetech.yogurt.sbgl.service.sbglservice;
import com.jinhetech.yogurt.sbgl.util.sbglutil;
import com.jinhetech.yogurt.user.entity.user;
import com.jinhetech.yogurt.user.entity.userinfo;
import com.jinhetech.yogurt.user.service.userservice;
import com.jinhetech.yogurt.user.util.userutil;
import com.jinhetech.yogurt.util.base.basecontroller;
import com.jinhetech.yogurt.util.base.constants;
import com.jinhetech.yogurt.util.common.textutils;
import com.jinhetech.yogurt.util.common.uuidhexgenerator;
 
/**
 * 系统用户管理控制类
 *
 * @author wang hao
 * @version 1.0 2014-02-28 初版
 */
@controller("sbglcontroller")
@requestmapping("/sbgl")
public class sbglcontroller extends basecontroller {
//  @resource(name = "sbgldao")
//  private sbgldao sbgldao;
  
  @resource(name = "sbglservice")
  private sbglservice sbglservice;
  
  /**
   * 平台权限管理导航*/
  @requestmapping("/home_list")
  public modelandview home() {
 
    return new modelandview("iot/permis/permis_home");
  }
  
  
  /**
   * 查询设备信息列表(支持分页和多条件查询)。
   *
   */
  @requestmapping("sbgl_list")
  @requiresroles("sbgl/sbgl_list")
  public string getuserlist(model model) throws exception {
    //显示设备列表
//    list<sbgl> lst=new arraylist<sbgl>();
//    lst=(list<sbgl>) sbglservice.getall();
    
    map<string, object> resultmap = null;
    // 查询表单或分页保持请求时 请求参数的接收
    map<string, string> serargs = new hashmap<string, string>();
    serargs = sbglutil.getselargstomap(request);//这个类在下面给出
    
 
    resultmap = sbglservice.getuserbysearch(serargs, "wxlxdh");
    
    model.addattribute("resultmap", resultmap);
//    model.addattribute("lst", lst);
 
    return "sbgl/sbgl_list";
  }
 
  /**
   * 新增设备信息列表(支持分页和多条件查询)。
   *
   * @author yangzhenghua 2014-5-28
   * @throws exception
   */
  @requestmapping("sbgl_add_list")
  @requiresroles("sbgl/sbgl_add_list")
  public string getaddlist(model model) throws exception {
 
    
    system.out.println("aaa");
    model.addattribute("aaa","aaa");
    model.addattribute("resultmap", "hello world");
    
 
    return "sbgl/sbgl_add_list";
  }
  
  
  /**
   * 保存、修改用户信息。
   *
   * @author yangzhenghua 2014-5-28
   * @throws exception
   */
  @requestmapping("sbgl_save_list")
  @requiresroles("sbgl/sbgl_save_list")
  public modelandview savesbgl(sbgl sbgl) throws exception {
 
    string sbmc=request.getparameter("sbmc");
    string sblx=request.getparameter("sblx");
    string sssx=request.getparameter("sssx");
    string ssjd=request.getparameter("ssjd");
    string azsj=request.getparameter("azsj");
    
    string azry=request.getparameter("azry");
    string sbzt=request.getparameter("sbzt");
    string sbjd=request.getparameter("sbjd");
    string sbwd=request.getparameter("sbwd");
    string wxlxr=request.getparameter("wxlxr");
    
    string wxlxdh=request.getparameter("wxlxdh");
    string sbywxcs=request.getparameter("sbywxcs");
    string jzpylyz=request.getparameter("jzpylyz");
    string mqsbcyr=request.getparameter("mqsbcyr");
    string bzsm=request.getparameter("bzsm");
    
    sbgl.setsbbm(uuidhexgenerator.generate());
    sbgl.setsbmc(sbmc);
    sbgl.setsblx(sblx);
    sbgl.setsssx(sssx);
    sbgl.setssjd(ssjd);
    sbgl.setazsj(azsj);
    
    sbgl.setazry(azry);
    sbgl.setsbzt(sbzt);
    sbgl.setsbjd(sbjd);
    sbgl.setsbwd(sbwd);
    sbgl.setwxlxr(wxlxr);
    
    sbgl.setwxlxdh(wxlxdh);
    sbgl.setsbywxcs(sbywxcs);
    sbgl.setjzpylyz(jzpylyz);
    sbgl.setmqsbcyr(mqsbcyr);
    sbgl.setbzsm(bzsm);
 
    sbglservice.save(sbgl);
 
    return new modelandview(new redirectview("sbgl_list"));
  }
 
 
}

3.2、sbglutil.java

封装从前台传递过来的查询参数、前台传递过来的分页参数,都放到map集合serargs中

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.jinhetech.yogurt.sbgl.util;
 
import java.util.arraylist;
import java.util.hashmap;
import java.util.list;
import java.util.map;
import java.util.map.entry;
 
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpsession;
 
import org.springframework.web.context.request.requestcontextholder;
import org.springframework.web.context.request.servletrequestattributes;
 
import com.jinhetech.yogurt.organization.entity.orgtable;
import com.jinhetech.yogurt.report.entity.report;
import com.jinhetech.yogurt.role.entity.role;
import com.jinhetech.yogurt.user.entity.user;
import com.jinhetech.yogurt.user.entity.userinfo;
import com.jinhetech.yogurt.util.common.jsonutils;
import com.jinhetech.yogurt.util.common.textutils;
 
/**
 * 用户功能模块工具类。
 *
 * @author yangzhenghua
 * @version v1.0 2014-5-16 初版
 *
 */
public class sbglutil {
 
  /**
   * 封装从前台传递过来的查询参数。
   *
   * @author yangzhenghua
   * @date 2014-6-26
   */
  public static map<string, string> getselargstomap(httpservletrequest request) throws exception {
    map<string, string> serargs = new hashmap<string, string>();
 
    string sersbmc = request.getparameter("sersbmc");
    string sersblx = request.getparameter("sersblx");
    string sersssx = request.getparameter("sersssx");
    string serjdmc = request.getparameter("serjdmc");
    string sersbzt = request.getparameter("sersbzt");
    
    string pagenum = request.getparameter("pagenum") == null ? "1" : request.getparameter("pagenum");
    string pagesize = request.getparameter("pagesize") == null ? "10" : request.getparameter("pagesize");
 
 
    //serargs.put("serusername", java.net.urldecoder.decode(serusername == null ? "" : serusername, "utf-8"));
    serargs.put("sersbmc", sersbmc);
    serargs.put("sersblx", sersblx);
    serargs.put("sersssx", sersssx);
    serargs.put("serjdmc", serjdmc);
    serargs.put("sersbzt", sersbzt);
    
    serargs.put("pagenum", pagenum);
    serargs.put("pagesize", pagesize);
 
    return serargs;
  }
 
 
 
}

3.3、sbglservice.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.jinhetech.yogurt.sbgl.service;
 
import java.util.list;
import java.util.map;
 
import org.springframework.stereotype.service;
import org.springframework.transaction.annotation.transactional;
 
import com.jinhetech.yogurt.sbgl.entity.sbgl;
 
 
public interface sbglservice {
 
  public list<sbgl> getall() throws exception;
  
  public sbgl save(sbgl sbgl) throws exception;
  
  public map<string, object> getuserbysearch(map<string, string> serargs, final string sorttype) throws exception;
  
}

3.4、sbglserviceimpl.java (根据前台传递来的查询参数进行查询,获得的结果数据放到page objpage参数)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.jinhetech.yogurt.sbgl.service.impl;
 
import java.util.arraylist;
import java.util.list;
import java.util.map;
 
import javax.annotation.resource;
import javax.persistence.criteria.criteriabuilder;
import javax.persistence.criteria.criteriaquery;
import javax.persistence.criteria.jointype;
import javax.persistence.criteria.listjoin;
import javax.persistence.criteria.predicate;
import javax.persistence.criteria.root;
 
import org.springframework.data.domain.page;
import org.springframework.data.domain.pageable;
import org.springframework.data.jpa.domain.specification;
import org.springframework.stereotype.service;
import org.springframework.transaction.annotation.transactional;
 
import com.jinhetech.yogurt.role.entity.role;
import com.jinhetech.yogurt.sbgl.dao.sbgldao;
import com.jinhetech.yogurt.sbgl.entity.sbgl;
import com.jinhetech.yogurt.sbgl.service.sbglservice;
import com.jinhetech.yogurt.user.entity.userinfo;
import com.jinhetech.yogurt.util.common.pageutils;
import com.jinhetech.yogurt.util.common.textutils;
 
@service("sbglservice")
@transactional
public class sbglserviceimpl implements sbglservice{
 
  @resource(name = "sbgldao")
  private sbgldao sbgldao;
  
  
  public list<sbgl> getall() throws exception{
    
    return (list<sbgl>) this.sbgldao.findall();
  }
 
  public sbgl save(sbgl sbgl) throws exception {
    return sbgldao.save(sbgl);
  }
  
  
  /**
   * 查询用户信息列表(支持分页和多条件查询)。
   *
   * @author yangzhenghua 2014-6-19
   */
  public map<string, object> getuserbysearch(final map<string, string> serargs, final string sorttype) throws exception {
 
    // 获得分页对象pageable,并且在pageable中页码是从0开始,设定按照sorttype升序排列
    pageable pageable = pageutils.buildpagerequest(integer.valueof(serargs.get("pagenum")),
        integer.valueof(serargs.get("pagesize")), sorttype);
 
    page<sbgl> objpage = sbgldao.findall(new specification<sbgl>() {
 
      public predicate topredicate(root<sbgl> root, criteriaquery<?> query, criteriabuilder cb) {
        list<predicate> lstpredicates = new arraylist<predicate>();
 
        if (textutils.isnotblank(serargs.get("sersbmc"))) {
          lstpredicates.add(cb.like(root.get("sbmc").as(string.class), "%" + serargs.get("sersbmc") + "%"));
        }
        if (textutils.isnotblank(serargs.get("sersblx"))) {
          lstpredicates.add(cb.like(root.get("sblx").as(string.class), "%" + serargs.get("sersblx") + "%"));
        }
        if (textutils.isnotblank(serargs.get("sersssx"))) {
          lstpredicates.add(cb.like(root.get("sssx").as(string.class), "%" + serargs.get("sersssx") + "%"));
        }
        if (textutils.isnotblank(serargs.get("serjdmc"))) {
          lstpredicates.add(cb.like(root.get("jdmc").as(string.class), "%" + serargs.get("serjdmc") + "%"));
        }
 
        if (textutils.isnotblank(serargs.get("sersbzt"))) {
          lstpredicates.add(cb.equal(root.get("sbzt"), serargs.get("sersbzt")));
        }
 
        predicate[] arraypredicates = new predicate[lstpredicates.size()];
        return cb.and(lstpredicates.toarray(arraypredicates));
      }
    }, pageable);
 
    return pageutils.getpagemap(objpage);
  }
  
  
}

3.4.1、pageutils.java(分页数据工具类)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.jinhetech.yogurt.util.common;
 
import java.util.hashmap;
import java.util.map;
 
import org.springframework.data.domain.page;
import org.springframework.data.domain.pagerequest;
import org.springframework.data.domain.sort;
import org.springframework.data.domain.sort.direction;
 
import com.jinhetech.yogurt.util.base.constants;
 
/**
 * 分页数据工具类。
 *
 * @author yangzhenghua
 * @version v1.0 2014-6-24 初版
 *
 */
public class pageutils {
 
  /**
   * 封装分页数据到map中。
   */
  public static map<string, object> getpagemap(page<?> objpage) {
    map<string, object> resultmap = new hashmap<string, object>();
 
    resultmap.put(constants.page_result_list, objpage.getcontent()); // 数据集合
    resultmap.put(constants.page_total_num, objpage.gettotalelements()); // 总记录数
    resultmap.put(constants.page_total_page, objpage.gettotalpages()); // 总页数
    resultmap.put(constants.page_num, objpage.getnumber()); // 当前页码
    resultmap.put(constants.page_size, objpage.getsize()); // 每页显示数量
 
    return resultmap;
  }
 
  /**
   * 创建分页请求。
   *
   * @author yangzhenghua
   * @date 2014-7-14
   *
   * @param pagenum 当前页
   * @param pagesize 每页条数
   * @param sorttype 排序字段
   * @param direction 排序方向
   */
  public static pagerequest buildpagerequest(int pagenum, int pagesize, string sorttype, string direction) {
    sort sort = null;
 
    if (!textutils.isnotblank(sorttype)) {
      return new pagerequest(pagenum - 1, pagesize);
    } else if (textutils.isnotblank(direction)) {
      if (direction.asc.equals(direction)) {
        sort = new sort(direction.asc, sorttype);
      } else {
        sort = new sort(direction.desc, sorttype);
      }
      return new pagerequest(pagenum - 1, pagesize, sort);
    } else {
      sort = new sort(direction.asc, sorttype);
      return new pagerequest(pagenum - 1, pagesize, sort);
    }
  }
 
  /**
   * 创建分页请求(该方法可以放到util类中).
   */
  public static pagerequest buildpagerequest(int pagenum, int pagesize, string sorttype) {
    return buildpagerequest(pagenum, pagesize, sorttype, null);
  }
  
  /**
   * 创建分页请求
   *
   * @author yangzhenghua
   * @date 2014-11-12
   *
   * @param pagenum
   * @param pagesize
   * @param sort
   * @return
   */
  public static pagerequest buildpagerequest(int pagenum, int pagesize, sort sort) {
    return new pagerequest(pagenum - 1, pagesize, sort);
  }
 
  /**
   * 创建分页请求(该方法可以放到util类中).
   */
  public static pagerequest buildpagerequest(int pagenum, int pagesize) {
    return buildpagerequest(pagenum, pagesize, null, null);
  }
 
}

 4、dao(sbgldao.java),对,只需要继承spring data jpa的pagingandsortingrepository接口,controller中调用其findall()方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.jinhetech.yogurt.sbgl.dao;
 
import org.springframework.data.jpa.repository.jpaspecificationexecutor;
import org.springframework.data.repository.crudrepository;
import org.springframework.data.repository.pagingandsortingrepository;
import org.springframework.stereotype.repository;
 
import com.jinhetech.yogurt.sbgl.entity.sbgl;
import com.jinhetech.yogurt.user.entity.userinfo;
 
@repository("sbgldao")
public interface sbgldao extends pagingandsortingrepository<sbgl, string>, jpaspecificationexecutor<sbgl> {
  
  
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/Donnnnnn/p/6277872.html