Mybatis引入PageHelper插件并通过PageInfo获取更多分页信息-----Mybatis框架

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.powernode</groupId>
    <artifactId>mybatis-014-page</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.10</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.33</version>
        </dependency>
<!--        mybatis插件pageHelper-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.3.2</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.11</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <!--    配置mybatis逆向工程的插件,build表示构建逆向工程-->
    <build>
        <!--        plugins表示配置多个插件-->
        <plugins>
            <plugin>
                <!--                插件的坐标-->
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.4.2</version>
                <!--                允许覆盖,必须要有,防止生成的内容不是覆盖而是追加到源程序上导致错误-->
                <configuration>
                    <overwrite>true</overwrite>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>8.0.33</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.powernode</groupId>
    <artifactId>mybatis-014-page</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.10</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.33</version>
        </dependency>
<!--        mybatis插件pageHelper-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.3.2</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.11</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <!--    配置mybatis逆向工程的插件,build表示构建逆向工程-->
    <build>
        <!--        plugins表示配置多个插件-->
        <plugins>
            <plugin>
                <!--                插件的坐标-->
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.4.2</version>
                <!--                允许覆盖,必须要有,防止生成的内容不是覆盖而是追加到源程序上导致错误-->
                <configuration>
                    <overwrite>true</overwrite>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>8.0.33</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>
<?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>
    <properties resource="jdbc.properties"/>
    <settings>
<!--        全局的延迟加载的开关,默认为false,全局的分步查询都支持-->
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <setting name="logImpl" value="SLF4J"/>
        <setting name="cacheEnabled" value="true"/>
    </settings>
    <typeAliases>
        <package name="com.powernode.mybatis.pojo"/>
    </typeAliases>
<!--    mybatis分页的拦截器-->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
    </plugins>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <package name="com.powernode.mybatis.mappers"/>
    </mappers>
</configuration>

<?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>
    <properties resource="jdbc.properties"/>
    <settings>
<!--        全局的延迟加载的开关,默认为false,全局的分步查询都支持-->
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <setting name="logImpl" value="SLF4J"/>
        <setting name="cacheEnabled" value="true"/>
    </settings>
    <typeAliases>
        <package name="com.powernode.mybatis.pojo"/>
    </typeAliases>
<!--    mybatis分页的拦截器-->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
    </plugins>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <package name="com.powernode.mybatis.mappers"/>
    </mappers>
</configuration>

<?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="com.powernode.mybatis.mappers.CarMapper">
  <resultMap id="BaseResultMap" type="com.powernode.mybatis.pojo.Car">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="car_num" jdbcType="VARCHAR" property="carNum" />
    <result column="brand" jdbcType="VARCHAR" property="brand" />
    <result column="guide_price" jdbcType="DECIMAL" property="guidePrice" />
    <result column="produce_time" jdbcType="CHAR" property="produceTime" />
    <result column="car_type" jdbcType="VARCHAR" property="carType" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <select id="selectByPage" resultType="car">
    select * from t_car limit #{startIndex},#{pageSize}
  </select>
  <select id="selectAllByPageHelper" resultType="Car">
    select * from t_car
  </select>
  <sql id="Update_By_Example_Where_Clause">
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    id, car_num, brand, guide_price, produce_time, car_type
  </sql>
  <select id="selectByExample" parameterType="com.powernode.mybatis.pojo.CarExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from t_car
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from t_car
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from t_car
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <delete id="deleteByExample" parameterType="com.powernode.mybatis.pojo.CarExample">
    delete from t_car
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.powernode.mybatis.pojo.Car">
    insert into t_car (id, car_num, brand, 
      guide_price, produce_time, car_type
      )
    values (#{id,jdbcType=BIGINT}, #{carNum,jdbcType=VARCHAR}, #{brand,jdbcType=VARCHAR}, 
      #{guidePrice,jdbcType=DECIMAL}, #{produceTime,jdbcType=CHAR}, #{carType,jdbcType=VARCHAR}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.powernode.mybatis.pojo.Car">
    insert into t_car
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="carNum != null">
        car_num,
      </if>
      <if test="brand != null">
        brand,
      </if>
      <if test="guidePrice != null">
        guide_price,
      </if>
      <if test="produceTime != null">
        produce_time,
      </if>
      <if test="carType != null">
        car_type,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=BIGINT},
      </if>
      <if test="carNum != null">
        #{carNum,jdbcType=VARCHAR},
      </if>
      <if test="brand != null">
        #{brand,jdbcType=VARCHAR},
      </if>
      <if test="guidePrice != null">
        #{guidePrice,jdbcType=DECIMAL},
      </if>
      <if test="produceTime != null">
        #{produceTime,jdbcType=CHAR},
      </if>
      <if test="carType != null">
        #{carType,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.powernode.mybatis.pojo.CarExample" resultType="java.lang.Long">
    select count(*) from t_car
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    update t_car
    <set>
      <if test="row.id != null">
        id = #{row.id,jdbcType=BIGINT},
      </if>
      <if test="row.carNum != null">
        car_num = #{row.carNum,jdbcType=VARCHAR},
      </if>
      <if test="row.brand != null">
        brand = #{row.brand,jdbcType=VARCHAR},
      </if>
      <if test="row.guidePrice != null">
        guide_price = #{row.guidePrice,jdbcType=DECIMAL},
      </if>
      <if test="row.produceTime != null">
        produce_time = #{row.produceTime,jdbcType=CHAR},
      </if>
      <if test="row.carType != null">
        car_type = #{row.carType,jdbcType=VARCHAR},
      </if>
    </set>
    <if test="example != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update t_car
    set id = #{row.id,jdbcType=BIGINT},
      car_num = #{row.carNum,jdbcType=VARCHAR},
      brand = #{row.brand,jdbcType=VARCHAR},
      guide_price = #{row.guidePrice,jdbcType=DECIMAL},
      produce_time = #{row.produceTime,jdbcType=CHAR},
      car_type = #{row.carType,jdbcType=VARCHAR}
    <if test="example != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.powernode.mybatis.pojo.Car">
    update t_car
    <set>
      <if test="carNum != null">
        car_num = #{carNum,jdbcType=VARCHAR},
      </if>
      <if test="brand != null">
        brand = #{brand,jdbcType=VARCHAR},
      </if>
      <if test="guidePrice != null">
        guide_price = #{guidePrice,jdbcType=DECIMAL},
      </if>
      <if test="produceTime != null">
        produce_time = #{produceTime,jdbcType=CHAR},
      </if>
      <if test="carType != null">
        car_type = #{carType,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.powernode.mybatis.pojo.Car">
    update t_car
    set car_num = #{carNum,jdbcType=VARCHAR},
      brand = #{brand,jdbcType=VARCHAR},
      guide_price = #{guidePrice,jdbcType=DECIMAL},
      produce_time = #{produceTime,jdbcType=CHAR},
      car_type = #{carType,jdbcType=VARCHAR}
    where id = #{id,jdbcType=BIGINT}
  </update>
