SSM使用mybatis分页插件pagehepler实现分页示例

网友投稿 254 2023-02-11


SSM使用mybatis分页插件pagehepler实现分页示例

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

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

Mybatis分页插件 demo 项目地址:free-Mybatis_PageHelper_jb51.rar

我这里使用 maven 工程实现:

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

com.github.pagehelper

pagehelper

5.0.0

2.配置分页拦截器插件

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

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

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

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

params=value1

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

3.调用方法

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

SELECT

id,

countryname,

countrycode

FROM

country

mapper 层接口:

/**

* 查询

* @param params

* @return

*/

public List finds();

service 业务层接口:

/**

* 分页查询

* @param params 分页参数 pageNo(页码),pageSize(每页查询数目)

* @return

*/

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 返回到控制层

/**

* 查询

*/

public PageInfo finds(Params params)

//查询

int pageNo = params.getPageNo();

int pageSize = params.getPageSize();

PageHelper.startPage(pageNo, pageSize);

List blogs = countryMapper.finds();

//用PageInfo对结果进行包装

PageInfo pageInfo = new PageInfo(blogs);

return pageInfo;

}

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

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

/**

* 首页,并且分页查询

* @return

*/

@RequestMapping("/index")

public ModelAndView index(Params params){ ModelAndView modelAndView = new ModelAndView();

//一开始第一页,查询10条

params.setPageNo(1);

params.setPageSize(10);

PageInfo pageInfo = countryService.finds(params);

List clist = pageInfo.getList();

//查询数量

long couts = countryService.counts();

modelAndView.addObject("clist", clist);

modelAndView.addObject("couts", couts);

modelAndView.setViewName("index");

return modelAndView;

}

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

pom.xml

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

4.0.0

com.krry

maven_pagehepler_ora

war

1.0-SNAPSHOT

maven_pagehepler_ora

http://maven.apache.org

UTF-8

${basedir}/src/main/java

com.isea533.mybatis.mapper

com.isea533.mybatis.model

${basedir}/src/main/resources

mapper

1.6

3.3.1

3.3.6

5.0.0

5.1.29

4.1.2.RELEASE

1.2.4

junit

junit

4.11

test

log4j

log4j

1.2.17

javax.servlet

servlet-api

2.5

provided

javax.servlet.jsp

jsp-api

2.1

provided

javax.servlet

jstl

1.2

javax.ws.rs

javax.ws.rs-api

2.0

javax.websocket

javax.websocket-api

1.0

javax.annotation

javax.annotation-api

1.2

javax.transaction

javax.transaction-api

1.2

org.springframework

spring-context

org.springframework

spring-orm

org.springframework

spring-oxm

org.springframework

spring-jdbc

org.springframework

spring-tx

org.springframework

spring-web

org.springframework

spring-webmvc

org.springframework

spring-aop

org.springframework

spring-test

org.codehaus.castor

castor-xml

1.3.3

com.fasterxml.jackson.core

jackson-databind

2.4.2

com.fasterxml.jackson.dataformat

jackson-dataformat-xml

2.4.2

org.aspectj

aspectjweaver

1.8.2

commons-fileupload

commons-fileupload

1.3.1

mysql

mysql-connector-java

${mysql.version}

com.alibaba

druid

1.0.11

org.mybatis

mybatis

${mybatis.version}

org.mybatis

mybatis-spring

${mybatis.spring.version}

org.mybatis.generator

mybatis-generator-core

1.3.2

compile

true

com.github.pagehelper

pagehelper

${pagehelper.version}

tk.mybatis

mapper

${mapper.version}

org.apache.commons

commons-lang3

3.3.2

org.springframework

spring-framework-bom

${spring.version}

pom

import

nexus

local private nexus

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

true

false

sonatype-nexus-releases

Sonatype Nexus Releases

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

true

false

sonatype-nexus-snapshots

Sonatype Nexus Snapshots

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

false

true

org.apache.maven.plugins

maven-compiler-plugin

3.2

1.7

1.7

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

4.0.0

com.krry

maven_pagehepler_ora

war

1.0-SNAPSHOT

maven_pagehepler_ora

http://maven.apache.org

