EasyUi+Spring Data 实现按条件分页查询的实例代码

2025-05-29 0 109

spring data 介绍

spring data 出现目的 为了简化、统一 持久层 各种实现技术 api ,所以 spring data 提供一套标准 api 和 不同持久层整合技术实现 .

自己开发 repository 只需要继承 jparepository 接口crudrepository
save、 delete、 deteleall、 findall、 findone、 count
pagingandsortingrepository
findall(sort) 基于排序的查询、 findall(pageable) 基于分页的查询

spring data query 使用 实现条件查询

第一种 根据方法命名规则自动生成

基于一列查询等值查询 findby 列名 例如: findbyname(string name);

基于一列模糊查询 findby 列名 like 例如: findbynamelike(string name)

基于两列等值查询 findby 列名 and 列名 例如: findbyusernameandpassword(string username, string password )

第二种 不按命名规则写的查询方法,可以配置@query 绑定 jpal 语句或者 sql 语句

EasyUi+Spring Data 实现按条件分页查询的实例代码

第三种 不按命名规则写的查询方法 配置@query 没写语句 , 实体类 @namedquery 定义

EasyUi+Spring Data 实现按条件分页查询的实例代码

带有条件 修改和删除操作

使用@query 注解完成 , 搭配使用@modifying 标记修改、删除操作

将记录 1 的 最小长度改为 15

EasyUi+Spring Data 实现按条件分页查询的实例代码

注意:使用单体测试,测试 dao ,要添加事务,设置事务不回滚

EasyUi+Spring Data 实现按条件分页查询的实例代码

代码实现

1.前端页面端

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20
//首先需要使用到表单序列化为json对象的方法

  

//将表单序列化为json对象

$.fn.serializejson=function(){

var serializeobj={};

var array=this.serializearray();

var str=this.serialize();

$(array).each(function(){

if(serializeobj[this.name]){

if($.isarray(serializeobj[this.name])){

serializeobj[this.name].push(this.value);

}else{

serializeobj[this.name]=[serializeobj[this.name],this.value];

}

}else{

serializeobj[this.name]=this.value;

}

});

return serializeobj;

};

2.获取到giid表单信息

?

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
$(function(){

// 先将body隐藏,再显示,不会出现页面刷新效果

$("body").css({visibility:"visible"});

// 信息表格

$('#grid').datagrid( {

iconcls : 'icon-forward',

fit : true,

border : false,

rownumbers : true,

striped : true,

pagelist: [30,50,100],

pagination : true,

toolbar : toolbar,

url : "../../courier_pagequery.action",

idfield : 'id',

columns : columns,

ondblclickrow : dodblclickrow

//按条件查询

$("#searchbtn").click(function(){

//将searchfrom表单中的数据转成json数据

var params = $("#searchform").serializejson();

//将json对象,绑定到datagrid上,完成带有条件查询的请求

$("#grid").datagrid('load',params);

//关闭查询窗口

$("#searchwindow").window('close');

});

});

});

3.后台代码

action操作

?

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

227

228

229

230

231

232

233
@suppresswarnings("all")

@parentpackage("json-default")

@namespace("/")

@controller

@scope("prototype")

public class courieraction extends actionsupport implements modeldriven<courier>{

//模型驱动

private courier courier = new courier();

@override

public courier getmodel() {

return courier;

}

//注入courierservice

@autowired

private courierservice courierserive;

/**

* 保存

*/

@action(value="curier_save",results={@result(name="success",

location="./pages/base/courier.html",type="redirect")})

public string save(){

//调用业务层

courierserive.save(courier);

return success;

}

//--分页查询所有取派员数据---

//接收参数 属性驱动

private int page;

private int rows;

public void setpage(int page) {

this.page = page;

}

public void setrows(int rows) {

this.rows = rows;

}

/**

* 分页查询所有的信息

*/

@action(value="courier_pagequery",results={@result(name="success",type="json")})

public string pagequery(){

//调用spring的方法

pageable pageable = new pagerequest(page-1, rows);

//根据查询条件,构造specification条件查询对象

specification<courier> specification = new specification<courier>() {

/**

* 构造条件查询方法,如果方法返回null 代表无条件查询

* root 参数 获取条件表达式

* criteriaquery 参数 构造简单查询条件返回,提供where方法

* criteriabuilder 参数 构造predicate对象,条件对象,构造复杂查询效果

*/

@override

public predicate topredicate(root<courier> root, criteriaquery<?> query, criteriabuilder cb) {

//将查询到的结果放到集合中

list<predicate> list = new arraylist<predicate>();

//单表查询(查询当前对象对应的数据表),查询工号,等值查询

if(stringutils.isnotblank(courier.getcouriernum())){

predicate p1 =cb.equal(root.get("couriernum").as(string.class), courier.getcouriernum());

list.add(p1);

}

if(stringutils.isnotblank(courier.getcompany())){

//查询所属单位,模糊查询

predicate p2 = cb.like(root.get("company").as(string.class), "%"+courier.getcompany()+"%");

list.add(p2);

}

if(stringutils.isnotblank(courier.gettype())){

//查询类型,等值查询

predicate p3 = cb.equal(root.get("type").as(string.class),courier.gettype());

list.add(p3);

}

//多表查询,查询标准

join<courier, standard> standardroot = root.join("standard",jointype.inner);

if(courier.getstandard() != null && stringutils.isnotblank(courier.getstandard().getname())){

//进行模糊查询

predicate p4 = cb.like(standardroot.get("name").as(string.class),

"%"+courier.getstandard().getname()+"%");

list.add(p4);

}

//new predicate[0] 代表一个泛型,返回类型是predicate

return cb.and(list.toarray(new predicate[0]));

}

};

//调用业务层

page<courier> pagedata=courierserive.findall(specification,pageable);

//压入值栈对象

//根据查询结果封装datagrid需要的数据格式

map<string,object> result = new hashmap<string,object>();

result.put("total", pagedata.gettotalelements());

result.put("rows", pagedata.getcontent());

//压入值栈返回

actioncontext.getcontext().getvaluestack().push(result);

return success;

}

}

service层操作

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15
/**

* 条件查询所有的快递员信息

*/

@override

public page<courier> findall(specification<courier> specification, pageable pageable) {

return courierrepository.findall(specification,pageable);

}

dao操作,dao接口需要继承两个接口

spring data 还是非常强大的,封装了hibernate的很多方法,我们可以直接拿来用

?

1

2

3

4

5
public interface courierrepository extends jparepository<courier, integer>,

jpaspecificationexecutor<courier>{

}

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

原文链接:http://www.cnblogs.com/mrccjj/p/7253497.html?utm_source=tuicool&utm_medium=referral

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 EasyUi+Spring Data 实现按条件分页查询的实例代码 https://www.kuaiidc.com/115557.html

相关文章

猜你喜欢
发表评论
暂无评论