使用hibernate和spring mvc
通過id查詢到乙個使用者,但是只返回指定的字段
方式一: 拼接hql
方式二:使用投影/***
* 通過資料庫id查詢使用者,但是只返回指定的字段
*@param id
*@param propertynames : 指定的多個成員變數
*@return
*/public object getpropertiesbyid(int id,string propertynames)
string hql="select "+propertynames[0];
for (int i = 1; i < propertynames.length; i++)
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();
objectobjs=null;
if(result instanceof object)else;
}return objs;
}/***
* 通過id,查詢到一條記錄,但是只返回指定的兩個字段
*@param id
*@param propertyname1 : 類的成員變數
*@param propertyname2 : 類的成員變數
*@return
*/public object getpropertiesbyid(int id,string propertyname1,string 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();
objectobjs=null;
if(result instanceof object)else;
}return objs;
}
使用場景:知道id,但是沒必要獲取整條記錄,只需要獲取其中的兩三個字段而已./***
* 通過資料庫id查詢使用者,但是只返回指定的字段
*@param id
*@param propertynames : 指定的多個成員變數
*@return
*/public object getpropertiesbyid2(int id,string propertynames)
}object result=c.add(restrictions.ideq(id)).setprojection(projectionlist).uniqueresult();
objectobjs=null;
if(result instanceof object)else;
}return objs;
}/***
* 通過id,查詢到一條記錄,但是只返回指定的兩個字段
*@param id
*@param propertyname1 : 類的成員變數
*@param propertyname2 : 類的成員變數
*@return
*/public object getpropertiesbyid2(int id,string propertyname1,string propertyname2)
object result=c.add(restrictions.ideq(id)).setprojection(projectionlist).uniqueresult();
objectobjs=null;
if(result instanceof object)else;
}return objs;
}
通過id集合查詢sql(hibernate)
一 主要是 支援hibernate。這裡也讚美下hibernate的api設計,既提供了常規繁瑣的做法setparameter,也提供了簡潔易用的setparameterlist。這種一致性,對於熟悉和不熟悉hibernate的人來說,提供了更多的選擇。1 通過id集合獲取物件 string hql...
1 Mybatis框架根據使用者ID查詢使用者資訊簡析
是目前比較主流的持久層框架,是apache的乙個開源專案ibatis。mybatis是面向sql的持久層框架,他封裝了jdbc訪問資料庫的過程,我們開發,只需專注於sql語句本身的拼裝,其它複雜的過程全部可以交給mybatis去完成。1.1對映檔案和sql連線 id statementid resu...
從網頁中傳入乙個Id 通過Id 刪除或者查詢
如果要用id 查詢,刪除的話 hibernatebasedao中是 查詢單個 public object queryone string claname,int id 根據id刪除 public void deletebyid string claname,int id 列 如果乙個user到 要通...