您的位置:首页 > 编程语言 > Java开发

java后台的数据层数据组织

2016-01-15 23:19 441 查看
1、DataGrid的数据组织—service在事物内

(1)action

public void getExchange() {
this.writeJson(orderService.getExchange(exchange));
}


(2)service

StringBuffer data = new StringBuffer();
Map<String, Object> params = new HashMap<String, Object>();
data.append(" from Texchange as t where t.isValid=1");
if (ex.getUserId() != null) {
data.append(" and t.userId=:userId");
params.put("userId", ex.getUserId());
}
String count = "select count(*)" + data.toString();
if (ex.getSort() != null) {
data.append(" order by" + ex.getSort() + " " + ex.getOrder());
}
try {
List<Texchange> tes = dao.find(data.toString(), params, ex.getPage(), ex.getRows());
List<Exchange> es = new ArrayList<Exchange>();
for (int i = 0; i < tes.size(); i++) {
Texchange te = tes.get(i);
Exchange e = new Exchange();
BeanUtils.copyProperties(te, e);
es.add(e);
}
DataGrid dataGrid = new DataGrid();
dataGrid.setRows(es);
dataGrid.setTotal(dao.count(count, params));
return dataGrid;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}


(3)数据:

{
"rows": [
{
"consignee": "个广告",
"consigneeAddress": "安徽省安庆市枞阳县ggg",
"consigneeTel": "18365409608",
"datetime": "2016-01-13 23:07:34",
"exchangeNum": 134,
"id": 3,
"isValid": true,
"productId": 1,
"productName": "前列腺按摩",
"userId": 21,
"userName": "yamadv"
}
}


2、HashMap

////HashMap<String, Object>的组织数据


(1)action:

int typeId = Integer.parseInt(this.getRequest().getParameter("typeId"));
List<Tactivityslide> plist = this.activitySlideService.getAllActivitySlide(typeId);
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject = new JSONObject();
for (int i = 0; i < plist.size(); i++) {
Tactivityslide ta = plist.get(i);
jsonObject.clear();
jsonObject.put("id", ta.getId());
jsonObject.put("name", ta.getActivityName());
jsonObject.put("imgUrl", ta.getImgUrl());
int tieId = ta.getTargetId();
jsonObject.put("targetId", tieId);
if (typeId == 2) {
Tfatie t = bbsService.getSingleFaTie(tieId).get(0);
jsonObject.put("theme", t.getTheme());
jsonObject.put("content", t.getContent());
jsonObject.put("fatieTime", t.getFatieTime());
try {
String imgurls = "";
List<Ttieziimg> tbi = this.bbsService.getTieImagesByTieId(t.getId());
if (tbi.size() == 0) {
jsonObject.put("imgUrls", "no");
} else {
for (int j = 0; j < tbi.size(); j++) {
if (j == tbi.size() - 1) {
imgurls += tbi.get(j).getTieImg();
} else {
imgurls += tbi.get(j).getTieImg() + ",";
}
}
jsonObject.put("imgUrls", imgurls);
}
} catch (Exception e) {
e.printStackTrace();
}
try {
Tuser u = this.userService.getSingleUser(t.getUserId()).get(0);
jsonObject.put("userId", u.getId());
jsonObject.put("nickName", u.getNickName());
jsonObject.put("userLever", u.getCommuniteAccess());
jsonObject.put("headImgUrl", u.getHeaderImgUrl());
} catch (Exception e) {
e.printStackTrace();
}
}
jsonArray.add(jsonObject);
}
HashMap<String, Object> jsMap = new HashMap<String, Object>();
jsMap.put("rows", jsonArray);
this.JSObj = JSONObject.fromObject(jsMap);
this.writeJson(JSObj);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: