ssm mysql分页_SSM 使用 mybatis 分页插件 pagehepler 实现分页

前几天在这里分享了手写 sql 分页查询实现分页,现在来看看使用 mybatis 分页插件 pagehepler 来实现分页

使用分页插件的原因,简化了sql代码的写法,实现较好的物理分页,比写一段完整的分页sql代码,也能减少了误差性。

我这里使用 maven 工程实现:

1.首先导入分页插件的依赖:

1

2 com.github.pagehelper

3 pagehelper

4 5.0.0

5

2.配置分页拦截器插件

官方文档有两种配置分页拦截器插件

1. 在 MyBatis 配置 xml 中配置拦截器插件

2. 在 Spring 配置文件中配置拦截器插件

使用 spring 的属性配置方式,可以使用 plugins 属性像下面这样配置:

params=value1

这里我项目中使用的是第二种,里面的参数根据实际情况配置,也可以不配置

3.调用方法

mapper 层 sql 语句按照一般查询写法,不用写分页:

1

2

3 SELECT4 id,5 countryname,6 countrycode7 FROM8 country9

mapper 层接口:

1 /**

2 * 查询3 *@paramparams4 *@return

5 */

6 public List finds();

service 业务层接口:

1 /**

2 * 分页查询3 *@paramparams 分页参数 pageNo(页码),pageSize(每页查询数目)4 *@return

5 */

6 public PageInfo finds(Params params);

service 业务层实现类:PageHelper.startPage(1, 10);第一个参数表示第几页,第二个参数表示每页显示的记录数

执行完 PageHelper.startPage(1, 10);语句后,紧跟着的第一个select方法会被分页:List blogs = countryMapper.finds();

然后再用 PageInfo 对查询结果进行包装,PageInfo pageInfo = new PageInfo(blogs);

并将 pageInfo 返回到控制层

1 /**

2 * 查询3 */

4 public PageInfofinds(Params params) {5

6 //查询

7 int pageNo =params.getPageNo();8 int pageSize =params.getPageSize();9

10 PageHelper.startPage(pageNo, pageSize);11 List blogs =countryMapper.finds();12 //用PageInfo对结果进行包装

13 PageInfo pageInfo = new PageInfo(blogs);14

15 returnpageInfo;16

17 }

这里返回 pageInfo 后,在 controller 层传入 params 分页参数 和 解析 pageInfo:

List clist = pageInfo.getList(); 再将 clist  放进作用域,在前台页面利用可循环获得分页数据

1 /**

2 * 首页,并且分页查询3 *@return

4 */

5 @RequestMapping("/index")6 publicModelAndView index(Params params){7

8 ModelAndView modelAndView = newModelAndView();9 //一开始第一页,查询10条

10 params.setPageNo(1);11 params.setPageSize(10);12 PageInfo pageInfo =countryService.finds(params);13

14 List clist =pageInfo.getList();15

16 //查询数量

17 long couts =countryService.counts();18

19 modelAndView.addObject("clist", clist);20 modelAndView.addObject("couts", couts);21 modelAndView.setViewName("index");22

23 returnmodelAndView;24 }

上面说的都是关键分页的实现代码,现在看看全部配置和实现的代码:

pom.xml

1

2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

3 4.0.0

4 com.krry

5 maven_pagehepler_ora

6 war

7 1.0-SNAPSHOT

8 maven_pagehepler_ora

9 http://maven.apache.org

10

11

12 UTF-8

13

14

15 ${basedir}/src/main/java

16 com.isea533.mybatis.mapper

17 com.isea533.mybatis.model

18

19 ${basedir}/src/main/resources

20 mapper

21

22 1.6

23

24 3.3.1

25 3.3.6

26 5.0.0

27 5.1.29

28 4.1.2.RELEASE

29 1.2.4

30

31

32

33

34 junit

35 junit

36 4.11

37 test

38

39

40 log4j

41 log4j

42 1.2.17

43

44

45

46

47 javax.servlet

48 servlet-api

49 2.5

50 provided

51

52

53 javax.servlet.jsp

54 jsp-api

55 2.1

56 provided

57

58

59 javax.servlet

60 jstl

61 1.2

62

63

64 javax.ws.rs

65 javax.ws.rs-api

66 2.0

67

68

69 javax.websocket

70 javax.websocket-api

71 1.0

