• Hibernate查询返回自定义VO的两种方式


    说明: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

    脚踏实地,仰望星空。
  • 相关阅读:
    android工程混淆和反编译
    php+列出目录文件
    php+大文件管理
    支持粘贴图片的富文本编辑器
    web上传整个文件夹
    文件夹管理
    断点续传
    超大文件上传方案
    ueditor+word粘贴上传
    java+大文件上传
  • 原文地址:https://www.cnblogs.com/sijizhen/p/10057294.html
Copyright © 2020-2023  润新知