以前在用hibernate写查询的时候,都是带两个参数的查询,这样的话不利于放在底层,也就是不利于代码的
重复使用,下面是带两个参数的代码:
public
List find(String queryString, String value)
{
Session session = sessionManage.currentSession();
try {
Query query = session.createQuery(queryString);
query.setString(0, value);
query.setCacheable(cacheable);
return query.list();
} catch (HibernateException e) {
BaseService.getInstance().getContext().log(
"[PersistMap][find] HibernateException ",
e);
return null;
} finally {
sessionManage.closeSession();
}
}
Session session = sessionManage.currentSession();
try {
Query query = session.createQuery(queryString);
query.setString(0, value);
query.setCacheable(cacheable);
return query.list();
} catch (HibernateException e) {
BaseService.getInstance().getContext().log(
"[PersistMap][find] HibernateException ",
e);
return null;
} finally {
sessionManage.closeSession();
}
}
如果加上一个参数的话,加上,Hibernate内置的一个对象Type,这样就可以产生你想要的数据类型,
可以直接写query.setParameter(0, value, type); 这种形式,你可以在任何地方调用这个函数
并且可以带上你想要的类型。而且里面的值都可以是对象数组形式,下面是这种方法的事例代码:
public
List find(String queryString, Object value, Type type)
{
Session session = sessionManage.currentSession();
try {
Query query = session.createQuery(queryString);
query.setParameter(0, value, type);
query.setCacheable(cacheable);
return query.list();
} catch (HibernateException e) {
BaseService.getInstance().getContext().log(
"[PersistMap][find] HibernateException ",
e);
return null;
} finally {
sessionManage.closeSession();
}
}
/* (非 Javadoc)
* @see com.cn001play.general.database.IQueryPersist#find(java.lang.String, java.lang.Object[], net.sf.hibernate.type.Type[])
*/
public List find(String queryString, Object[] values, Type[] types) {
Session session = sessionManage.currentSession();
try {
Query query = session.createQuery(queryString);
for (int i = 0; i < values.length; i++) {
query.setParameter(i, values[i], types[i]);
}
query.setCacheable(cacheable);
return query.list();
} catch (HibernateException e) {
BaseService.getInstance().getContext().log(
"[PersistMap][find] HibernateException ",
e);
return null;
} finally {
sessionManage.closeSession();
}
}
Session session = sessionManage.currentSession();
try {
Query query = session.createQuery(queryString);
query.setParameter(0, value, type);
query.setCacheable(cacheable);
return query.list();
} catch (HibernateException e) {
BaseService.getInstance().getContext().log(
"[PersistMap][find] HibernateException ",
e);
return null;
} finally {
sessionManage.closeSession();
}
}
/* (非 Javadoc)
* @see com.cn001play.general.database.IQueryPersist#find(java.lang.String, java.lang.Object[], net.sf.hibernate.type.Type[])
*/
public List find(String queryString, Object[] values, Type[] types) {
Session session = sessionManage.currentSession();
try {
Query query = session.createQuery(queryString);
for (int i = 0; i < values.length; i++) {
query.setParameter(i, values[i], types[i]);
}
query.setCacheable(cacheable);
return query.list();
} catch (HibernateException e) {
BaseService.getInstance().getContext().log(
"[PersistMap][find] HibernateException ",
e);
return null;
} finally {
sessionManage.closeSession();
}
}