Hibernate自定义数据库查询(排序、输出条数)

 1 Hibernate数据库操作类(eg:TexDAO.java)
 2 
 3     /*
 4      * queryString HQL语句,first开始条数, max输出条数 ,norder排序
 5      * 例: List lis = dao.findOther("from Tex",2,10," uid desc ");
 6      */
 7     public List findOther(String queryString, int first, int max, String norder) {
 8         StringBuffer sb = new StringBuffer();
 9 
10         // 这里是hibernate 对应的bean名并非实际表,这个实际对应的是视图
11         sb.append(queryString);
12 
13         // 从大到小排序
14         sb.append(" order by "+norder);
15         Query queryObject = getSession().createQuery(sb.toString());
16 
17         // 开始条数
18         queryObject.setFirstResult(first);
19 
20         // 输出条数
21         queryObject.setMaxResults(max);
22         return queryObject.list();
23     }
24 
25 
26 
27 Action业务逻辑类(eg:TexAction.java)
28 
29 public String list_tex2() {
30         lis = dao.findOther("from Tex",2,10," uid desc ");
31         
32         //测试
33         for (Tex t : lis) {
34             System.out.println("编号:" + t.getUid());
35             System.out.println("姓名:" + t.getUtname());
36         }
37         return "success";
38     }

 

posted @ 2014-02-27 17:53  hlb  阅读(1059)  评论(0编辑  收藏  举报