</mapper>
<?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="com.powernode.mybatis.mappers.CarMapper">
  <resultMap id="BaseResultMap" type="com.powernode.mybatis.pojo.Car">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="car_num" jdbcType="VARCHAR" property="carNum" />
    <result column="brand" jdbcType="VARCHAR" property="brand" />
    <result column="guide_price" jdbcType="DECIMAL" property="guidePrice" />
    <result column="produce_time" jdbcType="CHAR" property="produceTime" />
    <result column="car_type" jdbcType="VARCHAR" property="carType" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <select id="selectByPage" resultType="car">
    select * from t_car limit #{startIndex},#{pageSize}
  </select>
  <select id="selectAllByPageHelper" resultType="Car">
    select * from t_car
  </select>
  <sql id="Update_By_Example_Where_Clause">
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    id, car_num, brand, guide_price, produce_time, car_type
  </sql>
  <select id="selectByExample" parameterType="com.powernode.mybatis.pojo.CarExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from t_car
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from t_car
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from t_car
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <delete id="deleteByExample" parameterType="com.powernode.mybatis.pojo.CarExample">
    delete from t_car
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.powernode.mybatis.pojo.Car">
    insert into t_car (id, car_num, brand, 
      guide_price, produce_time, car_type
      )
    values (#{id,jdbcType=BIGINT}, #{carNum,jdbcType=VARCHAR}, #{brand,jdbcType=VARCHAR}, 
      #{guidePrice,jdbcType=DECIMAL}, #{produceTime,jdbcType=CHAR}, #{carType,jdbcType=VARCHAR}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.powernode.mybatis.pojo.Car">
    insert into t_car
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="carNum != null">
        car_num,
      </if>
      <if test="brand != null">
        brand,
      </if>
      <if test="guidePrice != null">
        guide_price,
      </if>
      <if test="produceTime != null">
        produce_time,
      </if>
      <if test="carType != null">
        car_type,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=BIGINT},
      </if>
      <if test="carNum != null">
        #{carNum,jdbcType=VARCHAR},
      </if>
      <if test="brand != null">
        #{brand,jdbcType=VARCHAR},
      </if>
      <if test="guidePrice != null">
        #{guidePrice,jdbcType=DECIMAL},
      </if>
      <if test="produceTime != null">
        #{produceTime,jdbcType=CHAR},
      </if>
      <if test="carType != null">
        #{carType,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.powernode.mybatis.pojo.CarExample" resultType="java.lang.Long">
    select count(*) from t_car
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    update t_car
    <set>
      <if test="row.id != null">
        id = #{row.id,jdbcType=BIGINT},
      </if>
      <if test="row.carNum != null">
        car_num = #{row.carNum,jdbcType=VARCHAR},
      </if>
      <if test="row.brand != null">
        brand = #{row.brand,jdbcType=VARCHAR},
      </if>
      <if test="row.guidePrice != null">
        guide_price = #{row.guidePrice,jdbcType=DECIMAL},
      </if>
      <if test="row.produceTime != null">
        produce_time = #{row.produceTime,jdbcType=CHAR},
      </if>
      <if test="row.carType != null">
        car_type = #{row.carType,jdbcType=VARCHAR},
      </if>
    </set>
    <if test="example != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update t_car
    set id = #{row.id,jdbcType=BIGINT},
      car_num = #{row.carNum,jdbcType=VARCHAR},
      brand = #{row.brand,jdbcType=VARCHAR},
      guide_price = #{row.guidePrice,jdbcType=DECIMAL},
      produce_time = #{row.produceTime,jdbcType=CHAR},
      car_type = #{row.carType,jdbcType=VARCHAR}
    <if test="example != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.powernode.mybatis.pojo.Car">
    update t_car
    <set>
      <if test="carNum != null">
        car_num = #{carNum,jdbcType=VARCHAR},
      </if>
      <if test="brand != null">
        brand = #{brand,jdbcType=VARCHAR},
      </if>
      <if test="guidePrice != null">
        guide_price = #{guidePrice,jdbcType=DECIMAL},
      </if>
      <if test="produceTime != null">
        produce_time = #{produceTime,jdbcType=CHAR},
      </if>
      <if test="carType != null">
        car_type = #{carType,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.powernode.mybatis.pojo.Car">
    update t_car
    set car_num = #{carNum,jdbcType=VARCHAR},
      brand = #{brand,jdbcType=VARCHAR},
      guide_price = #{guidePrice,jdbcType=DECIMAL},
      produce_time = #{produceTime,jdbcType=CHAR},
      car_type = #{carType,jdbcType=VARCHAR}
    where id = #{id,jdbcType=BIGINT}
  </update>
