这段时间一直在使用Spring+JqGrid+Mybatis在做开发,写了一个模板类,和大家分享下
第一个是Mybatis的sql例子,红色字体不要修改,放那就行
<!-- 查询全部的Code信息,支持检索和分页 --> <select id="codeDtlMasterList" parameterType="CodeDtlVo" resultType="HashMap"> <choose> <when test="docount == 1 "> select to_char(count(*)) AS RECORDS from ( </when> </choose> <choose> <when test="dopage == 1 "> SELECT * FROM ( SELECT E.*, ROWNUM PAGE FROM ( </when> </choose> select CM.MAST_CODE_ID,CMT.DESCRIPTION, CM.CODE_LENGTH from OPM_CODE_MASTER CM, OPM_CODE_MASTER_TR CMT where 1=1 and CM.USAGE = 'Y' and CM.MAST_CODE_ID = CMT.MAST_CODE_ID and CMT.LANG_GB = #{lang} order by ${sidx} ${sord} <choose> <when test="docount == 1"> ) e </when> </choose> <choose> <when test="dopage == 1 "> ) e)WHERE PAGE > #{numPageStart} AND PAGE <= #{numPageEnd} </when> </choose> </select>
第二个是VO
CodeDtlVo:
public class CodeDtlVo extends LangVo{
private String mast_code_id="";
}
LangVo:
public class LangVo extends JqGridComDataVo implements LangVoIdentify{
private String soLange="";//源语言类型
private String toLange="";//目标语言类型
}
lang标识接口
public interface LangVoIdentify extends JqGridVoIdentify{
}
JqGrid抽象公共父类,放入了公共参数
public abstract class JqGridComDataVo extends JqGridVo{
private String lang;
}
JqGrid 抽象父类
public abstract class JqGridVo implements JqGridVoIdentify{
//前端必传
private String rows; //每页显示的行数
private String page; //当前页数
private String sidx; //排序的字段
private String sord; //排序的方式
//前端选传
private String oper; //操作类型
//系统自动
private String docount; //是否查询全部
private String dopage; //是否查询分页
//操作标识位
private String stateFlag="";
private Integer numPageStart;
private Integer numPageEnd;
}
JqGriddVo标识接口
public interface JqGridVoIdentify {
}
Dao标识接口
public interface JqGridDaoIdentify {
}
第三个是service
package egovframework.rte.cmmn.jqgrid.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.springframework.stereotype.Service;
import egovframework.rte.cmmn.jqgrid.intface.JqGridDaoIdentify;
import egovframework.rte.cmmn.jqgrid.intface.JqGridVoIdentify;
/**
* Jq公共类,自动封装成JqGrid数据
*/
@Service("jqGridService")
public class JqGridService {
/**
* @param jqGridVo
* @return
*/
public HashMap<String,Object> jqGridList(JqGridDaoIdentify jqGridDao,String methodName,JqGridVoIdentify jqGridVo,List<String> lstr,boolean noPage) {
HashMap<String,Object> resultHashMap = new HashMap<String, Object>();
String page = "1";
String rows = "0";
String records = "0";
try{
//查询总条数
this.setFieldValueByName("docount", jqGridVo, "1");
this.setFieldValueByName("dopage", jqGridVo, "0");
List<HashMap<String, String>> jqGridListTotal = this.executeMethod(jqGridDao, methodName, jqGridVo);//获得数量结果集
//查询分页数据
this.setFieldValueByName("docount", jqGridVo, "0");
if(noPage){
this.setFieldValueByName("dopage", jqGridVo, "0");
}else{
this.setFieldValueByName("dopage", jqGridVo, "1");
page = this.getFieldValueByName("page", jqGridVo).toString();
rows = this.getFieldValueByName("rows", jqGridVo).toString();
this.setFieldValueByName("numPageStart", jqGridVo, (Integer.parseInt(page)-1) * Integer.parseInt(rows));
this.setFieldValueByName("numPageEnd", jqGridVo, (Integer.parseInt(page)) * Integer.parseInt(rows));
}
List<HashMap<String, String>> jqGridListPage = this.executeMethod(jqGridDao, methodName, jqGridVo);
//存入数据
List<HashMap<String, Object>> rowsData = new ArrayList<HashMap<String,Object>>();
if(lstr.size()>0){
for (HashMap<String, String> jqGridItem : jqGridListPage) {
//自动把第一个值存入主键,前台必须和它对应key:true
HashMap<String, Object> rowsDataItem = new HashMap<String, Object>();
rowsDataItem.put(lstr.get(0).toLowerCase(), jqGridItem.get(lstr.get(0).toUpperCase()));
HashMap<String, Object> cellData = new HashMap<String, Object>();
for (String item : lstr) {
cellData.put(item.toLowerCase(), jqGridItem.get(item.toUpperCase()));
}
rowsDataItem.put("cell", cellData);
rowsData.add(rowsDataItem);
}
}
//生成前台json数据
records = jqGridListTotal.get(0).get("RECORDS");
resultHashMap.put("records", records);//总记录数
resultHashMap.put("page", page);//当前页数
resultHashMap.put("total", Math.ceil(Double.parseDouble(records) / Integer.parseInt(rows)));
resultHashMap.put("rows", rowsData);
return resultHashMap;
} catch (Exception e)
{
e.printStackTrace();
return null;
}
}
public HashMap<String,Object> jqGridList(JqGridDaoIdentify jqGridDao,String methodName,JqGridVoIdentify jqGridVo,List<String> lstr) {
return this.jqGridList(jqGridDao,methodName,jqGridVo,lstr,false);
}
private List<HashMap<String, String>> executeMethod(JqGridDaoIdentify jqGridDao,String methodName,JqGridVoIdentify jqGridVo){
try{
Class<?>[] d = jqGridDao.getClass().getInterfaces();
java.lang.reflect.Method method = d[0].getMethod(methodName, jqGridVo.getClass());
Object value = method.invoke(jqGridDao,jqGridVo);
@SuppressWarnings("unchecked")
List<HashMap<String, String>> tmpResult = (List<HashMap<String, String>>)value;
return tmpResult;
} catch (Exception e)
{
e.printStackTrace();
return null;
}
}
/**
* @param fieldName
* @param obj
* @return
* 获得VO的值
*/
private Object getFieldValueByName(String fieldName, Object obj){
try{
String firstLetter = fieldName.substring(0, 1).toUpperCase();
String getter = "get" + firstLetter + fieldName.substring(1);
java.lang.reflect.Method method = obj.getClass().getMethod(getter, new Class[] {});
Object value = method.invoke(obj, new Object[] {});
return value;
} catch (Exception e)
{
e.printStackTrace();
return null;
}
}
/**
* @param fieldName
* @param obj
* @param value
* 设置VO的值
*/
private void setFieldValueByName(String fieldName, Object obj,Object value){
try{
String firstLetter = fieldName.substring(0, 1).toUpperCase();
String setter = "set" + firstLetter + fieldName.substring(1);
java.lang.reflect.Method method = obj.getClass().getMethod(setter, value.getClass());
method.invoke(obj, value);
} catch (Exception e)
{
e.printStackTrace();
}
}
}
第四个是使用
自己的VO,必须继承自JqGridVo,或者实现JqGridVoIdentify这个接口
自己的DaoIntface 必须继承自JqGridDaoIdentify
引用服务
//JqGrid服务
@Resource(name="jqGridService")
private JqGridService jqGridService;
Param构造:
List<String> Param = new ArrayList<String>();
Param.add("MAST_CODE_ID");
//默认第一个为KEY
Param.add("STATEFLAG");
Param.add("DISLPAY_ORDER");
Param.add("CODE_CLASS");
调用(自己的DaoIntface,要调用DaoIntface的方法,vo参数,Key参数,是否使用分页)
jqGridService.jqGridList(codeMasterDaoIntface,"codeMasterList",codeMasterVo,Param,true);