hibernate DetachedCriteria的使用

hibernate DetachedCriteria的使用
public  PageModel < CrmWarning >  query(PageModel < CrmWarning >  pageModel)
            
throws  Exception {
        Assert.notNull(pageModel);
        
        DetachedCriteria dc 
=  DetachedCriteria.forClass(CrmWarning. class );
        
        
/*
         * 添加查询条件
         * 如:dc.add(Restrictions.like("key", "%"+pageModel.getCondition("key")+"%"));
         * 
         * 
*/
        
if  ( null   !=  pageModel.getCondition( " warnLevel " )) {
            dc.add(Restrictions.eq(
" warnLevel " , pageModel.getCondition( " warnLevel " )));
        }
        
if  (StringUtils.isNotBlank((String)pageModel.getCondition( " detail " ))) {
            dc.add(Restrictions.like(
" detail " " % " + pageModel.getCondition( " detail " ) + " % " ));
        }
        
if  (StringUtils.isNotBlank((String)pageModel.getCondition( " warnDate " ))) {
            
/*
             * warnDate格式是yyyy-MM-dd,这里改成查询这一天的预警
             * Restrictions.sqlRestriction()允许写原生sql的方式完成特殊需求,如下
             * 
*/
            dc.add(Restrictions.sqlRestriction(
" to_char(time_start,'yyyy-MM-dd') = ' "   +  pageModel.getCondition( " warnDate " +   " ' " ));
        }
        
if  ( null   !=  pageModel.getCondition( " templateId " )) {
            
/*
             * 这里的templateId是模板的主键id,查询时应该转换成模板对象
             * 
*/
            CrmWarnTemplate warnTemplate 
=   new  CrmWarnTemplate();
            warnTemplate.setTemplateId((Long)pageModel.getCondition(
" templateId " ));
            dc.add(Restrictions.eq(
" crmWarnTemplate " , warnTemplate));
        }
        
        dc.addOrder(Order.asc(
" crmWarnTemplate " ));
        dc.addOrder(Order.desc(
" timeStart " ));
        
        pageModel.setDateList(warningDao.query(dc, pageModel.getFirstNote(), pageModel.getNumInPage()));
        pageModel.setTotal(warningDao.count(dc));
        
        
return  pageModel;
    }

你可能感兴趣的:(hibernate DetachedCriteria的使用)