</mapper>
package com.powernode.mybatis.mappers;

import com.powernode.mybatis.pojo.Car;
import com.powernode.mybatis.pojo.CarExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;

public interface CarMapper {
    List<Car> selectAllByPageHelper();
    long countByExample(CarExample example);

    int deleteByExample(CarExample example);

    int deleteByPrimaryKey(Long id);

    List<Car> selectByPage(@Param("startIndex") int startIndex,@Param("pageSize") int pageSize);

    int insert(Car row);

    int insertSelective(Car row);

    List<Car> selectByExample(CarExample example);

    Car selectByPrimaryKey(Long id);

    int updateByExampleSelective(@Param("row") Car row, @Param("example") CarExample example);

    int updateByExample(@Param("row") Car row, @Param("example") CarExample example);

    int updateByPrimaryKeySelective(Car row);

    int updateByPrimaryKey(Car row);
}

package com.powernode.mybatis.mappers;

import com.powernode.mybatis.pojo.Car;
import com.powernode.mybatis.pojo.CarExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;

public interface CarMapper {
    List<Car> selectAllByPageHelper();
    long countByExample(CarExample example);

    int deleteByExample(CarExample example);

    int deleteByPrimaryKey(Long id);

    List<Car> selectByPage(@Param("startIndex") int startIndex,@Param("pageSize") int pageSize);

    int insert(Car row);

    int insertSelective(Car row);

    List<Car> selectByExample(CarExample example);

    Car selectByPrimaryKey(Long id);

    int updateByExampleSelective(@Param("row") Car row, @Param("example") CarExample example);

    int updateByExample(@Param("row") Car row, @Param("example") CarExample example);

    int updateByPrimaryKeySelective(Car row);

