使用oracle的分页实现。
1.1.1 dao
修改SysuserMapperCustom.xml中findSysuserList添加分页支持。
Oracle分页需要起始和结束下标,需要根据当前页码、每页显示数量、总条数(符合查询条件记录数)
使用PageQuery.java计算起始和结束的下标:
PageQuery为自己封装的工具类
使用方法:
New构造pageQuery对象,调用setPageParams方法,传入总条数、每页显示数量、当前页码,该对象中的属性PageQuery_start和PageQuery_end计算出了起始和结束的下标。
将PageQuery对象传入mybatis,将此对象作为包装对象的属性。
public classSysuserQueryVo {
private SysuserCustom sysuserCustom;
//分页参数
private PageQuery pageQuery;
Oracle分页sql:
分页mapper.xml
分页头和分页的尾。
<!-- 用户查询 -->
<select id="findSysuserList" parameterType="yycg.base.pojo.vo.SysuserQueryVo"
resultType="yycg.base.pojo.vo.SysuserCustom">
<!-- 分页头 -->
<if test="pageQuery!=null">
selectpage_2.*
from(select page_1.*, rownum page_num
from(
</if>
select* from (
selectSYSUSER.id,
SYSUSER.userid,
SYSUSER.username,
SYSUSER.groupid,
sysuser.USERSTATE,
SYSUSER.sysid,
decode(SYSUSER.Groupid,
'1',
(selectmc
fromuserjd where
id= sysuser.sysid),
'2',
(selectmc from userjd where
id=
sysuser.sysid),
'3',
(selectmc from useryy where id =
sysuser.sysid),
'4',
(selectmc from usergys where id = sysuser.sysid)
)
sysmc
from
SYSUSER
)sysuser
<where>
<include refid="query_sysuser_where" />
</where>
<!-- 分页尾 -->
<if test="pageQuery!=null">
)page_1
<![CDATA[
where rownum <=${pageQuery.PageQuery_end}) page_2
where page_2.page_num >=${pageQuery.PageQuery_start}
]]>
</if>
</select>
总条数:符合条件记录数
在mapper.xml中创建一个select,返回总条数
<!-- 查询列表的总记录数 -->
<select id="findSysuserCount" parameterType="yycg.base.pojo.vo.SysuserQueryVo"
resultType="int">
selectcount(*) from (
selectSYSUSER.id,
SYSUSER.userid,
SYSUSER.username,
SYSUSER.groupid,
sysuser.USERSTATE,
SYSUSER.sysid,
decode(SYSUSER.Groupid,
'1',
(selectmc
fromuserjd where
id= sysuser.sysid),
'2',
(selectmc from userjd where
id=
sysuser.sysid),
'3',
(selectmc from useryy where id =
sysuser.sysid),
'4',
(selectmc from usergys where id = sysuser.sysid)
)
sysmc
from
SYSUSER
)sysuser
<where>
<include refid="query_sysuser_where" />
</where>
</select>
1.1.2 service
public interfaceUserService {
//根据条件查询用户列表
publicList<SysuserCustom> findSysuserList (SysuserQueryVo sysuserQueryVo) throws Exception;
//根据条件查询记录总数
public int findSysuserCount (SysuserQueryVosysuserQueryVo) throwsException;
1.1.3 action
调用service执行分页查询,准备PageQuery对象数据(包括起始和结束下标)。
先调用service查询出总数。
Datagrid加载数据列表时,固定向action方法传两个分页参数:
当前页码和每页显示个数
Action分页查询代码:
//用户查询页面的结果集
//最终DataGridResultInfo通过@ResponseBody将java对象转成json
@RequestMapping("/queryuser_result")
public @ResponseBodyDataGridResultInfo queryuser_result(
SysuserQueryVosysuserQueryVo,
int page,//页码
int rows//每页显示个数
)throws Exception{
//非空校验
sysuserQueryVo= sysuserQueryVo!=null?sysuserQueryVo:new SysuserQueryVo();
//查询列表的总数
int total = userService.findSysuserCount(sysuserQueryVo);
PageQuerypageQuery = newPageQuery();
pageQuery.setPageParams(total,rows, page);
sysuserQueryVo.setPageQuery(pageQuery);
//分页查询,向sysuserQueryVo中传入pageQuery
List<SysuserCustom>list = userService.findSysuserList(sysuserQueryVo);
DataGridResultInfodataGridResultInfo = new DataGridResultInfo();
//填充 total
dataGridResultInfo.setTotal(total);
//填充 rows
dataGridResultInfo.setRows(list);
return dataGridResultInfo;
}