說明:createQuery用的hql語句進行查詢,createSQLQuery用sql語句查詢;
前者以hibernate生成的Bean為對象裝入list返回;
后者則是以對象數組進行存儲;
一、通過createSQLQuery()查詢獲得,代碼如下:
1 String sql = "select g.*, b.name as bigTypeName, s.name as smallTypeName from t_goods g, t_bigtype b, t_smalltype s " + 2 "where s.id = g.smallTypeId and b.id = g.bigTypeId and g.id = :id"; 3 Query query = this.getCurrentSession().createSQLQuery(sql); 4 query.setResultTransformer(Transformers.aliasToBean(GoodsBeanVo.class)); 5 GoodsBeanVo goodsBeanVo = (GoodsBeanVo)query.uniqueResult();
注意這里要用sql語句查詢,主要是
1 query.setResultTransformer(Transformers.aliasToBean(GoodsBeanVo.class));
這一句代碼,通過ResultTransformer這個類 AliasToBean,通過sql的查詢,會返回數組,然后 hibernate根據數據表的映射,自動幫我們來set對應的字段屬性,查詢的值與設置的vo對應;
setResultTransformer
(
ResultTransformer
transformer)
setResultTransformer的執行者,轉換查詢結果到實際應用的結果列表
ResultTransformer是個接口。
對應的GoodsBeanVo,這里的第二個構造函數可以不要。
1 package com.qc.mall.vo; 2 3 import com.qc.mall.entity.GoodsBean; 4 5 import java.math.BigDecimal; 6 7 /** 8 * @Author: sijizhen 9 * @Date: 2018/11/30 0030 下午 2:24 10 */ 11 public class GoodsBeanVo extends GoodsBean { 12 13 private String bigTypeName; 14 private String smallTypeName; 15 16 public GoodsBeanVo() { 17 } 18 19 /*public GoodsBeanVo(Integer id, String name, BigDecimal price, String proPic, String brand, 20 Integer sales, Integer views, Integer stock, String contents, Integer bigTypeId, 21 Integer smallTypeId, Integer state, Object createtime, Object updatetime, 22 BigDecimal marketReferencePrice, String spare, String remark, String remarkFirst, 23 String remarkSecond, String bigTypeName, String smallTypeName) { 24 super(id, name, price, proPic, brand, sales, views, stock, contents, bigTypeId, 25 smallTypeId, state, createtime, updatetime, marketReferencePrice, 26 spare, remark, remarkFirst, remarkSecond); 27 this.bigTypeName = bigTypeName; 28 this.smallTypeName = smallTypeName; 29 }*/ 30 31 public String getBigTypeName() { 32 return bigTypeName; 33 } 34 35 public void setBigTypeName(String bigTypeName) { 36 this.bigTypeName = bigTypeName; 37 } 38 39 public String getSmallTypeName() { 40 return smallTypeName; 41 } 42 43 public void setSmallTypeName(String smallTypeName) { 44 this.smallTypeName = smallTypeName; 45 } 46 }
二、通過createQuery()查詢獲得,代碼如下:
1 String hql = "select new com.qc.mall.vo.GoodsBeanVo(g.id, g.name, g.price, g.proPic, g.brand, g.sales, g.views, g.stock, g.contents, g.bigTypeId," + 2 "g.smallTypeId, g.state, g.createtime, g.updatetime, g.marketReferencePrice," + 3 "g.spare, g.remark, g.remarkFirst, g.remarkSecond, b.name, s.name) from GoodsBean g, BigTypeBean b, SmallTypeBean s " + 4 "where s.id = g.smallTypeId and b.id = g.bigTypeId and g.id = :id"; 5 Query query = this.getCurrentSession().createQuery(hql); 6 query.setParameter("id", id); 7 GoodsBeanVo goodsBeanVo = (GoodsBeanVo)query.uniqueResult();
用createQuery()查詢,需要vo有對應的構造函數,且hql語句中參數的順序以及參數的類型都要與構造函數中的一致,如果參數類型中有時間類型,需要做相應的轉換,否則會報錯:
Unable to locate appropriate constructor on class
報此類錯誤時具體可以參考:http://blog.sina.com.cn/s/blog_4ad7c2540102uzkc.html
對應的GoodsBeanVo代碼如下:
1 package com.qc.mall.vo; 2 3 import com.qc.mall.entity.GoodsBean; 4 5 import java.math.BigDecimal; 6 7 /** 8 * @Author: sijizhen 9 * @Date: 2018/11/30 0030 下午 2:24 10 */ 11 public class GoodsBeanVo extends GoodsBean { 12 13 private String bigTypeName; 14 private String smallTypeName; 15 16 public GoodsBeanVo() { 17 } 18 19 public GoodsBeanVo(Integer id, String name, BigDecimal price, String proPic, String brand, 20 Integer sales, Integer views, Integer stock, String contents, Integer bigTypeId, 21 Integer smallTypeId, Integer state, Object createtime, Object updatetime, 22 BigDecimal marketReferencePrice, String spare, String remark, String remarkFirst, 23 String remarkSecond, String bigTypeName, String smallTypeName) { 24 super(id, name, price, proPic, brand, sales, views, stock, contents, bigTypeId, 25 smallTypeId, state, createtime, updatetime, marketReferencePrice, 26 spare, remark, remarkFirst, remarkSecond); 27 this.bigTypeName = bigTypeName; 28 this.smallTypeName = smallTypeName; 29 } 30 31 public String getBigTypeName() { 32 return bigTypeName; 33 } 34 35 public void setBigTypeName(String bigTypeName) { 36 this.bigTypeName = bigTypeName; 37 } 38 39 public String getSmallTypeName() { 40 return smallTypeName; 41 } 42 43 public void setSmallTypeName(String smallTypeName) { 44 this.smallTypeName = smallTypeName; 45 } 46 }
如有紕漏,還望指正。
參考:https://blog.csdn.net/qq_38286331/article/details/81391948
參考:https://blog.csdn.net/richerg85/article/details/41745819