72

73

74 javax.annotation

75 javax.annotation-api

76 1.2

77

78

79 javax.transaction

80 javax.transaction-api

81 1.2

82

83

84

85

86 org.springframework

87 spring-context

88

89

90 org.springframework

91 spring-orm

92

93

94 org.springframework

95 spring-oxm

96

97

98 org.springframework

99 spring-jdbc

100

101

102 org.springframework

103 spring-tx

104

105

106 org.springframework

107 spring-web

108

109

110 org.springframework

111 spring-webmvc

112

113

114 org.springframework

115 spring-aop

116

117

118 org.springframework

119 spring-test

120

121

122

123

124 org.codehaus.castor

125 castor-xml

126 1.3.3

127

128

129

130 com.fasterxml.jackson.core

131 jackson-databind

132 2.4.2

133

134

135 com.fasterxml.jackson.dataformat

136 jackson-dataformat-xml

137 2.4.2

138

139

140

141 org.aspectj

142 aspectjweaver

143 1.8.2

144

145

146

147

148 commons-fileupload

149 commons-fileupload

150 1.3.1

151

152

153

154

155 mysql

156 mysql-connector-java

157 ${mysql.version}

158

159

160

161

162 com.alibaba

163 druid

164 1.0.11

165

166

167

168

169 org.mybatis

170 mybatis

171 ${mybatis.version}

172

173

174 org.mybatis

175 mybatis-spring

176 ${mybatis.spring.version}

177

178

179

180 org.mybatis.generator

181 mybatis-generator-core

182 1.3.2

183 compile

184 true

185

186

187

188 com.github.pagehelper

189 pagehelper

190 ${pagehelper.version}

191

192

193

194 tk.mybatis

195 mapper

196 ${mapper.version}

197

198

199

200 org.apache.commons

201 commons-lang3

202 3.3.2

203

204

205

206

207

208

209

210 org.springframework

211 spring-framework-bom

212 ${spring.version}

213 pom

214 import

215

216

217

218

219

220 nexus

221 local private nexus

222 http://maven.oschina.net/content/groups/public/

223

224 true

225

226

227 false

228

229

230

231 sonatype-nexus-releases

232 Sonatype Nexus Releases

233 http://oss.sonatype.org/content/repositories/releases

234

235 true

236

237

238 false

239

240

241

242 sonatype-nexus-snapshots

243 Sonatype Nexus Snapshots

244 http://oss.sonatype.org/content/repositories/snapshots

245

246 false

247

248

249 true

250

251

252

253

254

255

256 org.apache.maven.plugins

257 maven-compiler-plugin

258 3.2

259

260 1.7

261 1.7

262

263

264

265

266

applicationContext.xml

1 <?xml version="1.0" encoding="UTF-8"?>

2

3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"

4 xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"

5 xmlns:util="http://www.springframework.org/schema/util"xmlns:p="http://www.springframework.org/schema/p"

6 xsi:schemaLocation="http://www.springframework.org/schema/beans7 http://www.springframework.org/schema/beans/spring-beans.xsd8 http://www.springframework.org/schema/aop9 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd10 http://www.springframework.org/schema/tx11 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd12 http://www.springframework.org/schema/context13 http://www.springframework.org/schema/context/spring-context-3.0.xsd14 http://www.springframework.org/schema/util15 http://www.springframework.org/schema/util/spring-util-3.0.xsd16 ">

17

18

19

20

21

22

23

24

25

26

27

28 init-method="init"destroy-method="close">

29

30

31

32

33

34

35

36

37 class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

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

mybatis-config.xml

1 <?xml version="1.0" encoding="UTF-8"?>

2 "http://mybatis.org/dtd/mybatis-3-config.dtd">

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

springmvc.xml

1 <?xml version="1.0" encoding="UTF-8"?>

2

3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

4 xmlns:p="http://www.springframework.org/schema/p"

5 xmlns:context="http://www.springframework.org/schema/context"

6 xmlns:util="http://www.springframework.org/schema/util"

7 xmlns:mvc="http://www.springframework.org/schema/mvc"

8 xsi:schemaLocation="http://www.springframework.org/schema/beans9 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd10 http://www.springframework.org/schema/context11 http://www.springframework.org/schema/context/spring-context-3.0.xsd12 http://www.springframework.org/schema/util13 http://www.springframework.org/schema/util/spring-util-3.0.xsd14 http://www.springframework.org/schema/mvc15 http://www.springframework.org/schema/mvc/spring-mvc.xsd16 ">

17

18

19

20

21

22

23

24

25

26 text/plain;charset=UTF-8

27 text/html;charset=UTF-8

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45 NON_NULL

46

47

48

49

50

51 application/json;charset=UTF-8

52 application/x-www-form-urlencoded;charset=UTF-8

53

54

55

56

57

58

59

60

61

62

63

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

jdbc.properties 和 log4j.properties 就不用展示了,都差不多的

web.xml

1 <?xml version="1.0" encoding="UTF-8"?>

2

3 maven_pagehepler

4

5 index/index

6

7

8 contextConfigLocation

9 classpath:applicationContext.xml

10

11

12 org.springframework.web.context.ContextLoaderListener

13

14

15 org.springframework.web.util.IntrospectorCleanupListener

16

17

18 encoding

19 org.springframework.web.filter.CharacterEncodingFilter

20

21 encoding

22 UTF-8

23

24

25

26 encoding

27 /*

28

29

30 maven_pagehepler

31 org.springframework.web.servlet.DispatcherServlet

32

33 contextConfigLocation

34 classpath:springmvc.xml

35

36

37

38 maven_pagehepler

39 /index/index

40

41

42 maven_pagehepler

43 /

44

45

实体类:Country.java

1 packagecom.krry.entity;2

3 public classCountry {4

5

6 /**

7 * 主键8 */

9 privateInteger id;10

11 /**

12 * 名称13 */

14 privateString countryname;15

16 /**

17 * 代码18 */

19 privateString countrycode;20

21

22

23 publicCountry(Integer id, String countryname, String countrycode) {24 this.id =id;25 this.countryname =countryname;26 this.countrycode =countrycode;27 }28

29 /**

30 * 获取主键31 *32 *@returnId - 主键33 */

34 publicInteger getId() {35 returnid;36 }37

38 /**

39 * 设置主键40 *41 *@paramid 主键42 */

43 public voidsetId(Integer id) {44 this.id =id;45 }46

47 /**

48 * 获取名称49 *50 *@returncountryname - 名称51 */

52 publicString getCountryname() {53 returncountryname;54 }55

56 /**

57 * 设置名称58 *59 *@paramcountryname 名称60 */

61 public voidsetCountryname(String countryname) {62 this.countryname =countryname;63 }64

65 /**

66 * 获取代码67 *68 *@returncountrycode - 代码69 */

70 publicString getCountrycode() {71 returncountrycode;72 }73

74 /**

75 * 设置代码76 *77 *@paramcountrycode 代码78 */

79 public voidsetCountrycode(String countrycode) {80 this.countrycode =countrycode;81 }82 }

Params.java

1 packagecom.krry.entity;2

3 /**

4 *5 * Params6 *@authorkrry7 *@version1.0.08 *9 */

10 public classParams {11

12 private Integer pageSize = 10;13 private Integer pageNo = 0;14

15

16 publicInteger getPageNo() {17 returnpageNo;18 }19

20 public voidsetPageNo(Integer pageNo) {21 this.pageNo =pageNo;22 }23

24 publicInteger getPageSize() {25 returnpageSize;26 }27

28 public voidsetPageSize(Integer pageSize) {29 this.pageSize =pageSize;30 }31

32 }

持久层:CountryMapper.java

1 packagecom.krry.mapper;2

3 importjava.util.List;4

5 importcom.krry.entity.Country;6

7 /**

8 *9 * Mapper:操作数据库10 *@authorkrry11 *@version1.0.012 *13 */

14 public interfaceCountryMapper {15

16 /**

17 * 查询18 *@paramparams19 *@return

20 */

21 public Listfinds();22

23 /**

24 * 计算25 * com.krry.dao.admin26 * 方法名:countBlogs27 *@authorkrry28 *@paramparams29 *@returnint30 *@exception

31 *@since1.0.032 */

33 public longcounts();34

35

36 }

CountryMapper.xml

1 <?xml version="1.0" encoding="UTF-8"?>

2 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

4

5

6

7

8

9 SELECT10 id,11 countryname,12 countrycode13 FROM14 country15

16

17

18

19 SELECT20 count(*)21 FROM22 country23

