下面列出了org.hibernate.criterion.Restrictions#like ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public Criterion toHibernateCriterion() {
Criterion restriction = null;
switch (getMatch()) {
case LIKE:
restriction = Restrictions.like(getName(), (String) getValue(), MatchMode.ANYWHERE);
break;
case ILIKE:
restriction = Restrictions.like(getName(), (String) getValue(), MatchMode.ANYWHERE).ignoreCase();
break;
case NOT_EQ:
restriction = Restrictions.ne(getName(), getValue());
break;
case IN:
restriction = Restrictions.in(getName(), (Object[]) getValue());
break;
case NOT_IN:
restriction = Restrictions.not(Restrictions.in(getName(), (Object[]) getValue()));
break;
default:
restriction = Restrictions.eq(getName(), getValue());
break;
}
return restriction;
}
@SuppressWarnings("unchecked")
public CmsDictionary findByValue(String type,String value){
Criterion cron_type=null,cron_value=null;
if(StringUtils.isNotBlank(type)){
cron_type = Restrictions.like("type",type);
}
if(StringUtils.isNotBlank(value)){
cron_value = Restrictions.like("value",value);
}
List<CmsDictionary>li=findByCriteria(cron_type,cron_value);
if(li!=null&&li.size()>0){
return li.get(0);
}else{
return null;
}
}
/**
* 按属性条件参数创建Criterion,辅助函数.
*/
protected Criterion buildCriterion(final String propertyName, final Object propertyValue, final MatchType matchType) {
AssertUtils.hasText(propertyName, "propertyName不能为空");
Criterion criterion = null;
//根据MatchType构造criterion
switch (matchType) {
case EQ:
criterion = Restrictions.eq(propertyName, propertyValue);
break;
case LIKE:
criterion = Restrictions.like(propertyName, (String) propertyValue, MatchMode.ANYWHERE);
break;
case LE:
criterion = Restrictions.le(propertyName, propertyValue);
break;
case LT:
criterion = Restrictions.lt(propertyName, propertyValue);
break;
case GE:
criterion = Restrictions.ge(propertyName, propertyValue);
break;
case GT:
criterion = Restrictions.gt(propertyName, propertyValue);
break;
case NE:
criterion = Restrictions.ne(propertyName, propertyValue);
}
return criterion;
}
public static Criterion getCategoryCriterionRestriction(CategoryCriterion categoryCriterion) {
Criterion restriction = null;
if (categoryCriterion.prefix != null && categoryCriterion.prefix.length() > 0) {
if (categoryCriterion.caseSensitive) {
restriction = Restrictions.like(categoryCriterion.field, categoryCriterion.prefix, categoryCriterion.matchMode);
} else {
restriction = Restrictions.ilike(categoryCriterion.field, categoryCriterion.prefix, categoryCriterion.matchMode);
}
} else if (EmptyPrefixModes.NON_EMPTY_ROWS.equals(categoryCriterion.emptyPrefixMode)) {
restriction = Restrictions.not(Restrictions.or(Restrictions.eq(categoryCriterion.field, ""), Restrictions.isNull(categoryCriterion.field)));
} else if (EmptyPrefixModes.EMPTY_ROWS.equals(categoryCriterion.emptyPrefixMode)) {
restriction = Restrictions.or(Restrictions.eq(categoryCriterion.field, ""), Restrictions.isNull(categoryCriterion.field));
}
return restriction;
}
@Override
public Criterion getHibernateCriterion( QueryPath queryPath )
{
if ( caseSensitive )
{
return Restrictions.like( queryPath.getPath(), String.valueOf( args.get( 0 ) ).replace( "%", "\\%" ), matchMode );
}
else
{
return Restrictions.ilike( queryPath.getPath(), String.valueOf( args.get( 0 ) ).replace( "%", "\\%" ), matchMode );
}
}
public Pagination getPage(String queryType,int pageNo, int pageSize) {
Criteria crit = createCriteria();
if(StringUtils.isNotBlank(queryType)){
Criterion cron = Restrictions.like("type",queryType);
crit.add(cron);
}
Pagination page = findByCriteria(crit, pageNo, pageSize);
return page;
}
/** {@inheritDoc} */
@Override
public Object visit(PropertyIsLike filter, Object userData) {
String propertyName = getPropertyName(filter.getExpression());
String finalName = parsePropertyName(propertyName, userData);
String value = filter.getLiteral();
value = value.replaceAll("\\*", "%");
value = value.replaceAll("\\?", "_");
if (filter.isMatchingCase()) {
return Restrictions.like(finalName, value);
} else {
return Restrictions.ilike(finalName, value);
}
}
@SuppressWarnings("unchecked")
public List<CmsDictionary> getList(String type){
Criterion cron = Restrictions.like("type",type);
return findByCriteria(cron);
}
public Criterion build(String propertyName, Object value) {
return Restrictions.like(propertyName, value.toString(), MatchMode.ANYWHERE);
}
/**
* 按属性条件参数创建Criterion,辅助函数.
*
* @param propertyName
* String
* @param propertyValue
* Object
* @param matchType
* MatchType
* @return Criterion
*/
public static Criterion buildCriterion(String propertyName,
Object propertyValue, MatchType matchType) {
Assert.hasText(propertyName, "propertyName不能为空");
Criterion criterion = null;
// 根据MatchType构造criterion
switch (matchType) {
case EQ:
criterion = Restrictions.eq(propertyName, propertyValue);
break;
case NOT:
criterion = Restrictions.ne(propertyName, propertyValue);
break;
case LIKE:
criterion = Restrictions.like(propertyName, (String) propertyValue,
MatchMode.ANYWHERE);
break;
case LE:
criterion = Restrictions.le(propertyName, propertyValue);
break;
case LT:
criterion = Restrictions.lt(propertyName, propertyValue);
break;
case GE:
criterion = Restrictions.ge(propertyName, propertyValue);
break;
case GT:
criterion = Restrictions.gt(propertyName, propertyValue);
break;
case IN:
criterion = Restrictions.in(propertyName,
(Collection) propertyValue);
break;
case INL:
criterion = Restrictions.isNull(propertyName);
break;
case NNL:
criterion = Restrictions.isNotNull(propertyName);
break;
default:
criterion = Restrictions.eq(propertyName, propertyValue);
break;
}
return criterion;
}
public Criterion build(String propertyName, Object value) {
return Restrictions.like(propertyName, value.toString(), MatchMode.END);
}
public Criterion build(String propertyName, Object value) {
return Restrictions.like(propertyName, value.toString(), MatchMode.START);
}