UTF-8

${basedir}/src/main/java

com.isea533.mybatis.mapper

com.isea533.mybatis.model

${basedir}/src/main/resources

mapper

1.6

3.3.1

3.3.6

5.0.0

5.1.29

4.1.2.RELEASE

1.2.4

junit

junit

4.11

test

log4j

log4j

1.2.17

javax.servlet

servlet-api

2.5

provided

javax.servlet.jsp

jsp-api

2.1

provided

javax.servlet

jstl

1.2

javax.ws.rs

javax.ws.rs-api

2.0

javax.websocket

javax.websocket-api

1.0

javax.annotation

javax.annotation-api

1.2

javax.transaction

javax.transaction-api

1.2

org.springframework

spring-context

org.springframework

spring-orm

org.springframework

spring-oxm

org.springframework

spring-jdbc

org.springframework

spring-tx

org.springframework

spring-web

org.springframework

spring-webmvc

org.springframework

spring-aop

org.springframework

spring-test

org.codehaus.castor

castor-xml

1.3.3

com.fasterxml.jackson.core

jackson-databind

2.4.2

com.fasterxml.jackson.dataformat

jackson-dataformat-xml

2.4.2

org.aspectj

aspectjweaver

1.8.2

commons-fileupload

commons-fileupload

1.3.1

mysql

mysql-connector-java

${mysql.version}

com.alibaba

druid

1.0.11

org.mybatis

mybatis

${mybatis.version}

org.mybatis

mybatis-spring

${mybatis.spring.version}

org.mybatis.generator

mybatis-generator-core

1.3.2

compile

true

com.github.pagehelper

pagehelper

${pagehelper.version}

tk.mybatis

mapper

${mapper.version}

org.apache.commons

commons-lang3

3.3.2

org.springframework

spring-framework-bom

${spring.version}

pom

import

nexus

local private nexus

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

true

false

sonatype-nexus-releases

Sonatype Nexus Releases

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

true

false

sonatype-nexus-snapshots

Sonatype Nexus Snapshots

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

false

true

org.apache.maven.plugins

maven-compiler-plugin

3.2

1.7

1.7

applicationContext.xml

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

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

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

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-3.0.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-3.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/util

http://springframework.org/schema/util/spring-util-3.0.xsd

">

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

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

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

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

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

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-3.0.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-3.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/util

http://springframework.org/schema/util/spring-util-3.0.xsd

">

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

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

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

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

mybatis-config.xml

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

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

springmvc.xml

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

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

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

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

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

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/util

http://springframework.org/schema/util/spring-util-3.0.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc.xsd

">

text/plain;charset=UTF-8

text/html;charset=UTF-8

NON_NULL

application/json;charset=UTF-8

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

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

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

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

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

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

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/util

http://springframework.org/schema/util/spring-util-3.0.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc.xsd

">

text/plain;charset=UTF-8

text/html;charset=UTF-8

NON_NULL

application/json;charset=UTF-8

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

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

web.xml

maven_pagehepler

index/index

contextConfigLocation

classpath:applicationContext.xml

org.springframework.web.context.ContextLoaderListener

org.springframework.web.util.IntrospectorCleanupListener

encoding

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

encoding

/*

maven_pagehepler

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:springmvc.xml

maven_pagehepler

/index/index

maven_pagehepler

/

实体类:Country.java

package com.krry.entity;

public class Country {

/**

* 主键

*/

private Integer id;

/**

* 名称

*/

private String countryname;

/**

* 代码

*/

private String countrycode;

public Country(Integer id, String countryname, String countrycode) {

this.id = id;

this.countryname = countryname;

this.countrycode = countrycode;

}

/**

* 获取主键

*

* @return Id - 主键

*/

public Integer getId() {

return id;

}

/**

* 设置主键

*

* @param id 主键

*/

public void setId(Integer id) {

this.id = id;

}

/**

* 获取名称

*

* @return countryname - 名称

*/

public String getCountryname() {

return countryname;

}

/**

* 设置名称

*

* @param countryname 名称

*/

public void setCountryname(String countryname) {

this.countryname = countryname;

}

/**

* 获取代码

*

* @return countrycode - 代码

*/

