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

springmvc+datatables+mybatis分页

2015-11-07 16:43 447 查看
1.分页封装类

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.context.request.WebRequest;

public class DataTablePageUtil<T> {

private int draw; // 第几次请求
private int start = Constants.IDISPLAYSTART;// 起止位置
private int length = Constants.DISPLAYLENGTH; // 数据长度
private List<T> data;
private int recordsTotal; // 数据总记录数
private int recordsFiltered; // 过滤数

private Map<String, Object> condition = new HashMap<String, Object>(); // 查询条件

public DataTablePageUtil() {

}

public DataTablePageUtil(WebRequest request) {
if (StringUtils.isNotBlank(request.getParameter("aoData"))) {
JSONArray jsonarray = JSONArray.fromObject(request
.getParameter("aoData"));
for (int i = 0; i < jsonarray.size(); i++) {
JSONObject obj = (JSONObject) jsonarray.get(i);
if (obj.get("name").equals("sEcho"))
this.setDraw(obj.getInt("value"));

if (obj.get("name").equals("iDisplayStart"))
this.setStart(obj.getInt("value"));

if (obj.get("name").equals("iDisplayLength"))
this.setLength(obj.getInt("value"));
}
}
if (StringUtils.isNotBlank(request.getParameter("searchData"))
&& request.getParameter("searchData").startsWith("[")) {
JSONArray searchData = JSONArray.fromObject(request
.getParameter("searchData"));
if (searchData != null && searchData.size() > 0) {
for (int i = 0; i < searchData.size(); i++) {
JSONObject jsonObject = searchData.getJSONObject(i);
if (StringUtils.isNotBlank(jsonObject.getString("value"))) {
this.condition.put(jsonObject.getString("name"),
jsonObject.get("value"));
}
}
}
}
}

public int getDraw() {
return draw;
}

public void setDraw(int draw) {
this.draw = draw;
}

public int getStart() {
return start;
}

public void setStart(int start) {
this.start = start;
}

public int getLength() {
return length;
}

public void setLength(int length) {
this.length = length;
}

public List<T> getData() {
return data;
}

public void setData(List<T> data) {
this.data = data;
}

public Map<String, Object> getCondition() {
return condition;
}

public void setCondition(Map<String, Object> condition) {
this.condition = condition;
}

public int getRecordsTotal() {
return recordsTotal;
}

public void setRecordsTotal(int recordsTotal) {
this.recordsTotal = recordsTotal;
}

public int getRecordsFiltered() {
return recordsFiltered;
}

public void setRecordsFiltered(int recordsFiltered) {
this.recordsFiltered = recordsFiltered;
}

}


2.分页controller层

/**
* 查询二级分类信息
* @param request
* @return
*/
@RequestMapping(value = "/returnTwoSortMan")
@ResponseBody
public DataTablePageUtil<Map<String, Object>> findTwoSortLevel(WebRequest request) {
DataTablePageUtil<Map<String, Object>> dataTable = new DataTablePageUtil<Map<String, Object>>(request);
return sortManService.findSortTwoLevelInfo(dataTable);
}


3.分页service层

/**
* 获取分页数据
* @param dataTable
* @return
*/
public DataTablePageUtil<Map<String, Object>> findBrands(DataTablePageUtil<Map<String, Object>> dataTable) {
dataTable.setData(brandMapper.findBrands(dataTable));
dataTable.setRecordsTotal(brandMapper.findBrandCounts(dataTable));
dataTable.setRecordsFiltered(dataTable.getRecordsTotal());
return dataTable;
}


4.mybatis层

<select id="findTwoSort" parameterType="com.hxqc.oms.util.DataTablePageUtil" resultType="Map">
select 字段 from 表 where 1=1
<if test="param.condition !=null">
<if test="param.condition.对应查询字段 !=null and param.condition.对应查询字段 != ''">
and 字段 like
CONCAT('%',#{param.condition.对应查询字段},'%')
</if>
<if test="param.condition.对应查询字段 !=null and param.condition.对应查询字段 != 0">
and parent_id = #{param.condition.对应查询字段}
</if>
<if test="param.condition.对应查询字段 !=null">
and deleted = #{param.condition.对应查询字段}
</if>
</if>
order by 字段 limit #{param.start},#{param.length}
</select>


总结:相对于Struts分页感觉还是这个分页好用(表现在查询方面,方便)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: