您的位置:首页 > 其它

数据查询中 IN关键字的使用方法总结

2013-05-17 15:48 465 查看
第一种(在hql语句中的where条件中拼接好in的内容):

 

List<TUmUserrole> roleList = hibernateDao.queryObjectsByWhere(
TUmUserrole.class, wherePart, parmaters.toArray(),
sortField, null, null);

String rolesStr = "";// 所有rolepid组合字符串
if (null != roleList && roleList.size() > 0) {
// userrole不为空时,取到所有的rolepid ,根据所有rolepid去rolelimits表中找所有的limits
for (TUmUserrole userRole : roleList) {
if (rolesStr != "")
rolesStr += ",";
rolesStr += "'" + userRole.getRolepid() + "'";
}
}
user.setRolepid(rolesStr);
String limitsStr = "";
if (StringUtil.notEmpty(rolesStr)) {
parmaters.clear();
wherePart = " ROLEPID IN ( " + rolesStr + " ) ";
wherePart += " AND STATUS = ? ";
parmaters.add(ISuperVO.STATUS_ACTIVE);
sortField = " UPDATEDATETIME DESC";
List<TUmRolelimits> limitList = hibernateDao
.queryObjectsByWhere(TUmRolelimits.class, wherePart,
parmaters.toArray(), sortField, null, null);


第二种(在如下回调中使用list类型参数):

List orgList = null;
StringBuffer wherePart = new StringBuffer();
List<Object> parmaters = new ArrayList<Object>();
//指定区域查询
if (StringUtil.notEmpty(entity.getAreaCode())) {
wherePart.append("arCd = ?");
parmaters.add(entity.getAreaCode());
}
orgList = this.hibernateDao.queryObjectsByWhere(Healthorgreg.class, wherePart.toString(), parmaters.toArray(), null, null, null);
List tempList = new ArrayList();
for(int i=0;i<orgList.size();i++){
Healthorgreg healthOrgReg = (Healthorgreg) orgList.get(i);
tempList.add(healthOrgReg.getOrganizationCode());
}
//
List resultList = null;
StringBuffer wherePart1 = new StringBuffer();
List<Object> parmaters1 = new ArrayList<Object>();
// 指定机构代码查询
wherePart1.append("belongOrgCode in(:typeids)");
parmaters1.add(tempList);

resultList = this.hibernateDao
.queryObjectsByWhere(Hospitalassets.class, wherePart1.toString(),
parmaters1.toArray(), null, null, null);
@SuppressWarnings("unchecked")
public List queryObjectsByWhere(Class cl, String wherePart,
Object[] parmaters, String sortField, Integer startRow,
Integer rowCount) throws DaoException {
try {
if (cl == null) {
throw new DaoException(DaoExceptionType.TYPE_CODE_PARAM_NULL,
"vo is not null");
}
String hql = "from " + cl.getName() + " where 1=1 ";
if (wherePart != null && wherePart.trim().length() > 0) {
hql = hql + " and " + wherePart;
}
if (sortField != null && sortField.trim().length() > 0) {
hql = hql + " order by " + sortField;
}
return queryObjectsByHQL(hql, parmaters, startRow, rowCount);
} catch (Exception e) {
throw new DaoException(DaoExceptionType.TYPE_CODE_DAO, e);
}
}


 

@SuppressWarnings("unchecked")
public List queryObjectsByHQL(final String HQLStr,
final Object[] parmaters, final Integer startRow,
final Integer rowCount) throws DaoException {

try {
List list = getHibernateTemplate().executeFind(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(HQLStr);

if (startRow != null && startRow.intValue() >= 0
&& rowCount != null
&& rowCount.intValue() >= 0) {
query.setFirstResult(startRow);
query.setMaxResults(rowCount);
}
if (parmaters != null && parmaters.length > 0) {
for (int i = 0; i < parmaters.length; i++) {
if((parmaters[i]) instanceof List){
List tempList = (List) parmaters[i];
query.setParameterList("typeids", tempList);
}else{

query.setParameter(i, parmaters[i]);
}
}
}
List list = query.list();
return list;
}

});
return list;
} catch (Exception e) {
throw new DaoException(DaoExceptionType.TYPE_CODE_DAO, e);
}

}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