码迷,mamicode.com
首页 > 数据库 > 详细

hibernate用sql联合查询数据库

时间:2015-04-02 19:14:33      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:


@RequestMapping(value = "/FeedPager.cqzk")
    @ResponseBody
    public String url_sa1(HttpServletRequest request, BootPage page)
            throws ServletException, IOException, RuntimeException {
        @SuppressWarnings("unchecked")
        BootPage pager = feedBackDao.getBySQLPage("select Feedback.id,Feedback.title,Feedback.content," +
                "Feedback.creattime,Rms_User.user_name from t_Feedback Feedback,Rms_User where Feedback.userId = Rms_User.user_id ", page, null);
        System.out.println((JSONArray.fromObject(pager)).getString(0).toString());
        System.out.println((JSONArray.fromObject(pager)).getString(0).toString());
        return (JSONArray.fromObject(pager)).getString(0).toString();
    }
public BootPage getBySQLPage(String hql,BootPage pager,Map<String, Object> condition){
        if (pager == null) {
            throw new IllegalArgumentException("分页 不能为空!");
        }
        
        Query q = sessionFactory.getCurrentSession().createSQLQuery(hql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);;
        q.setFirstResult(pager.getOffset());
        q.setMaxResults(pager.getLimit());
        if(condition!=null){
            Iterator<?> iter = condition.keySet().iterator();
            while (iter.hasNext()) {
                String paramName = (String) iter.next();
                Object paramValue = condition.get(paramName);
                q.setParameter(paramName, paramValue);
            }
        }
        pager.setRows(q.list());
        pager.setTotal(this.countAllSQL(hql,condition));
        return pager;
        
    }
    protected Long countAllSQL(String hql, Map<String, Object> condition) {
        if (hql == null) {
            return 0l;
        }
        String sql = " select count(*) total from ( " + hql + " ) as temp ";        
        Query q = sessionFactory.getCurrentSession().createSQLQuery(sql);
        if(condition!=null){
            Iterator<?> iter = condition.keySet().iterator();
            while (iter.hasNext()) {
                String paramName = (String) iter.next();
                Object paramValue = condition.get(paramName);
                q.setParameter(paramName, paramValue);
            }
        }
        return  new Long(((Integer) q.uniqueResult()).intValue());
    }


hibernate用sql联合查询数据库

标签:

原文地址:http://my.oschina.net/u/2340612/blog/395307

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!