一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務(wù)器之家 - 編程語言 - JAVA教程 - Spring MVC結(jié)合Spring Data JPA實現(xiàn)按條件查詢和分頁

Spring MVC結(jié)合Spring Data JPA實現(xiàn)按條件查詢和分頁

2021-01-27 11:20王曉東1號 JAVA教程

這篇文章主要為大家詳細(xì)介紹了Spring MVC結(jié)合Spring Data JPA實現(xiàn)按條件查詢,以及分頁效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了android九宮格圖片展示的具體代碼,供大家參考,具體內(nèi)容如下

推薦視頻:尚硅谷spring data jpa視頻教程,一學(xué)就會,百度一下就有。

后臺代碼:在dao層繼承spring data jpa的pagingandsortingrepository接口實現(xiàn)的 (實現(xiàn)方法主要在sbglserviceimpl.java類中)

前臺表現(xiàn):用kkpaper表現(xiàn)出來

實現(xiàn)效果:

Spring MVC結(jié)合Spring Data JPA實現(xiàn)按條件查詢和分頁

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中最后查詢和分頁后獲得的所有數(shù)據(jù)信息,resultmap.resultlist:resultlist是map集合的屬性,里面存著<key,value>數(shù)據(jù)

 

?
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>設(shè)備管理</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();">設(shè)備管理</a> > 設(shè)備列表
    </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();">導(dǎo)航</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();">機構(gòu)</a>
    </li>
  </ul>
  
  <!-- 根據(jù)條件查詢 -->
  <div class="clear ppd10" style="line-height:32px">
    <form action="${ctx }/sbgl/sbgl_list" method="post" id="searchform">
      <div style="float:left; width:200px">
        設(shè)備名稱:
        <input type="text" name="sersbmc" id="sersbmc" >
      </div>
      <div class="floatleft">
          設(shè)備類型:
        <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">
          設(shè)備狀態(tài):
        <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">共查詢出數(shù)據(jù)結(jié)果:<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','');">新增設(shè)備</button> -->
      <button type="button" class="btn btn-success yogurt_search_button2" onclick="window.location.href='${ctx }/sbgl/sbgl_add_list' ">新增設(shè)備</button>
      <button type="button" class="btn btn-success yogurt_search_button2" onclick="console('delete','');">刪除設(shè)備</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%">設(shè)備名稱</th>
            <th width="10%">設(shè)備類型</th>
            <th width="10%">所屬市縣</th>
            <th width="10%">基地名稱</th>
            <th width="10%">設(shè)備狀態(tài)</th>
            <th width="10%">維修聯(lián)系人</th>
            <th width="10%">聯(lián)系電話</th>
            <th width="10%">設(shè)備持有人</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;
        //初始化分頁控件
        //有些參數(shù)是可選的,比如lang,若不傳有默認(rèn)值
        kkpager.init({
          pno: pagenum,
          //總頁碼
          total: "${resultmap.totalpage}",
          //總數(shù)據(jù)條數(shù)
          totalrecords: totalrecords,
          //鏈接前部
          hrefformer: 'sbgl_list',
          //鏈接尾部
          hreflatter: '',
          getlink: function(n) {
            return getinitparam() + "&pagenum=" + n + "&pagesize=" + pagesize;
          },
          lang: {
            prepagetext: '上一頁',
            nextpagetext: '下一頁',
            totalpagebeforetext: '共',
            totalpageaftertext: '頁',
            totalrecordsaftertext: '條數(shù)據(jù)',
            gopagebeforetext: '轉(zhuǎn)到',
            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>
    <!--分頁結(jié)束-->
  </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;
 
/**
 * 系統(tǒng)用戶管理控制類
 *
 * @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;
  
  /**
   * 平臺權(quán)限管理導(dǎo)航*/
  @requestmapping("/home_list")
  public modelandview home() {
 
    return new modelandview("iot/permis/permis_home");
  }
  
  
  /**
   * 查詢設(shè)備信息列表(支持分頁和多條件查詢)。
   *
   */
  @requestmapping("sbgl_list")
  @requiresroles("sbgl/sbgl_list")
  public string getuserlist(model model) throws exception {
    //顯示設(shè)備列表
//    list<sbgl> lst=new arraylist<sbgl>();
//    lst=(list<sbgl>) sbglservice.getall();
    
    map<string, object> resultmap = null;
    // 查詢表單或分頁保持請求時 請求參數(shù)的接收
    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";
  }
 
  /**
   * 新增設(shè)備信息列表(支持分頁和多條件查詢)。
   *
   * @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

封裝從前臺傳遞過來的查詢參數(shù)、前臺傳遞過來的分頁參數(shù),都放到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 {
 
  /**
   * 封裝從前臺傳遞過來的查詢參數(shù)。
   *
   * @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 (根據(jù)前臺傳遞來的查詢參數(shù)進行查詢,獲得的結(jié)果數(shù)據(jù)放到page objpage參數(shù))

?
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開始,設(shè)定按照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(分頁數(shù)據(jù)工具類)

?
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;
 
/**
 * 分頁數(shù)據(jù)工具類。
 *
 * @author yangzhenghua
 * @version v1.0 2014-6-24 初版
 *
 */
public class pageutils {
 
  /**
   * 封裝分頁數(shù)據(jù)到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()); // 數(shù)據(jù)集合
    resultmap.put(constants.page_total_num, objpage.gettotalelements()); // 總記錄數(shù)
    resultmap.put(constants.page_total_page, objpage.gettotalpages()); // 總頁數(shù)
    resultmap.put(constants.page_num, objpage.getnumber()); // 當(dāng)前頁碼
    resultmap.put(constants.page_size, objpage.getsize()); // 每頁顯示數(shù)量
 
    return resultmap;
  }
 
  /**
   * 創(chuàng)建分頁請求。
   *
   * @author yangzhenghua
   * @date 2014-7-14
   *
   * @param pagenum 當(dāng)前頁
   * @param pagesize 每頁條數(shù)
   * @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);
    }
  }
 
  /**
   * 創(chuàng)建分頁請求(該方法可以放到util類中).
   */
  public static pagerequest buildpagerequest(int pagenum, int pagesize, string sorttype) {
    return buildpagerequest(pagenum, pagesize, sorttype, null);
  }
  
  /**
   * 創(chuàng)建分頁請求
   *
   * @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);
  }
 
  /**
   * 創(chuàng)建分頁請求(該方法可以放到util類中).
   */
  public static pagerequest buildpagerequest(int pagenum, int pagesize) {
    return buildpagerequest(pagenum, pagesize, null, null);
  }
 
}

 4、dao(sbgldao.java),對,只需要繼承spring data jpa的pagingandsortingrepository接口,controller中調(diào)用其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> {
  
  
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 天天白天天谢天天啦 | 美女露尿口 | 99在线免费观看 | 天天gan | 黄 色 大 片 网站 | 国产区最新 | 成人在线播放 | www.俺去啦 | 77色视频在线| 亚洲天堂视频在线播放 | 69av导航 | 忘忧草在线 | 538精品视频在线观看 | 91尤物在线| 国产亚洲精品激情一区二区三区 | 亚洲精品国产成人中文 | 袖珍人与大黑人性视频 | 亚洲欧美久久久久久久久久爽网站 | 操久久| 日韩毛片免费在线观看 | 天堂中文在线免费观看 | 天天做天天爽天天谢 | 秋葵视频成人 | 99成人国产精品视频 | 欧美一级在线视频 | 办公室恋情在线 | 国产小嫩模好紧 | 无套白浆| 欧美一卡2卡三卡4卡5卡免费观看 | 风间由美一区二区播放合集 | 亚洲偷窥图区色 | 91精品国产9l久久久久 | 91免费高清视频 | nhdta系列媚药系列 | 潘甜甜在线观看 | 深夜影院深a久久 | 欧美日韩精品免费一区二区三区 | ffyybb免费福利视频 | 国产成人亚洲精品91专区高清 | 欧美精品一区二区三区免费 | 男生操女生动态图 |