HQL查询在其中一个参数传入一个list的时候如何对该list做模糊查询呢?

publicintcount(QueryTypequeryType,List<String>source,DatestartDate,DateendDate,inttyp... public int count(QueryType queryType ,List<String> source,Date startDate,Date endDate,int type){
String hql = "select count(*) from AlarmBean a where a.source in (:list) and a.lastdate>(:d1) and lastdate<(:d2) and a.typeId = (:id);"

List<Long> = getHibernateTemplate().execute(new HibernateCallback<List>()){
public List doInHibernate(Session session){
Query query = session.createQuery(hql);

query.setParameterList("list",source);

query.setTimestamp("d1",startDate);

}

}
}
就是这 假如说我传进来的List是{FS001,FS002} 而数据库里面是baheaFS001,baheaFS002,就是这如何给他们匹配上呢,求教 多谢大家 感谢。
展开
 我来答
匿名用户
推荐于2016-11-06
展开全部
一、绑定参数法:

Sql代码
Session session=HibernateUtil.getSessionFactory
().getCurrentSession();
session.beginTransaction();
String strSQL="from Classes as a where a.classno like :name";
Query query = session.createQuery(strSQL);
query.setString("name", "%"+OId+"%");
List result=query.list();
[sql] view plaincopyprint?
Session session=HibernateUtil.getSessionFactory
().getCurrentSession();

session.beginTransaction();

String strSQL="from Classes as a where a.classno like :name";

Query query = session.createQuery(strSQL);

query.setString("name", "%"+OId+"%");

List result=query.list();
Session session=HibernateUtil.getSessionFactory
().getCurrentSession();

session.beginTransaction();

String strSQL="from Classes as a where a.classno like :name";

Query query = session.createQuery(strSQL);

query.setString("name", "%"+OId+"%");

List result=query.list();
二、在面向对象的查询语言 HQL 中

Sql代码
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List result=session.createQuery("from Classes as a where a.classno
like " '%"+OId+"%'").list();
[sql] view plaincopyprint?
Session session = HibernateUtil.getSessionFactory().getCurrentSession();

session.beginTransaction();

List result=session.createQuery("from Classes as a where a.classno

like " '%"+OId+"%'").list();
Session session = HibernateUtil.getSessionFactory().getCurrentSession();

session.beginTransaction();

List result=session.createQuery("from Classes as a where a.classno

like " '%"+OId+"%'").list();
三、模糊查询参数化

Sql代码
Session session=HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List result=session.createQuery("from Classes as a where a.classno
like :name").setParameter("pid",OId).list();
[sql] view plaincopyprint?
Session session=HibernateUtil.getSessionFactory().getCurrentSession();

session.beginTransaction();

List result=session.createQuery("from Classes as a where a.classno

like :name").setParameter("pid",OId).list();
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式