<?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); } }
本文含有隐藏内容,请 开通VIP 后查看