HQL语句查询中一些参数设置的问题
来源:互联网 发布:mac win 截图 编辑:程序博客网 时间:2024/06/04 19:22
以前在用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();
}
}
- HQL语句查询中一些参数设置的问题
- hibernate HQL语句的参数设置
- Hibernate HQL语句的参数设置
- 使用HQL语句查询的时候需要注意的一些问题
- hibernate HQL查询参数设置
- hibernate HQL查询参数设置
- hibernate HQL查询参数设置
- 常用的一些HQL查询语句详解(1)
- 常用的一些HQL查询语句详解(2)
- 常用的一些HQL查询语句详解(3)
- 项目中遇到的HQL查询问题
- Hibernate的HQL中in参数设置
- Hibernate的HQL中in参数设置
- Hibernate的HQL中in参数设置
- Hibernate的HQL中in参数设置
- hibernate的HQL中in参数设置
- Hibernate的HQL中in参数设置
- Hibernate的HQL中in参数设置
- 用于查询当前数据库中所有表格的记录条数的脚本
- 五种提高 SQL 性能的方法
- 程序加载时的旋转进度圈组件
- “/”应用程序中的服务器错误 之 无法识别的配置节“connectionStrings”
- Java中的UTF-8
- HQL语句查询中一些参数设置的问题
- 英倫佳麗 Babes in Pornland 15: British Babes
- Web服务器搭建与安全
- RHEL AS4 U2下安装32位ORACLE10G
- Js的MessageBox
- 华为(2007年3月)一道面试题: c语言删除一个字符串中的第i个字符后的j个字符
- 向其他进程注入代码的三种方法
- 奥运门票可以网上卖,火车票为什么不行
- D语言介绍