zoukankan      html  css  js  c++  java
  • 在hibernate中实现查找部分字段

    今天对网站进行修改,想优化一下产品列表。原来的查找产品列表的时候,把产品的所有信息全部取出来了。我现在想只获取必须的字段。
    我在网上查了一下,可以使用hql语句:“select new 包名.类名(属性1,属性2……) from 实体类来实现

    我是这么写的:

        public List<TgShop> getSimpleTgShopByCid(int cid, String date,
    int startIndex, int pageSize) {
    // TODO Auto-generated method stub
    return (List<TgShop>) this
    .getHibernateTemplate()
    .executeFind(
    new PageHibernateCallback(
    "select new "
    + className
    + "(id, shopName, begintime, endtime, successPeople, discount, maxNum, unitPrice, lowPeople,tgImage) from "
    + className
    + " t where t.stateValue=2 and t.cityInfo.id="
    + cid
    + " and '"
    + date
    + "' between t.begintime and t.endtime order by t.id desc",
    startIndex, pageSize));

    }

    其中我的className是类名。我试着调用了一个这个方法。然后就报错了。好像是提示没有相应的构造方法。
    于是我在TgShop里面添加了一个构造方法,如下:

        public TgShop(Integer id, String shopName, Date begintime, Date endtime,
    Integer successPeople, Double discount, Integer maxNum,
    Double unitPrice, Integer lowPeople,String tgImage) {
    this.id = id;
    this.shopName = shopName;
    this.begintime = begintime;
    this.endtime = endtime;
    this.successPeople = successPeople;
    this.discount = discount;
    this.maxNum = maxNum;
    this.unitPrice = unitPrice;
    this.lowPeople = lowPeople;
    this.tgImage=tgImage;
    }

    需要注意的是这个构造方法里面的参数是和hql语句里面的相对应的
    运行一下,成功,查询了我想要的字段,以list<T>格式返回

  • 相关阅读:
    demo04-默认标签
    demo03-段落标签
    demo02-标题标签
    demo01-注释标签
    前端基础介绍
    xadmin的详细使用
    设置Linux环境变量中文显示乱码
    ES应用
    HTTP协议
    jboss
  • 原文地址:https://www.cnblogs.com/qinying/p/2113972.html
Copyright © 2011-2022 走看看