Hibernate 怎样实现多条件模糊查询
展开全部
// 条件查询用户信息,
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 + "%"));
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 + "%"));
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
ZESTRON
2024-09-04 广告
2024-09-04 广告
在Dr. O.K. Wack Chemie GmbH,我们高度重视ZESTRON的表界面分析技术。该技术通过深入研究材料表面与界面的性质,为提升产品质量与可靠性提供了有力支持。ZESTRON的表界面分析不仅涵盖了相变化、化学反应、吸附与解吸...
点击进入详情页
本回答由ZESTRON提供
展开全部
一、绑定参数法:
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();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session=sessionFactory.openSession();
Ccriteria criteria=session.createCriteria(Login.class);//Login实体类名
criteria.add(Restrictions.like("uname","刘%"));//查找姓刘的
Session session=sessionFactory.openSession();
Ccriteria criteria=session.createCriteria(Login.class);//Login实体类名
criteria.add(Restrictions.like("uname","刘%"));//查找姓刘的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用 or关键字
更多追问追答
追问
你有代码吗?给看看完整的代码吧!
追答
你不是like 了吗,中间再用or连接就实现了
from Department d where d.dataPrivilege like '%"+did+"%' and d.rank like '%大队%'"
自己改下。我这个是hql,sql也一样
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询