Hibernate 怎样实现多条件模糊查询

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate 怎样实现多条件模糊查询相关的知识,希望对你有一定的参考价值。

参考技术A // 条件查询用户信息,
public List<User> searchUser(final String username, final int userId,
final String lastVisitIP, final Date fromCreateDate,
final Date toCreateDate, final Date fromVisitDate,
final Date toVisitDate, final String isdesc)
List<User> users = (List<User>) this.hibernateTemplate
.execute(new HibernateCallback()
public Object doInHibernate(Session session)
throws HibernateException, SQLException
Criteria cr = session.createCriteria(User.class);
System.out.println("用户ID" + userId);
if (userId > 0)
cr.add(Expression.eq("id", userId));
else
if (username != null && !username.equals(""))
cr.add(Expression.like("username", "%"
+ username + "%"));

if (lastVisitIP != null && !lastVisitIP.equals(""))
cr.add(Expression.like("lastVisitIP",
lastVisitIP + "%"));

System.out.println("时间" + fromCreateDate + " || "
+ toCreateDate + " || " + fromVisitDate
+ " || " + toVisitDate);
Date fromdate = new Date();
fromdate.setYear(02);
Date todate = new Date();
todate.setYear(998);
if (!fromCreateDate.before(fromdate)
&& !toCreateDate.after(todate))
cr.add(Restrictions.between("registTime",
fromCreateDate, toCreateDate));

if (!fromVisitDate.before(fromdate)
&& !toVisitDate.after(todate))

cr.add(Restrictions.between("lastVisitTime",
fromVisitDate, toVisitDate));


if (isdesc != null && !isdesc.equals(""))
if (isdesc.equals("true"))
cr.addOrder(Order.desc("id"));
else
cr.addOrder(Order.asc("id"));






return cr.list();


);
for (User user : users)
System.out.println(user.getUsername() + " 封妻荫子" + user.getId());

return users;


曾经做的多条件查询,觉得有用的,就研究下吧!
这是模糊的 cr.add(Expression.like("username", "%" + username + "%"));
参考技术B 一、绑定参数法:

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();
参考技术C SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session=sessionFactory.openSession();
Ccriteria criteria=session.createCriteria(Login.class);//Login实体类名
criteria.add(Restrictions.like("uname","刘%"));//查找姓刘的
参考技术D 用 or关键字追问

你有代码吗?给看看完整的代码吧!

追答

你不是like 了吗,中间再用or连接就实现了
from Department d where d.dataPrivilege like '%"+did+"%' and d.rank like '%大队%'"
自己改下。我这个是hql,sql也一样

追问

是用or还是and啊?

追答

、、、and、不好意思。后面的hql是对的、按后面这个

追问

谢了啊!

追答

不用。大家都一样。多交流

Hibernate的QBC查询有多个条件模糊查询怎么实现的?

Criteria查询,有多个条件,名称,描述,价格,数量....模糊查询,条件都有可能为空,
请问是先把条件封装了再调用Criteria查询方法吗? 具体怎么实现的呢?

用不用框架思路都差不多 遍历所有需要判断的属性 如果有值就添加条件  无值就下一个


for(属性 xxx:全部属性)

    if(xxx!=null)
        criteria.add(Restrictions.like(xxx,yyyy));

criteria.list();

参考技术A Criteria criteria = this.getSession().createCriteria(Fen.class);
criteria.createAlias("emp", "e");
criteria.add(Restrictions.like("e.company","%"+fq.getEmp().getCompany()+"%"));

以上是关于Hibernate 怎样实现多条件模糊查询的主要内容,如果未能解决你的问题,请参考以下文章

jsp如何按指定条件进行模糊查询

python mongoDB 模糊查询(多条件)

用js实现表格多条件模糊查询,可兼容分页

JSP+Servlet+javabean实现页面多条件模糊查询

JSP+Servlet+javabean+oracle实现页面多条件模糊查询

Redis实现分页和多条件模糊查询方案