    int updateByPrimaryKey(Car row);
}
package com.powernode.mybatis.Test;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.powernode.mybatis.mappers.CarMapper;
import com.powernode.mybatis.pojo.Car;
import com.powernode.mybatis.utils.SqlSessionUtils;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class Test
{
    private static final Logger logger = LoggerFactory.getLogger(Test.class);
    @org.junit.Test
    public void selectByPage()
    {
        SqlSession sqlSession = SqlSessionUtils.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        int pageNum = 1;
        int pageSize = 3;
        int startIndex = (pageNum - 1) * pageSize;
        List<Car> cars =  mapper.selectByPage(startIndex,pageSize);
        cars.forEach(car -> logger.info(car.toString()));
    }
    @org.junit.Test
    public void selectAllByPageHelper()
    {
        SqlSession sqlSession = SqlSessionUtils.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        //执行DQL语句之前一定要开启pageHelper
        int pageNum = 1;
        int pageSize = 2;
        PageHelper.startPage(pageNum,pageSize);
        List<Car> cars = mapper.selectAllByPageHelper();
        //navigatePages导航页数,有多少填多少即可
        //封装我们的分页信息,可以获取更多分页信息
        PageInfo<Car> Info = new PageInfo<>(cars,5);
        logger.info(Info.toString());
//PageInfo的输出信息
//PageInfo{pageNum=1, pageSize=2, size=2, startRow=1, endRow=2,
//total=4, pages=2, list=Page{count=true, pageNum=1, pageSize=2,
//startRow=0, endRow=2, total=4, pages=2, reasonable=false,
//pageSizeZero=false}[com.powernode.mybatis.pojo.Car@1601e47, com.powernode.mybatis.pojo.Car@3bffddff],
//prePage=0, nextPage=2, isFirstPage=true, isLastPage=false, hasPreviousPage=false,
//hasNextPage=true, navigatePages=5, navigateFirstPage=1, navigateLastPage=2, navigatepageNums=[1, 2]}
        cars.forEach(car -> logger.info(car.toString()));
        SqlSessionUtils.close(sqlSession);
    }
}

package com.powernode.mybatis.Test;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.powernode.mybatis.mappers.CarMapper;
import com.powernode.mybatis.pojo.Car;
import com.powernode.mybatis.utils.SqlSessionUtils;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class Test
{
    private static final Logger logger = LoggerFactory.getLogger(Test.class);
    @org.junit.Test
    public void selectByPage()
    {
        SqlSession sqlSession = SqlSessionUtils.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        int pageNum = 1;
        int pageSize = 3;
        int startIndex = (pageNum - 1) * pageSize;
        List<Car> cars =  mapper.selectByPage(startIndex,pageSize);
        cars.forEach(car -> logger.info(car.toString()));
    }
    @org.junit.Test
    public void selectAllByPageHelper()
    {
        SqlSession sqlSession = SqlSessionUtils.openSession();
        CarMapper mapper = sqlSession.getMapper(CarMapper.class);
        //执行DQL语句之前一定要开启pageHelper
        int pageNum = 1;
        int pageSize = 2;
        PageHelper.startPage(pageNum,pageSize);
        List<Car> cars = mapper.selectAllByPageHelper();
        //navigatePages导航页数,有多少填多少即可
        //封装我们的分页信息,可以获取更多分页信息
        PageInfo<Car> Info = new PageInfo<>(cars,5);
        logger.info(Info.toString());
//PageInfo的输出信息
//PageInfo{pageNum=1, pageSize=2, size=2, startRow=1, endRow=2,
//total=4, pages=2, list=Page{count=true, pageNum=1, pageSize=2,
//startRow=0, endRow=2, total=4, pages=2, reasonable=false,
//pageSizeZero=false}[com.powernode.mybatis.pojo.Car@1601e47, com.powernode.mybatis.pojo.Car@3bffddff],
//prePage=0, nextPage=2, isFirstPage=true, isLastPage=false, hasPreviousPage=false,
//hasNextPage=true, navigatePages=5, navigateFirstPage=1, navigateLastPage=2, navigatepageNums=[1, 2]}
        cars.forEach(car -> logger.info(car.toString()));
        SqlSessionUtils.close(sqlSession);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
关于使用 MyBatis 分页插件 PageHelper,可以参考以下步骤: 1. 在项目的 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> ``` 2. 在 MyBatis 配置文件中配置 PageHelper 插件: ```xml <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <!--数据库类型,mybatis会根据不同的数据库使用不同的分页策略--> <property name="helperDialect" value="mysql"/> <!--参数映射,不设置该参数时使用默认值--> <property name="reasonable" value="true"/> </plugin> </plugins> ``` 3. 在代码中使用 PageHelper.startPage 方法设置分页查询: ```java // pageNum:当前页码,pageSize:每页显示条数 PageHelper.startPage(pageNum, pageSize); // 调用 MyBatis 的查询方法,会自动实现分页 List<User> userList = userDao.queryUsers(); ``` 4. 在页面中显示分页信息和查询结果: ```html <!-- pageInfoPageHelper 返回的分页信息 --> <nav> <ul class="pagination"> <li><a href="${pageInfo.prePage}">上一页</a></li> <li><a href="${pageInfo.nextPage}">下一页</a></li> </ul> </nav> <!-- userList 是上述查询返回的结果 --> <table> <thead> <tr> <th>编号</th> <th>用户名</th> <th>邮箱</th> <th>手机号码</th> </tr> </thead> <tbody> <c:forEach items="${userList}" var="user"> <tr> <td>${user.id}</td> <td>${user.username}</td> <td>${user.email}</td> <td>${user.mobile}</td> </tr> </c:forEach> </tbody> </table> ``` 希望这份简单的使用说明能够帮到你,如果有其它问题,请随时提出。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旧约Alatus

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值