24

25

26

业务层接口:

1 packagecom.krry.service;2

3 importjava.util.HashMap;4 importjava.util.List;5

6 importjavax.servlet.http.HttpServletRequest;7

8 importorg.apache.ibatis.annotations.Param;9

10 importcom.github.pagehelper.PageInfo;11 importcom.krry.entity.Country;12 importcom.krry.entity.Params;13

14 /**

15 * service层:处理业务逻辑(impl里面实现)16 *@authorasusaad17 *18 */

19 public interfaceICountryService {20

21 /**

22 * 分页查询所有博客23 *@paramparams 分页参数 pageNo(页码),pageSize(每页查询数目)24 *@return

25 */

26 public PageInfofinds(Params params);27

28 /**

29 * 计算博客数量30 *@paramparams31 *@return

32 */

33 public longcounts();34

35 }

业务层实现类

1 packagecom.krry.service.impl;2

3 importjava.util.HashMap;4 importjava.util.List;5

6 importorg.springframework.beans.factory.annotation.Autowired;7 importorg.springframework.stereotype.Service;8 importorg.springframework.web.servlet.ModelAndView;9

10 importcom.github.pagehelper.PageHelper;11 importcom.github.pagehelper.PageInfo;12 importcom.krry.entity.Country;13 importcom.krry.entity.Params;14 importcom.krry.mapper.CountryMapper;15 importcom.krry.service.ICountryService;16

17 /**

18 * 实现service层接口19 *@authorasusaad20 *21 */

22 @Service23 public class CountryService implementsICountryService{24

25 @Autowired26 privateCountryMapper countryMapper;27

28 /**

29 * 查询30 */

31 public PageInfofinds(Params params) {32

33 //查询

34 int pageNo =params.getPageNo();35 int pageSize =params.getPageSize();36

37 PageHelper.startPage(pageNo, pageSize);38 List blogs =countryMapper.finds();39 //用PageInfo对结果进行包装

40 PageInfo pageInfo = new PageInfo(blogs);41

42 returnpageInfo;43

44 }45

46 /**

47 * 计算48 *@paramparams49 *@return

50 */

51 public longcounts(){52

53 long couts =countryMapper.counts();54

55 returncouts;56 }57

58

59

60 }

控制层:KrryController.java

1 packagecom.krry.controller;2

3 importjava.util.ArrayList;4 importjava.util.HashMap;5 importjava.util.List;6

7 importorg.junit.Test;8 importorg.springframework.beans.factory.annotation.Autowired;9 importorg.springframework.stereotype.Controller;10 importorg.springframework.web.bind.annotation.RequestMapping;11 importorg.springframework.web.bind.annotation.ResponseBody;12 importorg.springframework.web.servlet.ModelAndView;13

14 importcom.github.pagehelper.PageInfo;15 importcom.krry.entity.Country;16 importcom.krry.entity.Params;17 importcom.krry.service.ICountryService;18

19 /**

20 * KrryController21 * controller层,作为请求转发22 *@authorasusaad23 *24 */

25 @Controller //表示是多例模式,每个用户返回的web层是不一样的

26 @RequestMapping("/index")27 public classKrryController {28

29 @Autowired30 privateICountryService countryService;31

32 /**

33 * 首页,并且分页查询34 *@return

35 */

36 @RequestMapping("/index")37 publicModelAndView index(Params params){38

39 ModelAndView modelAndView = newModelAndView();40 //一开始第一页,查询10条

41 params.setPageNo(1);42 params.setPageSize(10);43 PageInfo pageInfo =countryService.finds(params);44

45 List clist =pageInfo.getList();46

47 //查询数量

48 long couts =countryService.counts();49

50 modelAndView.addObject("clist", clist);51 modelAndView.addObject("couts", couts);52 modelAndView.setViewName("index");53

54 returnmodelAndView;55 }56

57 /**

58 * ajax请求 的 分页查询59 *@paramparams60 *@return

61 */

62 @ResponseBody63 @RequestMapping("/loadData")64 public HashMaploadData(Params params){65

66

67 HashMap map = new HashMap();68 PageInfo pageInfo =countryService.finds(params);69 List clist =pageInfo.getList();70 map.put("clist", clist);71

72 returnmap;73 }74 //

75

76 }

3c32c8918aec76aa6dfcd5aabef11e08.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值