1. 程式人生 > >通過id查詢使用者,但是隻返回指定的欄位

通過id查詢使用者,但是隻返回指定的欄位

使用hibernate和spring MVC
通過id查詢到一個使用者,但是隻返回指定的欄位
方式一: 拼接hql

/***
     * 通過資料庫ID查詢使用者,但是隻返回指定的欄位
     * @param id
     * @param propertyNames : 指定的多個成員變數
     * @return
     */
    public Object[] getPropertiesById(int id,String[] propertyNames){
        if(ValueWidget.isNullOrEmpty(propertyNames)){
            return
null; } String hql="select "+propertyNames[0]; for (int i = 1; i < propertyNames.length; i++) { String string = propertyNames[i]; hql+=","+string; } String parameterId="id22"; hql+=" from "+clz.getCanonicalName()+" c where c.id=:"
+parameterId; Query q= this.sessionFactory.getCurrentSession().createQuery(hql); Object result=q.setInteger(parameterId, id).uniqueResult(); Object[]objs=null; if(result instanceof Object[]){ objs=(Object[])result; }else{//當只返回一個成員變數的時候 objs=new
Object[]{result}; } return objs; } /*** * 通過id,查詢到一條記錄,但是隻返回指定的兩個欄位 * @param id * @param propertyName1 : 類的成員變數 * @param propertyName2 : 類的成員變數 * @return */ public Object[] getPropertiesById(int id,String propertyName1,String propertyName2){ String hql="select "+propertyName1; if(!ValueWidget.isNullOrEmpty(propertyName2)){ hql+=","+propertyName2; } String parameterId="id22"; hql+=" from "+clz.getCanonicalName()+" c where c.id=:"+parameterId; Query q= this.sessionFactory.getCurrentSession().createQuery(hql); Object result=q.setInteger(parameterId, id).uniqueResult(); Object[]objs=null; if(result instanceof Object[]){ objs=(Object[])result; }else{//當只返回一個成員變數的時候 objs=new Object[]{result}; } return objs; }

方式二:使用投影

/***
     * 通過資料庫ID查詢使用者,但是隻返回指定的欄位
     * @param id
     * @param propertyNames : 指定的多個成員變數
     * @return
     */
    public Object[] getPropertiesById2(int id,String[] propertyNames){
        Criteria c=this.sessionFactory.getCurrentSession().createCriteria(clz);
        ProjectionList projectionList=Projections.projectionList();
        if(!ValueWidget.isNullOrEmpty(propertyNames)){
            for (int i = 0; i < propertyNames.length; i++) {
                String string = propertyNames[i];
                projectionList.add(Projections.property(string));
            }
        }
        Object result=c.add(Restrictions.idEq(id)).setProjection(projectionList).uniqueResult();
        Object[]objs=null;
        if(result instanceof Object[]){
            objs=(Object[])result;
        }else{//當只返回一個成員變數的時候
            objs=new Object[]{result};
        }
        return objs;

    }
/***
     * 通過id,查詢到一條記錄,但是隻返回指定的兩個欄位
     * @param id
     * @param propertyName1 : 類的成員變數
     * @param propertyName2 : 類的成員變數
     * @return
     */
    public Object[] getPropertiesById2(int id,String propertyName1,String propertyName2){
        Criteria c=this.sessionFactory.getCurrentSession().createCriteria(clz);
        ProjectionList projectionList=Projections.projectionList()
                .add(Projections.property(propertyName1));
        if(!ValueWidget.isNullOrEmpty(propertyName2)){
            projectionList.add(Projections.property(propertyName2));
        }
        Object result=c.add(Restrictions.idEq(id)).setProjection(projectionList).uniqueResult();
        Object[]objs=null;
        if(result instanceof Object[]){
            objs=(Object[])result;
        }else{//當只返回一個成員變數的時候
            objs=new Object[]{result};
        }
        return objs;

    }

使用場景:知道id,但是沒必要獲取整條記錄,只需要獲取其中的兩三個欄位而已.