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,就是这如何给他们匹配上呢,求教 多谢大家 感谢。 展开
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();
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();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询