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

2024-11-30 00:29:41
推荐回答(4个)
回答1:

// 条件查询用户信息,
public List 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 users = (List) 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 + "%"));

回答2:

一、绑定参数法:

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();

回答3:

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session=sessionFactory.openSession();
Ccriteria criteria=session.createCriteria(Login.class);//Login实体类名
criteria.add(Restrictions.like("uname","刘%"));//查找姓刘的

回答4:

用 or关键字