HQL语句查询中一些参数设置的问题
2007-11-02 12:31
531 查看
以前在用hibernate写查询的时候,都是带两个参数的查询,这样的话不利于放在底层,也就是不利于代码的
重复使用,下面是带两个参数的代码:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public List find(String queryString, String value) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Session session = sessionManage.currentSession();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Query query = session.createQuery(queryString);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
query.setString(0, value);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
query.setCacheable(cacheable);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return query.list();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (HibernateException e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BaseService.getInstance().getContext().log(
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
"[PersistMap][find] HibernateException ",
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} finally ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
sessionManage.closeSession();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
如果加上一个参数的话,加上,Hibernate内置的一个对象Type,这样就可以产生你想要的数据类型,
可以直接写query.setParameter(0, value, type); 这种形式,你可以在任何地方调用这个函数
并且可以带上你想要的类型。而且里面的值都可以是对象数组形式,下面是这种方法的事例代码:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public List find(String queryString, Object value, Type type) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Session session = sessionManage.currentSession();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Query query = session.createQuery(queryString);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
query.setParameter(0, value, type);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
query.setCacheable(cacheable);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return query.list();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (HibernateException e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BaseService.getInstance().getContext().log(
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
"[PersistMap][find] HibernateException ",
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} finally ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
sessionManage.closeSession();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//* (非 Javadoc)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @see com.cn001play.general.database.IQueryPersist#find(java.lang.String, java.lang.Object[], net.sf.hibernate.type.Type[])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public List find(String queryString, Object[] values, Type[] types) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Session session = sessionManage.currentSession();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Query query = session.createQuery(queryString);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for (int i = 0; i < values.length; i++) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
query.setParameter(i, values[i], types[i]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
query.setCacheable(cacheable);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return query.list();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (HibernateException e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BaseService.getInstance().getContext().log(
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
"[PersistMap][find] HibernateException ",
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} finally ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
sessionManage.closeSession();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
重复使用,下面是带两个参数的代码:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public List find(String queryString, String value) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Session session = sessionManage.currentSession();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Query query = session.createQuery(queryString);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
query.setString(0, value);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
query.setCacheable(cacheable);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return query.list();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (HibernateException e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BaseService.getInstance().getContext().log(
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
"[PersistMap][find] HibernateException ",
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} finally ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
sessionManage.closeSession();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
如果加上一个参数的话,加上,Hibernate内置的一个对象Type,这样就可以产生你想要的数据类型,
可以直接写query.setParameter(0, value, type); 这种形式,你可以在任何地方调用这个函数
并且可以带上你想要的类型。而且里面的值都可以是对象数组形式,下面是这种方法的事例代码:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public List find(String queryString, Object value, Type type) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Session session = sessionManage.currentSession();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Query query = session.createQuery(queryString);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
query.setParameter(0, value, type);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
query.setCacheable(cacheable);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return query.list();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (HibernateException e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BaseService.getInstance().getContext().log(
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
"[PersistMap][find] HibernateException ",
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} finally ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
sessionManage.closeSession();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//* (非 Javadoc)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @see com.cn001play.general.database.IQueryPersist#find(java.lang.String, java.lang.Object[], net.sf.hibernate.type.Type[])
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public List find(String queryString, Object[] values, Type[] types) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Session session = sessionManage.currentSession();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Query query = session.createQuery(queryString);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for (int i = 0; i < values.length; i++) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
query.setParameter(i, values[i], types[i]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
query.setCacheable(cacheable);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return query.list();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (HibernateException e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BaseService.getInstance().getContext().log(
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
"[PersistMap][find] HibernateException ",
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} finally ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
sessionManage.closeSession();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
相关文章推荐
- 使用HQL语句查询的时候需要注意的一些问题
- 使用CreateWindowEx()创建窗口的一些参数设置问题
- Android中的SQL查询语句LIKE绑定参数问题解决办法(sqlite数据库)
- Hibernate HQL语句的参数设置
- SSH+HQL语句进行复杂查询的一些小知识点泛谈
- jdbc查询语句中in后传参数问题的解决方案
- 开发日志:HQL关联查询出现语句正确,但一直查询不出结果的问题
- 在myeclipse6.5 中运行main函数测试webservice服务端利用hql语句查询数据库里的数据的时候的问题
- 常用的一些HQL查询语句详解(2)
- eclipse 虚拟机启动参数的一些问题及设置
- hibernate查询语句hql中的占位符?参数与命名参数:name设值方式搞混
- 使用HQL语句的按照参数名字查询数据库信息的时候 “=:”和参数之间不能存在空格,否则会报错
- hibernate--HQL查询语句参数绑定
- android之Android中的SQL查询语句LIKE绑定参数问题解决办法(sqlite数据库)
- SSH框架问题——hql语句查询级联对象(即:查询出包括当前对象内部维护的对象)的方法。
- 常用的一些HQL查询语句详解(1)
- 解决Spring和Hibernate整合时HQL查询语句乱码问题
- 关于在SSH框架整合时,使用hql语句查询出现 XXX is not mapped 的问题
- hibernate HQL查询参数设置
- 常用的一些HQL查询语句详解(3)