一、spring Data JPA分页
分页效果如下:
前台表格用的是: Bootstrap
kkpager是一个js分页展示控件,传入简单参数就能使用的分页效果控件,github地址:https://github.com/pgkk/kkpager
项目结构:
FamilyMember实体类:
?
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
|
package com.fendo.entity;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table (name= "FAMILY_MEMBER" )
public class FamilyMember implements Serializable{
private Integer id;
private String FamilyName;
private String FamilyCharge;
private String Mobile;
private String Email;
private String Address;
private Date CreateData;
@Id
@GeneratedValue (strategy=GenerationType.IDENTITY)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this .id = id;
}
@Column (name= "FAMILY_NAME" )
public String getFamilyName() {
return FamilyName;
}
public void setFamilyName(String familyName) {
FamilyName = familyName;
}
@Column (name= "FAMILY_CHARGE" )
public String getFamilyCharge() {
return FamilyCharge;
}
public void setFamilyCharge(String familyCharge) {
FamilyCharge = familyCharge;
}
@Column (name= "MOBILE" )
public String getMobile() {
return Mobile;
}
public void setMobile(String mobile) {
Mobile = mobile;
}
@Column (name= "EMAIL" )
public String getEmail() {
return Email;
}
public void setEmail(String email) {
Email = email;
}
@Column (name= "ADDRESS" )
public String getAddress() {
return Address;
}
public void setAddress(String address) {
Address = address;
}
@Column (name= "CREATE_DATA" )
public Date getCreateData() {
return CreateData;
}
public void setCreateData(Date createData) {
CreateData = createData;
}
public FamilyMember() {
super ();
// TODO Auto-generated constructor stub
}
public FamilyMember(Integer id, String familyName, String familyCharge, String mobile, String email, String address,
Date createData) {
super ();
this .id = id;
FamilyName = familyName;
FamilyCharge = familyCharge;
Mobile = mobile;
Email = email;
Address = address;
CreateData = createData;
}
}
|
FamilyDao接口类:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package com.fendo.dao;
import java.util.List;
import java.util.Map;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.PagingAndSortingRepository;
import com.fendo.entity.FamilyMember;
public interface FamilyDao extends PagingAndSortingRepository<FamilyMember, String>, JpaSpecificationExecutor<FamilyMember>{
}
|
FamilyService服务接口类:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package com.fendo.service;
import java.util.List;
import java.util.Map;
import com.fendo.entity.FamilyMember;
public interface FamilyService {
public List<FamilyMember> getAll() throws Exception;
public FamilyMember save(FamilyMember familyMember) throws Exception;
public Map<String, Object> getUserBySearch(Map<String, String> familyArgs, final String sortType) throws Exception;
}
|
FamilyService服务接口实现类:
?
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
|
package com.fendo.service.imp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import org.springframework.stereotype.Service;
import com.fendo.dao.FamilyDao;
import com.fendo.entity.FamilyMember;
import com.fendo.service.FamilyService;
import com.fendo.util.PageUtils;
@Service
@Transactional
public class FamilyServiceImp implements FamilyService{
@Autowired
public FamilyDao familyDao;
@Override
public List<FamilyMember> getAll() throws Exception {
return (List<FamilyMember>) this .familyDao.findAll();
}
@Override
public FamilyMember save(FamilyMember familyMember) throws Exception {
return familyDao.save(familyMember);
}
/**
* 查询用户信息列表(支持分页和多条件查询)。
*
*/
@Override
public Map<String, Object> getUserBySearch(Map<String, String> familyArgs, final String sortType) throws Exception {
// 获得分页对象pageable,并且在pageable中页码是从0开始,设定按照sortType升序排列
Pageable pageable = PageUtils.buildPageRequest(Integer.valueOf(familyArgs.get( "pageNum" )),
Integer.valueOf(familyArgs.get( "pageSize" )), sortType);
Page<FamilyMember> objPage = familyDao.findAll( new Specification<FamilyMember>() {
public Predicate toPredicate(Root<FamilyMember> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
List<Predicate> lstPredicates = new ArrayList<Predicate>();
if (StringUtils.isNotBlank(familyArgs.get( "FamilyName" ))) {
lstPredicates.add(cb.like(root.get( "familyName" ).as(String. class ), "%" + familyArgs.get( "FamilyName" ) + "%" ));
}
if (StringUtils.isNotBlank(familyArgs.get( "Mobile" ))) {
lstPredicates.add(cb.like(root.get( "mobile" ).as(String. class ), "%" + familyArgs.get( "Mobile" ) + "%" ));
}
Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
return cb.and(lstPredicates.toArray(arrayPredicates));
}
}, pageable);
return PageUtils.getPageMap(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
|
package com.fendo.util;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
/**
* 工具类
* @author fendo
*
*/
public class FamilyUtil {
/**
* 封装从前台传递过来的查询参数。
*
*/
public static Map<String, String> getSelArgsToMap(HttpServletRequest request) throws Exception {
Map<String, String> serArgs = new HashMap<String, String>();
String FamilyName = request.getParameter( "FamilyName" );
String Mobile = request.getParameter( "Mobile" );
String pageNum = request.getParameter( "pageNum" ) == null ? "1" : request.getParameter( "pageNum" );
String pageSize = request.getParameter( "pageSize" ) == null ? "10" : request.getParameter( "pageSize" );
serArgs.put( "FamilyName" , FamilyName);
serArgs.put( "Mobile" , Mobile);
serArgs.put( "pageNum" , pageNum);
serArgs.put( "pageSize" , pageSize);
return 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
package com.fendo.util;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
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;
public class PageUtils {
/**
* 封装分页数据到Map中。
*/
public static Map<String, Object> getPageMap(Page<?> objPage) {
Map<String, Object> resultMap = new HashMap<String, Object>();
resultMap.put( "resultList" , objPage.getContent()); // 数据集合
resultMap.put( "totalNum" , objPage.getTotalElements()); // 总记录数
resultMap.put( "totalPage" , objPage.getTotalPages()); // 总页数
resultMap.put( "pageNum" , objPage.getNumber()); // 当前页码
resultMap.put( "pageSize" , objPage.getSize()); // 每页显示数量
return resultMap;
}
/**
* 创建分页请求。
*
* @param pageNum 当前页
* @param pageSize 每页条数
* @param sortType 排序字段
* @param direction 排序方向
*/
public static PageRequest buildPageRequest( int pageNum, int pageSize, String sortType, String direction) {
Sort sort = null ;
if (!StringUtils.isNotBlank(sortType)) {
return new PageRequest(pageNum - 1 , pageSize);
} else if (StringUtils.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 );
}
/**
* 创建分页请求
*
* @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 );
}
}
|
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
|
package com.fendo.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.fendo.entity.FamilyMember;
import com.fendo.service.imp.FamilyServiceImp;
import com.fendo.util.FamilyUtil;
@Controller ()
@RequestMapping (value= "DataTable" )
public class DataTableController {
@Autowired
public FamilyServiceImp FamilyMember;
@RequestMapping (value= "/home_list" )
public String home(Model model,HttpServletRequest request,HttpServletResponse response){
Map<String, Object> resultMap = new HashMap<>();
List<FamilyMember> list;
try {
list = FamilyMember.getAll();
// 查询表单或分页保持请求时 请求参数的接收
Map<String, String> serArgs = new HashMap<String, String>();
serArgs = FamilyUtil.getSelArgsToMap(request); //这个类在下面给出
resultMap = FamilyMember.getUserBySearch(serArgs, "CreateData" ); //按创建时间排序
model.addAttribute( "resultMap" ,resultMap);
} catch (Exception e) {
e.printStackTrace();
}
return "datatable" ;
}
}
|
首页datatable.jsp
?
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
|