entity
page 实体类 用户来 controller 进行返回
package com.big.data.lab.entity;
import java.util.List;
/**
* 分页实体类
* 分页 查询 时候 返回的 类型
* */
@SuppressWarnings("rawtypes")
public class Page {
private List rows;//存储 分页 所查询的 集合
private long total;// 总行数
private Long allPage;// 所有页数
private Integer newPage;// 当前页数
public Page(){}
public Page(List rows, long total) {
super();
this.rows = rows;
this.total = total;
}
public Long getAllPage() {
return allPage;
}
public void setAllPage(Long allPage) {
this.allPage = allPage;
}
public Integer getNewPage() {
return newPage;
}
public void setNewPage(Integer newPage) {
this.newPage = newPage;
}
public List getRows() {
return rows;
}
public void setRows(List rows) {
this.rows = rows;
}
public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
}
@Override
public String toString() {
return "Page [rows=" + rows + ", total=" + total + "]";
}
}
Query 查询基础类
package com.big.data.lab.entity;
/*
* 类描述:查询基础类 查询 实体类 可继承
* @auther wangmx
* @create 2017/8/11 0011
*/
public class Query {
/** 要排序的字段名 */
protected String sort;
/** 排序方式: desc \ asc */
protected String order = "";
/** 获取一页行数 */
protected int limit;
/** 获取的页码 */
protected int page;
/** 起始记录 */
protected int offset;
public String getSort() {
return sort;
}
public void setSort(String sort) {
if(sort==null){
return ;
}
String orderSort = "";
for( int i = 0 ; i<sort.length(); i++ ){
char a = sort.charAt(i);
String b = ""+a;
if( a>64 && a<91 ){ //大写字母的ASCLL码取值范围
if(orderSort.equals("")){
orderSort = orderSort + b.toLowerCase();
}else{
orderSort = orderSort + "_" + b.toLowerCase();
}
}else{
orderSort = orderSort + b;
}
}
this.sort = orderSort;
}
public String getOrder() {
return order;
}
public void setOrder(String order) {
this.order = order;
}
public int getLimit() {
return limit;
}
public void setLimit(int limit) {
this.limit = limit;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getOffset() {
return (this.page-1)*limit;
}
public void setOffset(int offset) {
this.offset = offset;
}
}
QueryPersonalCenter 实体类
package com.big.data.lab.entity;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import com.fasterxml.jackson.annotation.JsonFormat;
/**
* 用来分页查询的 实体
* @author wangmx
*
*/
public class QueryPersonalCenter extends Query{
private String id;
private String userId;
private String heahPhoto;
private String articleId;
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
private Date collectTime;
private String stringDateTime;
private Integer state;
private Content content;//展示 右边 我的收藏 的文章对象
private String articleTitle;//用于 搜索 文章的 title 查询时候使用
private String valueParent;//查询 我的收藏 左边 展示框 一级树
private String valueChild;//查询 我的收藏 左边 展示框 二级树
private String articTitle;//查询 我的收藏 左边 展示框 文章的名字
public String getStringDateTime(){
String dateStr = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(collectTime);
return dateStr;
}
public String getArticleId() {
return articleId;
}
public void setArticleId(String articleId) {
this.articleId = articleId;
}
public String getArticTitle() {
return articTitle;
}
public void setArticTitle(String articTitle) {
this.articTitle = articTitle;
}
public String getValueParent() {
return valueParent;
}
public void setValueParent(String valueParent) {
this.valueParent = valueParent;
}
public String getValueChild() {
return valueChild;
}
public void setValueChild(String valueChild) {
this.valueChild = valueChild;
}
public String getArticleTitle() {
return articleTitle;
}
public void setArticleTitle(String articleTitle) {
this.articleTitle = articleTitle;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getHeahPhoto() {
return heahPhoto;
}
public void setHeahPhoto(String heahPhoto) {
this.heahPhoto = heahPhoto;
}
public Date getCollectTime() {
return collectTime;
}
public void setCollectTime(Date collectTime) {
this.collectTime = collectTime;
}
public Integer getState() {
return state;
}
public void setState(Integer state) {
this.state = state;
}
public Content getContent() {
return content;
}
public void setContent(Content content) {
this.content = content;
}
@InitBinder
public void initBinder(WebDataBinder binder) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(true);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
}
dao
// t 按照 自己的 实体进行更改
/**
* 分页查询
* @param queryModel 查询条件
* */
public List<T> findByPage(Q queryModel);
/**
* 统计
* @param queryModel 查询条件
* @return int
* */
public int count(Q queryModel);
mapper
<resultMap type="com.base.entity.PersonalCenter" id="FunctionMap">
<id property="funId" column="FUN_ID"/>
<result property="funCode" column="FUN_CODE"/>
<result property="funName" column="FUN_NAME"/>
<result property="menuId" column="MENU_ID"/>
<result property="orderNo" column="ORDER_NO"/>
<result property="updateDate" column="UPDATE_DATE"/>
</resultMap>
<!--分页查询-->
<select id="findByPage" parameterType="com.base.entity.QueryPersonalCenter" resultMap="FunctionMap">
SELECT FUN_ID,FUN_CODE,FUN_NAME,MENU_ID,ORDER_NO,UPDATE_DATE FROM t_function
WHERE 1=1
<if test="funCode!=null and funCode!='' " >
AND FUN_CODE = #{funCode}
</if>
<if test="funName!=null and funName!='' " >
AND FUN_NAME = #{funName}
</if>
<if test="menuId!=null and menuId!='' " >
AND MENU_ID = #{menuId}
</if>
<if test="orderNo!=null and orderNo!='' " >
AND ORDER_NO = #{orderNo}
</if>
<if test="updateDate!=null and updateDate!='' " >
AND UPDATE_DATE = #{updateDate}
</if>
<if test="sort!= null">
order by ${sort} ${order} <!-- sort 排序条件 order 排序 DESC 顺序还是 倒叙 -->
</if>
limit #{offset},#{limit}
</select>
<!--统计-->
<select id="count" parameterType="com.base.entity.QueryPersonalCenter" resultType="int">
SELECT count(*) FROM t_function
WHERE 1=1
<if test="funCode!=null and funCode!='' " >
AND FUN_CODE = #{funCode}
</if>
<if test="funName!=null and funName!='' " >
AND FUN_NAME = #{funName}
</if>
<if test="menuId!=null and menuId!='' " >
AND MENU_ID = #{menuId}
</if>
<if test="orderNo!=null and orderNo!='' " >
AND ORDER_NO = #{orderNo}
</if>
<if test="updateDate!=null and updateDate!='' " >
AND UPDATE_DATE = #{updateDate}
</if>
</select>
service
/**
* 分页查询
* @param queryModel 查询条件
* */
public Page findByPage(Q queryModel){
List<T> list = getDao().findByPage(queryModel);
int count = getDao().count(queryModel);
return new Page(list, count);
}
controller
方法一:
/**
* 案例二
* 分页查询
* @param entity
* @return
*/
@RequestMapping(value="/page")//queryPersonalCenterContent 改成 page qpcc
public String queryPersonalCenterContent(Model model,QueryPersonalCenter entity){//初始 时候展示
try {
User user1 = UserInfoUtil.getUser();//获取 登录用户的信息
if (entity.getPage() == 0) {
entity.setPage(1);
}
entity.setLimit(10);//给 默认的 行数 10行
Page page = service.queryPersonalCenterContent(entity);//total 页数 rows 行数
Long a = page.getTotal() / 10;
if (page.getTotal() % 10 > 0) {
a = a + 1;
}
page.setAllPage(a);// 获取 总页码数
page.setNewPage(entity.getPage());//获取当前页数
model.addAttribute("page", page);
} catch (Exception e) {
return "forward:/grzx/queryUser";
}
return "forward:/grzx/queryUser";
}
方法二:
/**
* 案例 一
* 功能描述:获取分页的数据 根据 自己 需要自己进行 编写
* @param entity
* @return
*/
@RequestMapping(value = "/list",method = RequestMethod.POST)
public Map<String,Object> list(@RequestBody Q entity){
Map<String,Object> result = new HashMap<String, Object>();
try{
Page page = getService().findByPage(entity);
result.put("totalCount",page.getTotal());
result.put("result",page.getRows());
}catch(Exception e){
e.printStackTrace();
}
return result;
}