hibernate native sql的小技巧

为了性能考虑,使用了 native sql 。因为需要分页,需要 2sql ,一个获取 list 一个取得总数。获取 list 很好写:

 

 1 ExpandedBlockStart.gif ContractedBlock.gif private  List getListByNativeSQL( final  Class cls,  final  String sql)  dot.gif {
 2 InBlock.gif
 3 ExpandedSubBlockStart.gifContractedSubBlock.gif        return  (List) getHibernateTemplate().execute( new  HibernateCallback()  dot.gif {
 4 InBlock.gif
 5 InBlock.gif            public  Object doInHibernate(Session session)
 6 InBlock.gif
 7 ExpandedSubBlockStart.gifContractedSubBlock.gif                   throws  HibernateException  dot.gif {
 8 InBlock.gif
 9 InBlock.gif               return  session.createSQLQuery(sql).addEntity(cls).list();
10 InBlock.gif
11 ExpandedSubBlockEnd.gif           }

12 InBlock.gif
13 ExpandedSubBlockEnd.gif       }
);
14 InBlock.gif
15 ExpandedBlockEnd.gif    }

16 None.gif

 

获取总数查了下 hibernatereference, 试了几次才明白用法 :<o:p></o:p>

<o:p> </o:p>

 1 ExpandedBlockStart.gif ContractedBlock.gif private  BigInteger getCountByNativeSQL( final  String sql)  dot.gif {
 2 InBlock.gif
 3 InBlock.gif        return  (BigInteger) getHibernateTemplate().execute(
 4 InBlock.gif
 5 ExpandedSubBlockStart.gifContractedSubBlock.gif               new  HibernateCallback()  dot.gif {
 6 InBlock.gif
 7 InBlock.gif                   public  Object doInHibernate(Session session)
 8 InBlock.gif
 9 ExpandedSubBlockStart.gifContractedSubBlock.gif                          throws  HibernateException  dot.gif {
10 InBlock.gif
11 InBlock.gif                      return  (BigInteger) (session.createSQLQuery(sql).uniqueResult());
12 InBlock.gif
13 ExpandedSubBlockEnd.gif                  }

14 InBlock.gif
15 ExpandedSubBlockEnd.gif              }
);
16 InBlock.gif
17 ExpandedBlockEnd.gif    }

18 None.gif

这里的

sql 是“ select count(*) 开头的”。这里大家可能要问,为什么要使用 BigInteger ,因为如果用 uniqueResult() 默认就返回 BigInteger ,而 BigInteger castInteger 会出错。那么如果我就是要返回 Integer 呢,可以通过下面的办法实现: <o:p> </o:p>

<o:p> </o:p>

 1 ExpandedBlockStart.gif ContractedBlock.gif private  Integer getCountByNativeSQL( final  String sql)  dot.gif {
 2 InBlock.gif
 3 InBlock.gif        return  (Integer) getHibernateTemplate().execute(
 4 InBlock.gif
 5 ExpandedSubBlockStart.gifContractedSubBlock.gif               new  HibernateCallback()  dot.gif {
 6 InBlock.gif
 7 InBlock.gif                   public  Object doInHibernate(Session session)
 8 InBlock.gif
 9 ExpandedSubBlockStart.gifContractedSubBlock.gif                          throws  HibernateException  dot.gif {
10 InBlock.gif
11 InBlock.gif                      return  (Integer) (session.createSQLQuery(sql).addScalar( " count " , Hibernate.INTEGER).uniqueResult());
12 InBlock.gif
13 ExpandedSubBlockEnd.gif                  }

14 InBlock.gif
15 ExpandedSubBlockEnd.gif              }
);
16 InBlock.gif
17 ExpandedBlockEnd.gif    }

18 None.gif

大家注意粗体的部分,这里是给一个

alias 赋予类型,那么 sql 就需要变成以 ”select count(*) as count ” 开头了。 <o:p> </o:p>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值