使用Map代替没完没了的POJO

    xiaoxiao2021-03-25  134

    说明:该篇文章中示例工程所使用架构为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;i<args.length;i++){ try { String value = request.getParameter(args[i]); String temp = value == null ? value : new String(value.getBytes("ISO-8859-1"),"UTF-8"); params[i] = "".equals(temp) ? null : temp; } catch (Exception e) { e.printStackTrace(); } } return params; } /** * 以字典形式小批量获取参数 * @param args * @return */ public static Map<String, Object> getParametersWithMap(HttpServletRequest request, String...args){ Map<String, Object> map = new HashMap<String, Object>(); 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中使用代码

    Map<String, Object> map = CommonUtils.getParametersWithMap( getRequest(), "size", "index", "placeName");

    二、ibaits输入输出参数均可以使用Map,所以我们可以在这里直接以Map形式获取参数,后面可以直接将该Map当作参数对象使用,从而省去大量的为承载参数而产生的pojo类,省去了管理与组装pojo类的麻烦,同时也省去在ibaits配置文件中编写parameterMap和resultMap,更具有灵活性

    Action层代码

    public String getTempAttPerson() { result.clear(); //以Map形式获取参数 Map<String, Object> map = CommonUtils.getParametersWithMap( getRequest(), "batchNo", "cusName", "studentNo", "size", "index"); //因为此处需要分页,所以要对参数进行一些处理 CommonUtils.putStartAndEnd(map); //调用Service层方法,并获取List<Map<String, Object>>形式的返回值 List<Map<String, Object>> list = attanceManager.getTempAttItemData(map); result.put("list", list); result.put("page", map); return SUCCESS; }

    Service层代码

    @Override public List<Map<String, Object>> getTempAttItemData(Map<String, Object> 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 @Repository public class GeneralDaoImpl extends IBatisGenericDao implements GeneralDao { @Resource(name = "sqlMapClient") private SqlMapClient sqlMapClient; @PostConstruct public void initSqlMapClient(){ super.setSqlMapClient(sqlMapClient); } //分页获取数据的通用方法 @SuppressWarnings("unchecked") public List<Map<String, Object>> getDataWithList(Map<String, Object> 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<String, Object> getDataWithObject(Map<String, Object> map) { return (Map<String, Object>) this.getSqlMapClientTemplate().queryForObject((String) map.get("dataSql"), map); } @Override String getSqlMapNamespace() { return null; } }

    ibaits配置文件

    <!-- 直接使用java.util.HashMap作为输入输出参数 --> <select id="getTempAttItemData" resultClass="java.util.HashMap" parameterClass="java.util.HashMap"> <include refid="paginationStart" /> select t1.customer_industry_no studentNo, t2.customername cusName, t3.organize_name orgName from temporary_att t1, tb_customer_info t2, tbbaseorganize t3 where t1.temporary_id = #batchNo# and t2.customerindustryno = t1.customer_industry_no and t3.organize_id = t2.customerdept <isNotNull prepend="and" property="cusName"> t2.customername like '%$cusName$%' </isNotNull> <isNotNull prepend="and" property="studentNo"> t1.customer_industry_no = #studentNo# </isNotNull> <include refid="paginationEnd" /> </select>

    页面示例代码

    //此处的数据即是Map中的数据,名称是和sql中对应的,oracle中默认全部是大写,所以此处字段名称全部是大写的 function initAttPersonGrid(){ var batchNo = $("#hid_batch").val(); attPersonGrid = $("#grid_att_person").exfgrid({ url : path + "/attendance/getTempAttPerson.action", /*caption : "<label></label>",*/ 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; } }] }); }
    转载请注明原文地址: https://ju.6miu.com/read-8813.html

    最新回复(0)