使用Map代替没完没了的POJO

使用Map代替没完没了的POJO

原创  2017年03月09日 09:46:27

说明:该篇文章中示例工程所使用架构为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;
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

Action中使用代码

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

二、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;
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

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);
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

持久层代码,使用通用的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;
    }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

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>
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

页面示例代码

//此处的数据即是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;
            }
        }]
    });
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值