• hibernate在使用sql查询query自动转化成model类型数据,query.addEntity


    hibernate使用自动的hql查询或者其封装的查询方法都能字段转化成对象

    而如果在hibernate中使用sql时大多返回为Object[]对象

    那么如何将object[]转换成model呢,答案就是

    addEntity(Class cls);

    实列

        public HqxgModel getMaxMinHqxg(){
            StringBuilder sb = new StringBuilder();
            sb.append("select  ");
            sb.append("'0' idStr,  ");
            sb.append("'' date,  ");
            sb.append("'0' hqsyl1End,'0' hqsyl1Start,  ");
            sb.append("'0' hqsyl2End,'0' hqsyl2Start,  ");
            sb.append("'' hqxgTitle,'' userid,  ");
            sb.append("'' industryCode,'' province,  ");
            sb.append("'' xxzrlxM,'' xxzrlxT,  ");
            //不保留小数点的
            sb.append("max(CEILING(h.hsl))hqhslEnd,min(FLOOR(h.hsl))hqhslStart, ");
            sb.append("max(CEILING(h.cjl))hqcjslEnd,min(FLOOR(h.cjl))hqcjslStart, ");
            sb.append("max(CEILING(h.cje))hqcjjeEnd,min(FLOOR(h.cje))hqcjjeStart, ");
            sb.append("max(CEILING(h.zdf))hqzdfEnd,min(FLOOR(h.zdf))hqzdfStart, ");
            sb.append("max(CEILING(h.zxj))hqzrspEnd,min(FLOOR(h.zxj))hqzrspStart, ");
            sb.append("max(CEILING(h.syl))sylEnd,min(FLOOR(h.syl))sylStart, ");
            sb.append("max(CEILING(h.mgjzc))mgjzcEnd,min(FLOOR(h.mgjzc))mgjzcStart, ");
            sb.append("max(CEILING(h.zsz))xxzszEnd,min(FLOOR(h.zsz))xxzszStart, ");
            sb.append("max(CEILING(h.zfz))fzHeJiEnd,min(FLOOR(h.zfz))fzHeJiStart, ");
            sb.append("max(CEILING(h.zzc))zczongJiEnd,min(FLOOR(h.zzc))zczongJiStart, ");
            sb.append("max(CEILING(h.zgb))xxzgbEnd,min(FLOOR(h.zgb))xxzgbStart, ");
            sb.append("max(CEILING(h.gsygpgsgdjlr))gsygpgsgdjlrEnd,min(FLOOR(h.gsygpgsgdjlr))gsygpgsgdjlrStart, ");
            sb.append("max(CEILING(h.mgsy))mgsyEnd,min(FLOOR(h.mgsy))mgsyStart, ");
            sb.append("max(CEILING(h.syl))ylnljzcsylroeEnd,min(FLOOR(h.syl))ylnljzcsylroeStart, ");
            sb.append("max(CEILING(h.srjll))ylncxljllEnd,min(FLOOR(h.srjll))ylncxljllStart, ");
            sb.append("max(CEILING(h.mll))mllEnd,min(FLOOR(h.mll))mllStart, ");
            sb.append("max(CEILING(h.jlr))jingLiRunEnd, min(FLOOR(h.jlr))jingLiRunStart, ");
            sb.append("max(CEILING(h.yysr))yingYeShouRuEnd,min(FLOOR(h.yysr))yingYeShouRuStart ");
            sb.append("from hqxg h ");
            try {
                Query query = super.getCurrentSession().createSQLQuery(sb.toString());
                List<HqxgModel> list = ((SQLQuery) query).addEntity(HqxgModel.class).list();
                if( list !=null && list.size()>0)return list.get(0);
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                //System.out.println("");
            }
            return null;
        }
  • 相关阅读:
    HDFS上传下载API
    HDFS2.X新特性:HA和Federation联盟
    HDFS2.X架构及工作原理
    浅析Secondary NameNode与namenode
    Flink安装部署
    HDFS基本命令行操作与简单API实用
    Linux基础_Hadoop环境搭建必备
    HBase BlockCache机制讲解
    HDFS集群安装部署
    我亦未曾饶过岁月_面试总结
  • 原文地址:https://www.cnblogs.com/hwaggLee/p/4644823.html
Copyright © 2020-2023  润新知