将多表数据封装为一个实体对象,需要vo类。
一、dao:
<span style="font-size:18px;">public List<DeptApplyVO> findAll() {
String sql ="SELECT * FROM dept_apply d JOIN dept_apply_info a ON d.id=a.dept_apply_id";
Query query = getSession().createSQLQuery(sql);
List list = query.list();
List<DeptApplyVO> list2 = new ArrayList<DeptApplyVO>();
Iterator it = list.iterator();
while (it.hasNext()){
DeptApplyVO vo = new DeptApplyVO();
Object[] obj = (Object[]) it.next();
for (int i=0;i<obj.length;i++){
vo.setId(Long.parseLong(obj[0].toString()));
vo.setApplyName(obj[1].toString());
vo.setPhone(obj[2].toString());
vo.setMobile(obj[3].toString());
try {
vo.setApplyTime(new SimpleDateFormat("yyyy-MM-dd").parse(obj[4].toString()));
vo.setEffeTime(new SimpleDateFormat("yyyy-MM-dd").parse(obj[5].toString()));
} catch (ParseException e) {
e.printStackTrace();
}
list2.add(vo);
}
}
return list2;
}</span>
二、action中处理返回json:
下面是封装了部分字段值的json结果:
<span style="font-size:18px;">{
"rows": [
{
"applyInfoId": "5B3BD148BE9F476CBD9A23AAEA3F06F2",
"applyName": "周芳",
"applyReason": "测试",
"applyTime": 1416499200000,
"applyUserId": "8288",
"effeTime": 1415203200000,
"filePath": "D:\\tools\\apache-tomcat-6.0.41\\webapps\\InerviceSystem\\upload",
"id": 3,
"mobile": "18954621",
"phone": "031-8745",
"userDeptId": "2006"
},
{
"$ref": "$.rows[0]"
},
{
"$ref": "$.rows[0]"
},
{
"$ref": "$.rows[0]"
},
{
"$ref": "$.rows[0]"
},
{
"$ref": "$.rows[0]"
},
{
"$ref": "$.rows[0]"
},
{
"$ref": "$.rows[0]"
},
{
"$ref": "$.rows[0]"
},
{
"$ref": "$.rows[0]"
},
{
"$ref": "$.rows[0]"
},
{
"$ref": "$.rows[0]"
},
{
"$ref": "$.rows[0]"
},
{
"$ref": "$.rows[0]"
},
{
"$ref": "$.rows[0]"
},
{
"$ref": "$.rows[0]"
},
{
"$ref": "$.rows[0]"
},
{
"$ref": "$.rows[0]"
},
{
"applyInfoId": "D95C3E0C4FF04C538B55E58D7C86DB25",
"applyName": "赵英",
"applyReason": "工作测试",
"applyTime": 1416758400000,
"applyUserId": "8289",
"effeTime": 1417190400000,
"filePath": "D:\\tools\\apache-tomcat-6.0.41\\webapps\\InerviceSystem\\upload",
"id": 21,
"mobile": "1893201456",
"phone": "0512-987456321",
"userDeptId": "2006"
},
{
"$ref": "$.rows[54]"
},
{
"$ref": "$.rows[54]"
},
{
"$ref": "$.rows[54]"
},
{
"$ref": "$.rows[54]"
},
{
"$ref": "$.rows[54]"
},
{
"$ref": "$.rows[54]"
},
{
"$ref": "$.rows[54]"
},
{
"$ref": "$.rows[54]"
},
{
"$ref": "$.rows[54]"
},
{
"$ref": "$.rows[54]"
},
{
"$ref": "$.rows[54]"
},
{
"$ref": "$.rows[54]"
},
{
"$ref": "$.rows[54]"
},
{
"$ref": "$.rows[54]"
},
{
"$ref": "$.rows[54]"
},
{
"$ref": "$.rows[54]"
},
{
"$ref": "$.rows[54]"
},
]
}</span>