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>格式返回

  • 相关阅读:
    TypeScript中处理大数字(会丢失后面部分数字)
    多行字符串换行符(`) + 模板字符串
    ES6 阮一峰阅读学习
    ms转成00:00:00的时间格式化
    android侧滑效果,SlidingMenu配置
    Android Developers:按需求加载视图
    Python测试代理ip是否有效
    JavaScript去除数组中重复的数字
    Python连接redis
    [已解决]报错: Creating Server TCP listening socket 127.0.0.1:6379: bind: No error
  • 原文地址:https://www.cnblogs.com/qinying/p/2113972.html
Copyright © 2011-2022 走看看