public String getCountrycode() {

return countrycode;

}

/**

* 设置代码

*

* @param countrycode 代码

*/

public void setCountrycode(String countrycode) {

this.countrycode = countrycode;

}

}

Params.java

package com.krry.entity;

/**

*

* Params

* @author krry

* @version 1.0.0

*

*/

public class Params {

private Integer pageSize = 10;

private Integer pageNo = 0;

public Integer getPageNo() {

return pageNo;

}

public void setPageNo(Integer pageNo) {

this.pageNo = pageNo;

}

public Integer getPageSize() {

return pageSize;

}

public void setPageSize(Integer pageSize) {

this.pageSize = pageSize;

}

}

持久层:CountryMapper.java

package com.krry.mapper;

import java.util.List;

import com.krry.entity.Country;

/**

*

* Mapper:操作数据库

* @author krry

* @version 1.0.0

*

*/

public interface CountryMapper {

/**

* 查询

* @param params

* @return

*/

public List finds();

/**

* 计算

* com.krry.dao.admin

* 方法名:countBlogs

* @author krry

* @param params

* @return int

* @exception

* @since 1.0.0

*/

public long counts();

}

CountryMapper.xml

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

SELECT

id,

countryname,

countrycode

FROM

country

SELECT

count(*)

FROM

country

业务层接口:

package com.krry.service;

import java.util.HashMap;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.annotations.Param;

import com.github.pagehelper.PageInfo;

import com.krry.entity.Country;

import com.krry.entity.Params;

/**

* service层:处理业务逻辑(impl里面实现)

* @author asusaad

*

*/

public interface ICountryService {

/**

* 分页查询所有博客

* @param params 分页参数 pageNo(页码),pageSize(每页查询数目)

* @return

*/

public PageInfo finds(Params params);

/**

* 计算博客数量

* @param params

* @return

*/

public long counts();

}

业务层实现类

package com.krry.service.impl;

import java.util.HashMap;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;

import com.github.pagehelper.PageInfo;

import com.krry.entity.Country;

import com.krry.entity.Params;

import com.krry.mapper.CountryMapper;

import com.krry.service.ICountryService;

/**

* 实现service层接口

* @author asusaad

*

*/

@Service

public class CountryService implements ICountryService{

@Autowired

private CountryMapper countryMapper;

/**

* 查询

*/

public PageInfo finds(Params params) {

//查询

int pageNo = params.getPageNo();

int pageSize = params.getPageSize();

PageHelper.startPage(pageNo, pageSize);

List blogs = countryMapper.finds();

//用PageInfo对结果进行包装

PageInfo pageInfo = new PageInfo(blogs);

return pageInfo;

}

/**

* 计算

* @param params

* @return

*/

public long counts(){

long couts = countryMapper.counts();

return couts;

}

}

控制层:KrryController.java

package com.krry.controller;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import org.junit.Test;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;

import com.krry.entity.Country;

import com.krry.entity.Params;

import com.krry.service.ICountryService;

/**

* KrryController

* controller层,作为请求转发

* @author asusaad

*

*/

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

@RequestMapping("/index")

public class KrryController {

@Autowired

private ICountryService countryService;

/**

* 首页,并且分页查询

* @return

*/

@RequestMapping("/index")

public ModelAndView index(Params params){

ModelAndView modelAndView = new ModelAndView();

//一开始第一页,查询10条

params.setPageNo(1);

params.setPageSize(10);

PageInfo pageInfo = countryService.finds(params);

List clist = pageInfo.getList();

//查询数量

long couts = countryService.counts();

modelAndView.addObject("clist", clist);

modelAndView.addObject("couts", couts);

modelAndView.setViewName("index");

return modelAndView;

}

/**

* ajax请求 的 分页查询

* @param params

* @return

*/

@ResponseBody

@RequestMapping("/loadData")

public HashMap loadData(Params params){

HashMap map = new HashMap();

PageInfo pageInfo = countryService.finds(params);

List clist = pageInfo.getList();

map.put("clist", clist);

return map;

}

//

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:深入理解Node module模块
下一篇:SpringMVC实现简单跳转方法(专题)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~