分析:
grid 数据列表要求返回的json格式
{data:[{key1=value1,key2=value2},{key1=value3,key2=value4}}],total:count}
其中data代表grid的列表当前显示所有行数据,total代表的是返回的列表的总记录条数
grid 列表所要求的这种格式的数据对应的java数据类型为Map
类型分别为data:List
springmvc的responbody注解很容器实现,然后就是后台数据的分页查询,同时还有条件查询了,
这一部分miniui的示例代码里面已经为我们准备的有例子,我只是在这基础上进行了修改。
代码示例:
Controller层:
/**
* 获取客户列表
* @return
* @throws ServiceException
*/
@RequestMapping("/getCustoms")
@ResponseBody
public HashMap getCustoms(HttpServletRequest request) throws ServiceException{
// 处理查询参数
String key = request.getParameter("key");
StringBuilder sb = new StringBuilder("SELECT * FROM custom WHERE 1 = 1");
if(!StringUtil.isNullOrEmpty(key)){
sb.append(" AND name ="+key);
}
//分页
int pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
int pageSize = Integer.parseInt(request.getParameter("pageSize"));
//字段排序
String sortField = request.getParameter("sortField");
String sortOrder = request.getParameter("sortOrder");
HashMap gridData = dao.getResultByPage(CustomVO.class, sb.toString(), pageIndex, pageSize, sortField, sortOrder);
System.err.println(gridData);
return gridData;
}
Dao 层数据接口:
/**
* 根据sql语句获取实体列表
* @return
*/
@SuppressWarnings("rawtypes")
public List findListVO(Class type,String sql, Object... values) throws ServiceException;
/**
* 根据sql语句查询实体并封装为List
Dao 层数据实现类:
@SuppressWarnings("unchecked")
@Override
public List findListVO(Class type,String sql,Object...values) throws ServiceException{
try {
if (!checkParamsEqual(sql, values, "?")) {
throw new ServiceException(AppConstants.ERRORCODE_PARAMERROR, "参数有误", null);
}
return super.getSessionFactory().getCurrentSession().
createSQLQuery(sql).setResultTransformer(Transformers.aliasToBean(type)).list();
}
catch (Exception e) {
throw new ServiceException(AppConstants.ERRORCODE_DATABASE, "数据库执行异常", e);
}
}
@SuppressWarnings("unchecked")
@Override
public List> findListMapVO(Class type,String sql, Object... values) throws ServiceException {
try {
List> list = new ArrayList>();
for(Object entity:findListVO(type, sql,values)){
T t = (T)entity;
Map map = new HashMap();
for(Field field : type.getDeclaredFields()){
if(!Modifier.isStatic(field.getModifiers())){
String fieldName = field.getName();
Object obj = type.getMethod("get"+StringUtil.fistUpper(field.getName())).invoke(t);
map.put(fieldName, StringUtil.objectToString(obj));
}
}
list.add(map);
}
return list;
} catch (Exception e) {
throw new ServiceException(AppConstants.ERRORCODE_DATABASE, "数据库执行异常", e);
}
}
@Override
public HashMap getResultByPage(Class type,String sql, int pageIndex, int pageSize, String sortField,
String sortOrder) throws ServiceException {
try {
StringBuilder sb = new StringBuilder(sql);
// 排序
if (!StringUtil.isNullOrEmpty(sortField)){
if (!"DESC".equals(sortOrder)){
sortOrder = "ASC";
}
sb.append(" ORDER BY "+sortField+" "+sortOrder);
}
else {
sb.append(" ORDER BY id");
}
List> dataListAll = this.findListMapVO(type, sb.toString());
// 分页处理
int start = pageIndex * pageSize;
int end = start + pageSize;
List> dataResultList = new ArrayList>();
for (int i = 0;i < dataListAll.size(); i++){
Map map = dataListAll.get(i);
if (map == null) continue;
if (start <= i && i < end){
dataResultList.add(map);
}
}
// 返回grid需要的数据
HashMap result = new HashMap();
result.put("data", dataResultList);
result.put("total", dataListAll.size());
return result;
} catch (Exception e) {
throw new ServiceException(AppConstants.ERRORCODE_DATABASE, "数据库执行异常", e);
}
}