
select * from
tableName ......
limit (pageNum - 1) * pageSize, pageSize
<select id="selectActivityByConditionForPage" parameterType="map" resultMap="BaseResultMap">
select
a.id,u1.name as owner,a.name,a.start_date,a.end_date,a.cost,a.description,a.create_time,u2.name as create_by,a.edit_time,u3.name as edit_by
from
tbl_activity a
join
tbl_user u1 on a.owner=u1.id
join
tbl_user u2 on a.create_by=u2.id
left join
tbl_user u3 on a.edit_by=u3.id
<where>
<if test="name!=null and name!=''">
and a.name like '%' #{name} '%'
if>
<if test="owner!=null and owner!=''">
and u1.name like '%' #{owner} '%'
if>
<if test="startDate!=null and startDate!=''">
and a.start_date>=#{startDate}
if>
<if test="endDate!=null and endDate!=''">
and a.end_date<=#{endDate}
if>
where>
order by a.create_time desc
limit #{beginNo},#{pageSize}
select>
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelperartifactId>
<version>5.3.2version>
dependency>
typeAliases标签下⾯进⾏配置:
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"/>
plugins>
@Test
public void testPageHelper() throws Exception{
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
SqlSession sqlSession = sqlSessionFactory.openSession();
CarMapper mapper = sqlSession.getMapper(CarMapper.class);
// 开启分⻚
PageHelper.startPage(2, 2);
// 执⾏查询语句
List<Car> cars = mapper.selectAll();
// 获取分⻚信息对象
PageInfo<Car> pageInfo = new PageInfo<>(cars, 5);
System.out.println(pageInfo);
}
PageInfo{
pageNum=2, pageSize=2, size=2, startRow=3, endRow=4, total=6, pages=3,list=Page{count=true, pageNum=2, pageSize=2, startRow=2, endRow=4, total=6, pages=3, reasonable=false, pageSizeZero=false}
[Car{id=86, carNum='1234', brand='丰⽥霸道', guidePrice=50.5, produceTime='2020-1011', carType='燃油⻋'},
Car{id=87, carNum='1234', brand='丰⽥霸道', guidePrice=50.5, produceTime='2020-10-11', carType='燃油⻋'}],
prePage=1, nextPage=3, isFirstPage=false, isLastPage=false, hasPreviousPage=true, hasNextPage=true,navigatePages=5, navigateFirstPage=1, navigateLastPage=3,navigatepageNums=[1, 2, 3]
}
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelper-spring-boot-starterartifactId>
<version>2.1.0version>
dependency>