当前位置: 移动技术网 > IT编程>网页制作>Html5 > java使用插件pagehelper在mybatis中实现分页查询

java使用插件pagehelper在mybatis中实现分页查询

2018年09月21日  | 移动技术网IT编程  | 我要评论

李瑞英与李先念,爆米花龙将,智器x7

摘要: com.github.pagehelper.pagehelper是一款好用的开源免费的mybatis第三方物理分页插件

pagehelper是国内牛人的一个开源项目,有兴趣的可以去看源码,都有中文注释

开源项目地址: 

 

请求url:http://www.lhsxpumps.com/_localhost:8080/listcity?page=1&limit=10

显示数据:

 

1、pagehelper的maven依赖及插件配置

<dependency>
  <groupid>com.github.pagehelper</groupid>
  <artifactid>pagehelper</artifactid>
  <version>5.1.6</version>
</dependency>

pagehelper除了本身的jar包外,它还依赖了一个叫jsqlparser的jar包,使用时,我们不需要单独指定jsqlparser的maven依赖,maven的间接依赖会帮我们引入。

2、配置拦截器插件

这个是配置在mybatis-config.xml文件中

文档中的示例

<!-- 
    plugins在配置文件中的位置必须符合要求,否则会报错,顺序如下:
    properties?, settings?, 
    typealiases?, typehandlers?, 
    objectfactory?,objectwrapperfactory?, 
    plugins?, 
    environments?, databaseidprovider?, mappers?
-->
<plugins>
    <!-- com.github.pagehelper为pagehelper类所在包名 -->
    <plugin interceptor="com.github.pagehelper.pageinterceptor">
        <!-- 使用下面的方式配置参数,后面会有所有的参数介绍 -->
        <property name="param1" value="value1"/>
    </plugin>
</plugins>

3、我的配置mybatis-config.xml:

<?xml version="1.0" encoding="utf-8"?>
<!doctype configuration public "-//mybatis.org//dtd config 3.0//en"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
    <typealiases>
        <package name="edu.nf.entity"/>
    </typealiases>
    <!-- 配置分页插件 -->
    <plugins>
        <plugin interceptor="com.github.pagehelper.pageinterceptor">
            <!--helperdialect 方言:就表示此插件针对哪个数据库进行优化处理
            这个方言可以不配置,因为此插件可以依据你的 url 的信息来推断出
            你用的数据库是哪一个
            -->
            <property name="helperdialect" value="mysql"/>
            <!--分页合理化参数-->
            <property name="reasonable" value="true"/>
        </plugin>
    </plugins>
    <!--配置数据库-->
    <environments default="mysql">
        <environment id="mysql">
            <transactionmanager type="jdbc"/>
            <datasource type="pooled">
                <property name="driver" value="com.mysql.jdbc.driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/citydb?usessl=true&amp;useunicode=true&amp;characterencoding=utf-8"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </datasource>
        </environment>
    </environments>

    <mappers>
        <mapper resource="mapper/city-mapper.xml"/>
    </mappers>
</configuration>

 

 

4、city-mapper.xml 数据库查询语句配置:

<?xml version="1.0" encoding="utf-8"?>
<!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="edu.nf.dao.citydao">
    <resultmap id="citymap" type="city" >
        <id property="cityid" column="city_id"/>
        <result property="cityen" column="city_en"/>
        <result property="citycn" column="city_cn"/>
        <result property="countrycode" column="country_code"/>
        <result property="countryen" column="country_en"/>
        <result property="countrycn" column="country_cn"/>
        <result property="provinceen" column="province_en"/>
        <result property="provincecn" column="province_cn"/>
    </resultmap>
    <!-- 这里写查询全部数据,配置好的分页插件他会自己加上limit 查询语句后面不能加; -->
    <select id="listcity" resultmap="citymap">
      select * from city_test
    </select>
    <delete id="deletecity" parametertype="java.util.list">
        delete from city_test where city_id in
        <foreach collection="list" item="city" open="(" separator="," close=")">
            #{city.cityid}
        </foreach>
    </delete>
</mapper>
view code

5、后台分页查询 servlet:

 

/**
 * @author hh
 * @date 2018/9/15
 */
@webservlet("/listcity")
public class citylistservlet extends httpservlet {
    @override
    protected void service(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
        resp.setcontenttype("application/json;charset=utf-8");
        //取出前端请求参数
        string page=req.getparameter("page");
        string limit=req.getparameter("limit");

        //分页查询结果 page页数 limit显示行数
        list<city> listcity=new cityservice().listcity(page,limit);

        // 包装page对象 listcity:page结果 , navigatepages: 页码数量
        pageinfo<city> list=new pageinfo<>(listcity,1);

        //自己写的一个响应视图类,因为前端用的是layui框架需要自己,所以自己定义responseview
        responseview vo=new responseview();
        //设值 取出总数据行
        vo.setcount(list.gettotal());
        //设值 查询的结果
        vo.setdata(list.getlist());
        //响应前端
        resp.getwriter().print(new gson().tojson(vo));
    }
}

 

 

 

6、响应视图类 responseview (因为前端用的是layui框架需要自己,所以自己定义responseview):

package edu.nf.vo;

/**
 * @author hh
 * @date 2018/9/15
 */
public class responseview {
    private int code =0;
    private long count=0l;
    private object data;

    public integer getcode() {
        return code;
    }

    public void setcode(integer code) {
        this.code = code;
    }

    public long getcount() {
        return count;
    }

    public void setcount(long count) {
        this.count = count;
    }

    public object getdata() {
        return data;
    }

    public void setdata(object data) {
        this.data = data;
    }
}
view code

7、实体类 city:

package edu.nf.entity;

/**
 * @author hh
 * @date 2018/9/14
 */
public class city {
    private string cityid;
    private string cityen;
    private string citycn;
    private string countrycode;
    private string countryen;
    private string countrycn;
    private string provinceen;
    private string provincecn;

    public string getcityid() {
        return cityid;
    }

    public void setcityid(string cityid) {
        this.cityid = cityid;
    }

    public string getcityen() {
        return cityen;
    }

    public void setcityen(string cityen) {
        this.cityen = cityen;
    }

    public string getcitycn() {
        return citycn;
    }

    public void setcitycn(string citycn) {
        this.citycn = citycn;
    }

    public string getcountrycode() {
        return countrycode;
    }

    public void setcountrycode(string countrycode) {
        this.countrycode = countrycode;
    }

    public string getcountryen() {
        return countryen;
    }

    public void setcountryen(string countryen) {
        this.countryen = countryen;
    }

    public string getcountrycn() {
        return countrycn;
    }

    public void setcountrycn(string countrycn) {
        this.countrycn = countrycn;
    }

    public string getprovinceen() {
        return provinceen;
    }

    public void setprovinceen(string provinceen) {
        this.provinceen = provinceen;
    }

    public string getprovincecn() {
        return provincecn;
    }

    public void setprovincecn(string provincecn) {
        this.provincecn = provincecn;
    }
}
view code

8、service 逻辑业务层(cityservice):

/**
 * @author hh
 * @date 2018/9/15
 */
public class cityservice {
    /**
     * 分页查询 城市信息集合
     * @return
     */
    public list<city> listcity(string offest,string pagesize){
        //类型转换
        integer pnum=integer.valueof(offest);
        integer psize=integer.valueof(pagesize);
        //调用pagehelper获取第1页,10条内容,默认查询总数count
        pagehelper.startpage(pnum,psize);
        //调用citydaoimpl 分页查询
        return new citydaoimpl().listcity();
    }

    /**
     * 批量删除
     * @param citydata
     * @return
     */
    public int deletecity(string citydata){
        list<city> list=new gson().fromjson(citydata,new typetoken<list<city>>(){}.gettype());
        try {
            new citydaoimpl().deletecity(list);
            return 200;
        } catch (exception e) {
            e.printstacktrace();
            return 403;
        }
    }
}

 

9、dao 接口类:

/**
 * @author hh
 * @date 2018/9/14
 */
public interface citydao {
    /**
     * 城市信息列表
     * @return
     */
    list<city> listcity();

    /**
     * 批量删除
     * @param listcity
     */
    void deletecity(list<city> listcity);
}

 

10、dao实现类:

/**
 * @author hh
 * @date 2018/9/14
 */
public class citydaoimpl implements citydao {
    @override
    public list<city> listcity() {
        list<city> list=null;
        try(sqlsession sqlsession = mybatisutil.getsqlsession()){
            citydao citydao=sqlsession.getmapper(citydao.class);
            list=citydao.listcity();
        }
        return list;
    }

    @override
    public void deletecity(list<city> listcity) {
        try(sqlsession sqlsession = mybatisutil.getsqlsession()){
            citydao citydao=sqlsession.getmapper(citydao.class);
            citydao.deletecity(listcity);
        }
    }
}

 

 我的项目案例(包括了上一篇博客的分页查询):

项目结构:

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网