分页:利于数据的展示
分页方法:
- limit limit语法limit(起始位置,要显示的页面条数)
- 分页插件:pageHelper
分页变量:
- pageSize:每页展示条目数
- currentPage:当前第几页
- totalPages:总页数
- totalCount:总记录数
- totalPages=totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1
核心类:pageInfo
范例:分页
1、接口
//分页的方法
public List<User> getUserListByPage(@Param("userName") String userName,@Param("from")Integer from,@Param("pageSize")Integer pageSize);
2、映射
<!-- 条件查询分页 -->
<select id="getUserListByPage" resultMap="userResult">
select * from smbms_user where 1=1
<if test="userName!=null and userName!=''">
and userName like concat('%',#{userName},'%')
</if>
order by creationDate desc
<if test="from!=null">
limit #{from}
</if>
<if test="pageSize!=null">
, #{pageSize}
</if>
</select>
测试
@Test
public void test23(){
SqlSession sqlSession = SqlSessionUtil.creatSqlSession();
List<User> list=sqlSession.getMapper(UserMapper.class).getUserListByPage("赵",1,2);
for(User u:list) {
System.out.println(u.getUserName()+"\t"+u.getUserPassword());
}
// 关闭sqlSession
SqlSessionUtil.closSqleSession(sqlSession);
}
范例:查询指定条件的总记录数
1、接口
//查询指定条件的总记录数
public int rowCount(@Param("userName")String userName);
2、映射
!-- 条件查询总记录数 -->
<select id="rowCount" resultType="Integer">
select count(1) from smbms_user where 1=1
<if test="userName!=null">
and userName like concat('%',#{userName},'%')
</if>
</select>
测试
@Test
public void test24(){
SqlSession sqlSession = SqlSessionUtil.creatSqlSession();
Integer count=sqlSession.getMapper(UserMapper.class).rowCount("赵");
System.out.println("总记录数:"+count);
// 关闭sqlSession
SqlSessionUtil.closSqleSession(sqlSession);
}