如何在MySQL中为hibernate中的select查询添加regexp。我正在使用ajax数据表。我的项目是在Spring 4 MVC和Hibernate与MySQL。 我已经使用MongoDB在hibernate中创建了这个,但我不知道如何在MySQL中使用。
使用mongodb代码进行休眠:
Query query = new Query();
query.addCriteria(Criteria.where("isDeleted").is(IsDeletedEnum.NOTDELETED));
String search = (String) rowStartIdxAndCount[4];
if (StringUtils.isNotBlank(search)) {
String searchExp = ".*" + search + ".*";
Criteria searchCriteria = new Criteria();
searchCriteria.orOperator(Criteria.where("name").regex(searchExp, "i"),
Criteria.where("mobileno").regex(searchExp, "i"),
Criteria.where("email").regex(searchExp, "i"),
Criteria.where("role").regex(searchExp, "i"));
query.addCriteria(searchCriteria);
}
我正在尝试使用MySQL进行Hibernate。但不知道接下来我要写什么?我必须在名称,mobileno,电子邮件和角色字段中添加正则表达式,它与或运算符一起使用。
Session session = getSessionFactory().openSession();
session.beginTransaction();
String search = (String) rowStartIdxAndCount[4];
String searchExp = ".*" + search + ".*";
String hql ="SELECT * FROM UserBean WHERE isDeleted=isDeleted OR name REGEXP 'searchExp''i'";
Query query = session.createQuery(hql);
query.setParameter("isDeleted", IsDeletedEnum.NOTDELETED);
那怎么办呢?请帮帮我。
答案 0 :(得分:4)
逐个制定标准:
criteria1
.setProjection(Projections.property("name"))
.add(Restrictions.sqlRestriction("REGEXP pattern")
criteria2
.setProjection(Projections.property("mobileno"))
.add(Restrictions.sqlRestriction("REGEXP pattern")
criteria3
.setProjection(Projections.property("email"))
.add(Restrictions.sqlRestriction("REGEXP pattern")
criteria4
.setProjection(Projections.property("role"))
.add(Restrictions.sqlRestriction("REGEXP pattern")
之后将这些标准添加到休眠查询中:
query.addCriteria(criteria1);
query.addCriteria(criteria2);
query.addCriteria(criteria3);
query.addCriteria(criteria4);