springboot分页功能
导入pageHelper依赖
参照网站:https://blog.csdn.net/Inmaturity_7/article/details/107870151
< ! -- 引入pageHelper依赖-- >
< dependency>
< groupId> com. github. pagehelper< / groupId>
< artifactId> pagehelper- spring- boot- starter< / artifactId>
< version> 1.2 .5 < / version>
< / dependency>
Mapper层配置
List< PositionPageBean> queryAllPosition ( Map< String, Object> map) ;
< ! -- sql块-- >
< sql id= "allPosition" >
team_name, company. company_user_id, company_name, company_field, company_curr, company_scale,
position_name, position_city, position_money, position_experience, position_education, position_walfare
< / sql>
< select id= "queryAllPosition" resultType= "com.weicai.dao.domain.PositionPageBean" parameterType= "hashmap" >
SELECT DISTINCT < include refid= "allPosition" / >
FROM weicai_company company, weicai_position position, weicai_team team
WHERE company. company_user_id = team. company_user_id
and company. id = position. position_company_id
and position. position_name like #{ search_input}
< where>
< if test= "salary_range!=null" >
and position. position_money= #{ salary_range}
< / if >
< if test= "work_experience!=null" >
and position. position_experience= #{ work_experience}
< / if >
< if test= "minimum_education!=null" >
and position. position_education= #{ minimum_education}
< / if >
< if test= "work_type!=null" >
and position. position_nature= #{ work_type}
< / if >
< if test= "develop_Time!=null" >
and str_to_date ( position. position_time, '%Y-%m-%d %H:%i:%s' ) > date_add ( now ( ) ,
interval #{ develop_Time} day)
< / if >
< if test= "work_city!=null and work_city!='全国'" >
and position. position_city= #{ work_city}
< / if >
< / where>
< / select>
List< PositionPageBean> queryAllCompany ( Map< String, Object> map) ;
< select id= "queryAllCompany" resultType= "com.weicai.dao.domain.PositionPageBean" parameterType= "hashmap" >
SELECT DISTINCT < include refid= "allPosition" / >
FROM weicai_company company, weicai_position position, weicai_team team
< where>
company. company_user_id = team. company_user_id
and company. id = position. position_company_id
and company. company_name
like #{ search_input}
< if test= "salary_range!=null" >
and position. position_money= #{ salary_range}
< / if >
< if test= "work_experience!=null" >
and position. position_experience= #{ work_experience}
< / if >
< if test= "minimum_education!=null" >
and position. position_education= #{ minimum_education}
< / if >
< if test= "work_type!=null" >
and position. position_nature= #{ work_type}
< / if >
< if test= "develop_Time!=null" >
and str_to_date ( position. position_time, '%Y-%m-%d %H:%i:%s' ) > date_add ( now ( ) ,
interval #{ develop_Time} day)
< / if >
< if test= "work_city!=null and work_city!='全国'" >
and position. position_city= #{ work_city}
< / if >
< / where>
< / select>
Service业务层配置
PageInfo< User> queryAllByPageHelper ( int pageNum, int PageSize) ;
public PageInfo< PositionPageBean> searchInfo ( String searchType_select, Map< String, Object> map, int pageNum, int pageSize) ;
@Override
public PageInfo< User> queryAllByPageHelper ( int pageNum, int pageSize) {
PageHelper. startPage ( pageNum, pageSize) ;
List< User> users = userMapper. queryAllUser ( ) ;
for ( User user : users) {
List< Label> labels = userMapper. queryLabels ( user. getUserId ( ) ) ;
user. setLableList ( labels) ;
}
PageInfo< User> pageInfo = new PageInfo < User> ( users) ;
return pageInfo;
}
@Override
public PageInfo< PositionPageBean> searchInfo ( String searchType_select, Map< String, Object> map, int pageNum, int pageSize) {
PageHelper. startPage ( pageNum, pageSize) ;
List< PositionPageBean> positionPageBeans = null;
int number = 0 ;
if ( searchType_select!= null&& searchType_select!= "" ) {
number = Integer. parseInt ( searchType_select) ;
}
System. out. println ( "map = " + map) ;
if ( map. get ( "search_input" ) == null|| searchType_select== null|| map. get ( "search_input" ) == "" ) {
System. out. println ( "查询主页信息..." ) ;
positionPageBeans = companyPositionMapper. queryAllCompanyPosition ( ) ;
} else if ( number== 1 ) {
System. out. println ( "按职位查询信息..." ) ;
System. out. println ( "map=" + map) ;
positionPageBeans = companyPositionMapper. queryAllPosition ( map) ;
} else if ( number== 2 ) {
System. out. println ( "按公司查询信息..." ) ;
positionPageBeans = companyPositionMapper. queryAllCompany ( map) ;
}
if ( positionPageBeans!= null) {
for ( PositionPageBean positionPageBean : positionPageBeans) {
List< WeicaiLable> labels = companyPositionMapper. queryLabels ( positionPageBean. getCompanyUserId ( ) ) ;
positionPageBean. setWeicaiLableList ( labels) ;
}
}
PageInfo< PositionPageBean> pageInfo = new PageInfo < PositionPageBean> ( positionPageBeans) ;
return pageInfo;
}
Controller控制层配置
@RequestMapping ( "/search" )
public String searchInfo ( @RequestParam ( required = false ) String searchType_select,
@RequestParam ( required = false ) String search_input,
@RequestParam ( defaultValue = "1" ) int currPage,
ModelMap map,
@RequestParam ( required = false ) String type,
@RequestParam ( required = false ) String require) {
System. out. println ( "type = " + type) ;
System. out. println ( "require = " + require) ;
Map< String, Object> parameterMap= new HashMap < String, Object> ( ) ;
if ( searchType_select!= null&& ! searchType_select. equals ( "" ) ) {
searchTypeSelect= searchType_select;
}
if ( search_input!= null&& ! "" . equals ( search_input) ) {
searchInput= "%" + search_input+ "%" ;
}
parameterMap. put ( "search_input" , searchInput) ;
if ( type!= null&& require!= null&& ! "" . equals ( type) && ! "" . equals ( require) ) {
parameterMap. put ( type, require) ;
map. put ( "type" , type) ;
map. put ( "require" , require) ;
}
System. out. println ( "searchTypeSelect = " + searchTypeSelect) ;
System. out. println ( "searchInput = " + searchInput) ;
PageInfo< PositionPageBean> pageBeanPageInfo = companyPositionService. searchInfo ( searchTypeSelect, parameterMap, currPage, 3 ) ;
int totalPage = pageBeanPageInfo. getPages ( ) ;
int beginPage;
int endPage;
if ( totalPage< 10 ) {
beginPage= 1 ;
endPage = totalPage;
} else {
beginPage = currPage - 5 ;
endPage = currPage + 4 ;
if ( beginPage < 1 ) {
beginPage= 1 ;
endPage = 10 ;
}
if ( endPage> totalPage) {
endPage = totalPage;
beginPage = endPage - 9 ;
}
}
System. out. println ( "职位/公司----->" + searchTypeSelect) ;
System. out. println ( "当前页----->" + currPage) ;
map. put ( "searchTypeSelect" , searchTypeSelect) ;
map. put ( "search_input" , search_input) ;
System. out. println ( "pageBeanPageInfo.getList() = " + pageBeanPageInfo. getList ( ) ) ;
map. put ( "positionPageBeans" , pageBeanPageInfo. getList ( ) ) ;
map. put ( "currPage" , currPage) ;
map. put ( "totalPage" , totalPage) ;
map. put ( "beginPage" , beginPage) ;
map. put ( "endPage" , endPage) ;
map. put ( "totalRecord" , pageBeanPageInfo. getTotal ( ) ) ;
return "/czp/list" ;
}
@RequestParam(required = false) String name// 如果传值为空,不报错
@RequestParam(defaultValue = "1") // 如果未传值默认值为1
前端页面(仅用于显示所有)
< % -- 分页显示 start-- % >
< div class = "Pagination myself" >
< input type= "text" value= "${currPage}" id= "currPage" hidden/ >
< input type= "text" value= "${totalPage}" id= "totalPage" hidden/ >
< a class = "current" id= "firstPage" onclick= "first_page_button()" > 首页< / a>
< span class = "current" id= "prevPage" onclick= "prev_page_button()" > 上一页 < / span>
< % -- 分页页码显示-- % >
< c: forEach var = "i" begin= "${beginPage}" step= "1" end= "${endPage}" >
< a id= "pageSelectID_${i}" class = "current" onclick= "qty_page_button(${i})" > ${ i} < / a>
< / c: forEach>
< a class = "current" id= "nextPage" onclick= "next_page_button()" > 下一页 < / a>
< a class = "current" id= "endPagess" onclick= "end_page_button()" > 尾页< / a>
< / div>
< % -- 分页显示 end-- % >
前端a标签样式
< % -- 点击事件和样式渲染 start-- % >
< script type= "text/javascript" >
var currPageSelectId = "#pageSelectID_" + ${ currPage} ;
var idBeSelected = ${ currPage} ;
var totalPages = ${ totalPage} ;
$ ( currPageSelectId) . toggleClass ( function ( ) {
if ( $ ( currPageSelectId) . hasClass ( 'current' ) ) {
$ ( currPageSelectId) . removeClass ( 'current' ) ;
$ ( currPageSelectId) . attr ( "disabled" , true ) ;
$ ( currPageSelectId) . css ( "pointer-events" , "none" ) ;
}
if ( idBeSelected== 1 ) {
$ ( "#firstPage" ) . removeClass ( 'current' ) ;
$ ( "#firstPage" ) . attr ( "disabled" , true ) ;
$ ( "#firstPage" ) . css ( "pointer-events" , "none" ) ;
$ ( "#prevPage" ) . removeClass ( 'current' ) ;
$ ( "#prevPage" ) . attr ( "disabled" , true ) ;
$ ( "#prevPage" ) . css ( "pointer-events" , "none" ) ;
} else if ( totalPages== idBeSelected) {
$ ( "#endPagess" ) . removeClass ( 'current' ) ;
$ ( "#endPagess" ) . attr ( "disabled" , true ) ;
$ ( "#endPagess" ) . css ( "pointer-events" , "none" ) ;
$ ( "#nextPage" ) . removeClass ( 'current' ) ;
$ ( "#nextPage" ) . attr ( "disabled" , true ) ;
$ ( "#nextPage" ) . css ( "pointer-events" , "none" ) ;
}
} ) ;
function first_page_button ( ) {
var currPage = $ ( "#currPage" ) . val ( ) ;
if ( currPage<= 1 ) {
return ;
}
window. location. href= "${basePath}czp/showList" ;
}
function prev_page_button ( ) {
var currPage = $ ( "#currPage" ) . val ( ) ;
if ( currPage<= 1 ) {
return ;
}
window. location. href= "${basePath}czp/showList?currPage=" + ( currPage- 1 ) ;
}
function next_page_button ( ) {
var currPage = Number ( $ ( "#currPage" ) . val ( ) ) ;
var totalPage = Number ( $ ( "#totalPage" ) . val ( ) ) ;
if ( currPage>= totalPage) {
return ;
}
window. location. href= "${basePath}czp/showList?currPage=" + ( currPage+ 1 ) ;
}
function end_page_button ( ) {
var currPage = Number ( $ ( "#currPage" ) . val ( ) ) ;
var totalPage = Number ( $ ( "#totalPage" ) . val ( ) ) ;
if ( currPage>= totalPage) {
return ;
}
window. location. href= "${basePath}czp/showList?currPage=" + totalPage;
}
function qty_page_button ( currPage) {
var totalPage = Number ( $ ( "#totalPage" ) . val ( ) ) ;
if ( currPage< 1 || currPage> totalPage) {
return ;
}
window. location. href= "${basePath}czp/showList?currPage=" + currPage;
}
< / script>
< % -- 点击事件和样式渲染 end-- % >
if(currPage>=totalPage){
return;
}
window.location.href="${basePath}czp/showList?currPage="+totalPage;
}
// 按指定页查询
function qty_page_button(currPage){
var totalPage = Number($("#totalPage").val());
if(currPage<1 || currPage>totalPage){
return;
}
window.location.href="${basePath}czp/showList?currPage="+currPage;
}
</ script>
<%--点击事件和样式渲染 end--%>