用HQL语句实现多表联查和返回DTO对象
当需要join多个表的时候,如何写HQL
SQL
select *
from P_GROUP g
left join P_DEPT d on d.P_UID = g.P_UID
left join P_PATIENT p on d.P_NO = p.P_NO
where p.CARD_TYPE = '1'
and p.CARD_NUMBER = '111123196709274653';
HQL
select g
from PGroup g, PDept d, PPatient p
where p.cardType = '1'
and p.cardNumber = '111123196709274653'
and d.pUid = g.pUid
and p.pNo = d.pNo;
简单说就是把我们需要join的表都放在from语句中。把关联条件都放在where语句中,并使用and关键字链接。
不管是inner join还是out join都统一使用上面的写法。
如何使用Hibernate给DTO对象赋值
对于这个问题,传统的方法是使用原生的SQL语句进行拼装。这么做使得代码冗长,不易于维护,也没有充分利用Hibernate解耦合的特性。
以下面这个SQL语句为例:
select g.pUid, g.pDeptUid, g.pNo, g.plaUid, g.packageId, g.groupId, g.interfaceId, g.applyDocId, gs.groupName, dt.departmentName
from P_GROUP g
left join P_DEPT d on d.P_UID = g.P_UID
left join P_PATIENT p on d.P_NO = p.P_NO
left join P_GROUPS gs on gs.ID = g.GROUP_ID
left join P_DEPARTMENT dt on dt.ID = g.DEPARTMENT_ID
where p.CARD_TYPE = '1'
and p.CARD_NUMBER = '111123196709274653';
新建DTO类
public class PGroupDto {
/**
* 组合名称
*/
private String groupName;
/**
* 科室名称
*/
private String departmentName;
/**
* 组合对象
*/
private PGroup pGroup;
/* 这里省略getter和setter */
/* 这里省略无参构造函数 */
public PGroupDto(String groupName, String departmentName, PGroup pGroup) {
this.groupName = groupName;
this.departmentName = departmentName;
this.pGroup = pGroup;
}
}
新建DTO类的时候选择组合PesPlaPatGroup,而不是继承PesPlaPatGroup是为了减少HQL的代码量。
HQL
select new com.future.pos.plan.dto.PGroupDto(g.groupName, g.departmentName, g)
from PGroup g, PDept d, PPatient p, PGroups gs, PDepartment dt
where p.cardType = :cardType
and p.cardNumber = :cardNum
and d.pUid = g.pUid
and p.pNo = d.pNo
and gs.id = g.groupId
and dt.id = g.departmentId
需要注意,HQL中的DTO类需要写全路径。
与前端框架layui的整合
service层关键代码
String hql = "select new com.future.pos.plan.dto.PGroupDto(g.groupName, g.departmentName, g) " +
"from PGroup g, PDept d, PPatient p, PGroups gs, PDepartment dt " +
"where p.cardType = :cardType " +
"and p.cardNumber = :cardNum " +
"and d.pUid = g.pUid " +
"and p.pNo = d.pNo " +
"and gs.id = g.groupId " +
"and dt.id = g.departmentId";
Query query = entityManager.createQuery(hql);
query.setParameter("cardType", cardType);
query.setParameter("cardNum", cardNum);
return (List<PGroupDto>) query.getResultList();
Controller层关键代码
List<PGroupDto> groupList = groupService.queryGroupByCardNum(cardType, cardNum);
returnMap.put("code", OK);
returnMap.put("msg", "成功取得组合信息列表");
returnMap.put("count", groupList.size());
returnMap.put("data", groupList);
return new JSONObject(returnMap).toJSONString();
这里使用了FastJson对对象进行序列化。
JavaScript关键代码
/**
* 渲染组合表
*/
table.render({
elem: "#groupTable",
url: baseUrl + "/groupController/getGroupList",
method: "post",
where: {"info": json},
cols: [[
{type:'checkbox'},
{width:80, title: 'ID', templet: function (d) {return d.pGroup.id}},
{field: "groupName", title: "组合名称"},
{title: "组合状态", templet: function (d) {return d.pGroup.state;}},
{title: "申请日期", templet: function (d) {return d.pGroup.applyDate;}},
{title: "机构名称", templet: function (d) {return d.pGroup.orgName;}},
{title: "检查日期", templet: function (d) {return d.pGroup.checkDate}},
{title: "修改人", templet: function (d) {return d.pGroup.modifyStaff}},
{title: "修改日期", templet: function (d) {return d.pGroup.modifyDate}}
]],
page: true
});
谢谢耐心地读到这里_
版权声明:本文为bluewudi原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。