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

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

开源项目地址: https://pagehelper.github.io/

 

请求URL:http://localhost:8080/listCity?page=1&limit=10

显示数据:

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

 

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

相关文章:

  • 2022-01-27
  • 2022-01-24
  • 2021-06-07
  • 2021-07-22
  • 2021-11-07
  • 2021-08-10
猜你喜欢
  • 2022-12-23
  • 2021-11-07
  • 2021-06-26
  • 2021-11-08
  • 2021-07-17
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案