博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用Map代替没完没了的POJO
阅读量:2058 次
发布时间:2019-04-29

本文共 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;i
getParametersWithMap(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中使用代码

Map
map = CommonUtils.getParametersWithMap( getRequest(), "size", "index", "placeName");

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

Action层代码

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

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/

你可能感兴趣的文章
集成测试(一)—— 使用PHP页面请求Spring项目的Java接口数据
查看>>
使用Maven构建的简单的单模块SSM项目
查看>>
Intellij IDEA使用(十四)—— 在IDEA中创建包(package)的问题
查看>>
Redis学习笔记(四)—— redis的常用命令和五大数据类型的简单使用
查看>>
重新夺回对 /etc/resolv.conf 的控制权
查看>>
突破 DockerHub 限制,全镜像加速服务
查看>>
使用 Sealos + Longhorn 部署 KubeSphere v3.0.0
查看>>
10小时,这回一次搞定 Kafka 源码!
查看>>
运维总监怒怼开发:你真的需要K8S吗?
查看>>
Prometheus hang 住问题定位解决
查看>>
别看 DNS 污染闹得欢,现在我用 CoreDNS 将它拉清单
查看>>
百度为什么掉队了
查看>>
Containerd 中的 Snapshot 到底是个什么鬼?
查看>>
Dockerd 资源泄露怎么办
查看>>
高性能 Nginx HTTPS 调优 - 如何为 HTTPS 提速 30%
查看>>
在 Kubernetes 中部署高可用 Harbor 镜像仓库
查看>>
容器网络一直在颤抖,罪魁祸首竟然是 ipvs 定时器
查看>>
阿里宣布拆中台,首当其冲就是优化数据中台架构?
查看>>
Cilium 源码解析:Node 之间的健康探测(health probe)机制
查看>>
前几天是谁说 WireGuard 不香的?看我今天怎么怼你
查看>>