本文共 4965 字,大约阅读时间需要 16 分钟。
说明:该篇文章中示例工程所使用架构为Spring+Struts2+ibaits+Oracle
一、使用通用的获取参数的方法,以List或Map形式获取参数
通用获取参数代码
/** * 以数组形式小批量获取参数 * @param args * @return */public static String[] getParametersWithArray(HttpServletRequest request, String...args){ String[] params = new String[args.length]; for(int i=0;igetParametersWithMap(HttpServletRequest request, String...args){ Map map = new HashMap (); for(String arg : args){ String temp; try { String value = request.getParameter(arg); temp = value == null ? value : new String(value.getBytes("ISO-8859-1"),"UTF-8"); map.put(arg, "".equals(temp) ? null : temp); } catch (Exception e) { e.printStackTrace(); } } return map;}
Action中使用代码
Mapmap = CommonUtils.getParametersWithMap( getRequest(), "size", "index", "placeName");
二、ibaits输入输出参数均可以使用Map,所以我们可以在这里直接以Map形式获取参数,后面可以直接将该Map当作参数对象使用,从而省去大量的为承载参数而产生的pojo类,省去了管理与组装pojo类的麻烦,同时也省去在ibaits配置文件中编写parameterMap和resultMap,更具有灵活性
Action层代码
public String getTempAttPerson() { result.clear(); //以Map形式获取参数 Mapmap = CommonUtils.getParametersWithMap( getRequest(), "batchNo", "cusName", "studentNo", "size", "index"); //因为此处需要分页,所以要对参数进行一些处理 CommonUtils.putStartAndEnd(map); //调用Service层方法,并获取List
Service层代码
@Overridepublic List> getTempAttItemData(Map map) { //将ibaits配置文件的命名域及sql的id装进Map中 //countSql为获取数据总条数的sql,因为此处需要分页,dataSql是获取数据的sql map.put("countSql", "TemporaryAttance.getTempAttItemDataCount"); map.put("dataSql", "TemporaryAttance.getTempAttItemData"); return generalDao.getDataWithList(map);}
持久层代码,使用通用的Dao,从而避免编写大量重复的Dao方法
//编写一个通用的Dao@Repositorypublic class GeneralDaoImpl extends IBatisGenericDao implements GeneralDao { @Resource(name = "sqlMapClient") private SqlMapClient sqlMapClient; @PostConstruct public void initSqlMapClient(){ super.setSqlMapClient(sqlMapClient); } //分页获取数据的通用方法 @SuppressWarnings("unchecked") public List> getDataWithList(Map map) { try { SqlMapClientTemplate client = this.getSqlMapClientTemplate(); //使用之前放进Map的countSql获取数据总条数 int total = (Integer) client.queryForObject((String) map.get("countSql"), map); map.put("total", total); if(!(total > 0)){ return Collections.EMPTY_LIST; } } catch (Exception e) { e.printStackTrace(); } //分页获取数据 return this.getSqlMapClientTemplate().queryForList((String) map.get("dataSql"), map); } //获取单个数据的通用方法 @SuppressWarnings("unchecked") public Map getDataWithObject(Map map) { return (Map ) this.getSqlMapClientTemplate().queryForObject((String) map.get("dataSql"), map); } @Override String getSqlMapNamespace() { return null; }}
ibaits配置文件
页面示例代码
//此处的数据即是Map中的数据,名称是和sql中对应的,oracle中默认全部是大写,所以此处字段名称全部是大写的function initAttPersonGrid(){ var batchNo = $("#hid_batch").val(); attPersonGrid = $("#grid_att_person").exfgrid({ url : path + "/attendance/getTempAttPerson.action", /*caption : "",*/ serialnumber : { enable : true, width : 30 }, sizelist : [50, 100, 150, 200],// footertext : "当前共{0}条信息", page : { size : 50, index : 0, batchNo : batchNo }, checkbox : true, checkbox : 1, columns : [{ name : "STUDENTNO", indexdata : "STUDENTNO", align : "center", caption : "学号", width : 60, render : function(v) { return !v ? "-" : v; } } ,{ name : "CUSNAME", indexdata : "CUSNAME", caption : "姓名", align : "center", width : 50, render : function(v) { return !v ? "-" : v; } } ,{ name : "ORGNAME", indexdata : "ORGNAME", caption : "组织", align : "center", width : 50, render : function(v) { return !v ? "-" : v; } }] });}
转载地址:http://tvtlf.baihongyu.com/