您的位置:首页 > 编程语言 > Java开发

分页查询简单实现(Freemarker+SpringMVC+Mybatis)

2017-08-10 10:59 851 查看
Page实体类:保存当前页面数据

public class Page {
private int current;//当前页
private int end;//尾页
private int numPerPage;//每页记录数
private int totalCount;//总记录数
private List<Item> items;//当前页数据
public int getCurrent() {
return current;
}
public void setCurrent(int current) {
this.current = current;
}
public int getEnd() {
return end;
}
public void setEnd(int end) {
this.end = end;
}
public int getNumPerPage() {
return numPerPage;
}
public void setNumPerPage(int numPerPage) {
this.numPerPage = numPerPage;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
this.end = (int)Math.ceil(totalCount/numPerPage);//计算尾页
}
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}

}


Mapper文件:编写sql语句

<?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.mapper.ItemMapper">
<resultMap type="Item" id="itemResult">
<id property="id" column="lItemId"/>
<result property="item" column="vItem"/>
</resultMap>
<!--分页查询-->
<select id="findByPage" resultMap="itemResult">
SELECT * FROM item
LIMIT #{0},#{1}
</select>
<!--返回总记录数目-->
<select id="getItemsCount" resultType="int">
SELECT COUNT(*) FROM item
</select>
</mapper>


分页业务实现:

@Service
public class ItemServiceImpl implements ItemService{

@Autowired
private ItemMapper itemMapper;

/*
* pageNum:当前页码
* numPerPage:每页记录数目
*/
@Override
public Page findByPage(int pageNum, int numPerPage) {
Page page = new Page();
page.setNumPerPage(numPerPage);
page.setCurrent(pageNum);

int totalCount = itemMapper.getItemsCount();
page.setTotalCount(totalCount);//设置总记录数

int start = (pageNum - 1)*numPerPage;
List list = itemMapper.findByPage(start, numPerPage);
page.setItems(list);//设置当前页面数据

return page;
}

}


Controller实现:

@Controller
@RequestMapping("/items/")
public class ItemController {

@Autowired
private ItemService itemService;

@RequestMapping("/page/{number}")
public ModelAndView queryByPage(@PathVariable("number") int pageNum){
ModelAndView mav = new ModelAndView("page");
Page page = itemService.findByPage(pageNum, 3);
mav.addObject("page", page);
return mav;
}
}


freemarker页面:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
.current{
text-decoration:none;
color:red;
}
</style>
</head>
<body>
<h2>分页查询</h2>
<#list page.items>
<ul>
<#items as item>
<li>${item.id}.${item.item}</li>
</#items>
</ul>
</#list>
<#if page.totalCount !=0>
<a href="
<#if page.current != 1>
${request.contextPath}/items/page/${page.current-1}
<#else>
#
</#if>
">上一页</a>
<#assign start = 1>
<!--尾页数<=5-->
<#if page.end lte 5>
<#list start..page.end as p>
<a class="<#if page.current == p>current</#if>" href="${request.contextPath}/items/page/${p}">${p}</a>
</#list>
<#else>
<#assign start = 1 + 4 * ((page.current/4)?ceiling - 1)>
<#if page.end - start gt 4>
<#list start..*5 as p>
<a class="<#if page.current == p>current</#if>" href="${request.contextPath}/items/page/${p}">${p}</a>
</#list>
<a href="#">...</a>
<a href="${request.contextPath}/items/page/${page.end}">${page.end}</a>
<#else>
<a href="#">...</a>
<#list start..page.end as p>
<a class="<#if page.current == p>current</#if>" href="${request.contextPath}/items/page/${p}">${p}</a>
</#list>
</#if>
</#if>
<a href="
<#if page.current != page.end>
${request.contextPath}/items/page/${page.current+1}
<#else>
#
</#if>
">下一页</a>
</#if>
</body>
</html>


Spring mvc相关配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<mvc:annotation-driven/>

<!-- 扫描生成controller组件 -->
<context:component-scan base-package="com.controller"/>

<!-- 配置根视图 -->
<mvc:view-controller path="/" view-name="redirect:/items/page/1"/>

<!-- freemarker配置 -->
<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer" >
<property name="templateLoaderPath" value="/WEB-INF/ftl/"/>
<property name="freemarkerSettings">
<props>
<!-- 设置标签类型 -->
<prop key="tag_syntax">auto_detect</prop>
<prop key="classic_compatible">true</prop>
<!-- 检查模板更新时间间隔,默认5s -->
<prop key="template_update_delay">1</prop>
<prop key="defaultEncoding">UTF-8</prop>
<prop key="url_escaping_charset">UTF-8</prop>
<prop key="locale">zh_CN</prop>
<prop key="boolean_format">true,false</prop>
<!-- 时间格式化 -->
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="date_format">yyyy-MM-dd</prop>
<prop key="time_format">HH:mm:ss</prop>
<prop key="number_format">0.######</prop>
<prop key="whitespace_stripping">true</prop>
</props>
</property>
</bean>
<!-- freemarker视图解析 -->
<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="order" value="0"/>
<property name="suffix" value=".ftl"/>
<property name="contentType" value="text/html; charset=UTF-8"/>
<property name="requestContextAttribute" value="request"/>
</bean>

</beans>


完整代码见:https://github.com/crazylai1996/querybypage
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mybatis 分页查询