Hibernate 執行普通的sql語句,並將結果封裝成DTO物件
阿新 • • 發佈:2019-02-03
hibernate 自動對映物件有時候在用到多個表進行查詢的時候根本搞不定
如果是查出來的結果進行篩選那麼需要分頁功能就完蛋了,所以需要用普通的聯合查詢sql,
下面上程式碼:
DTO物件:
package com.sc.tradmaster.service.project.impl.dto;
//門店業務DTO類
public class ShopBusinessDTO {
//門店客戶名稱
private String shopCustomerName;
//專案名稱
private String projectName;
//專案id
private String projectId;
//備案狀態 0 備案 1 已到訪 2 成交
private Integer applyStatus;
//備案時間
private String applyTime;
//案場客戶id
private String projectCustomerId;
public String getShopCustomerName() {
return shopCustomerName;
}
public void setShopCustomerName(String shopCustomerName) {
this .shopCustomerName = shopCustomerName;
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
public String getProjectId() {
return projectId;
}
public void setProjectId (String projectId) {
this.projectId = projectId;
}
public Integer getApplyStatus() {
return applyStatus;
}
public void setApplyStatus(Integer applyStatus) {
this.applyStatus = applyStatus;
}
public String getApplyTime() {
return applyTime;
}
public void setApplyTime(String applyTime) {
this.applyTime = applyTime;
}
public String getProjectCustomerId() {
return projectCustomerId;
}
public void setProjectCustomerId(String projectCustomerId) {
this.projectCustomerId = projectCustomerId;
}
@Override
public String toString() {
return "ShopBusinessDTO [shopCustomerName=" + shopCustomerName + ", projectName=" + projectName + ", projectId="
+ projectId + ", applyStatus=" + applyStatus + ", applyTime=" + applyTime + ", projectCustomerId="
+ projectCustomerId + "]";
}
}
//將查詢出來的結果封裝成DTO物件
Dao層:
/**
* @param sql 需要查詢的sql語句
* @param clazz DTO物件
* @param colums DTO物件的屬性名
* @param types DTO物件的type型別
*
*/
public List queryDTOBySql(String sql,Class clazz,String[] colums,String[] types) {
Session session = super.getSessionFactory().getCurrentSession();
SQLQuery query = session.createSQLQuery(sql);
if(colums!=null && types!=null && colums.length==types.length){
for(int i=0;i<colums.length;i++){
if(types[i].equals("Integer")){
query.addScalar(colums[i],StandardBasicTypes.INTEGER);
}else if(types[i].equals("String")){
query.addScalar(colums[i],StandardBasicTypes.STRING);
}else if(types[i].equals("Double")){
query.addScalar(colums[i],StandardBasicTypes.DOUBLE);
}
}
}
List<Object[]> list = query.setResultTransformer(Transformers.aliasToBean(clazz)).list();
return list;
}
Service層:
String hql = "SELECT sc.shopCustomerName as shopCustomerName,p.projectName as projectName, "
+ " p.projectId as projectId,g.applyStatus as applyStatus,g.applyTime as applyTime, g.projectCustomerId as projectCustomerId ";
hql += " from ";
hql += " t_shopCustomers as sc,t_projects as p,t_guideRecords as g";
hql += " where ";
hql += " sc.shopCustomerId = g.shopCustomerId and (g.applyStatus = 0 or g.applyStatus = 1) and p.projectId = g.projectId ";
hql += "and sc.shopId =" + user.getParentId();
//hql += " and sc.shopId = 1 ";
//模糊搜尋某一個客戶名稱
if(cusOrProName != null && !"".equals(cusOrProName)){
hql += " and sc.shopCustomerName like '%" + cusOrProName + "%' ";
}
//進行時間的篩選
if(startTime != null && !"".equals(startTime)){
hql += " and g.applyTime >= '" + startTime + "' ";
}
if(endTime != null && !"".equals(endTime)){
hql += " and g.applyTime <= '" + endTime + "' ";
}
//進行備案狀態的查詢
if(status != null && !"".equals(status)){
hql += " and g.applyStatus = " + status;
}
//分頁
hql += " limit " + page.getStart() + "," + page.getLimit();
String[] colums ={"shopCustomerName","projectName","projectId","applyStatus","applyTime","projectCustomerId"};
String[] types = {"String","String","String","Integer","String","String"};
List<ShopBusinessDTO> list = baseDao.queryDTOBySql(hql, ShopBusinessDTO.class, colums, types);
這樣的查詢使得hibernate可以更加靈活(為了一些特定的功能)