1、先注册该查询函数:
import org.hibernate.dialect.MySQL5Dialect;
import org.hibernate.dialect.function.SQLFunctionTemplate;
import org.hibernate.type.StandardBasicTypes;
public class CustomerMysqlDialect extends MySQL5Dialect {
public CustomerMysqlDialect() {
super();
// 注册mysql全文搜索
registerFunction("match_against",
new SQLFunctionTemplate(StandardBasicTypes.DOUBLE, "MATCH(?1) AGAINST(?2 IN BOOLEAN MODE)"));
}
}
2、配置该CustomerMysqlDialect
spring.jpa.properties.hibernate.dialect=com.example.CustomerMysqlDialect
3、使用该方法:
private List<Predicate> getPredicatesByCriteria(Root<OrderInfo> root, OrderInfoQueryCriteria criteria, CriteriaBuilder criteriaBuilder) {
List<Predicate> predicateList = CollUtil.newArrayList();
if(StrUtil.isNotBlank(criteria.getMailNo())){
Expression<Double> matchAgainst = criteriaBuilder.function("match_against", Double.class,
root.get("mailNo"), criteriaBuilder.literal(criteria.getMailNo()));
Predicate mailNoCondition = criteriaBuilder.greaterThan(matchAgainst, 0.0);
predicateList.add(mailNoCondition);
}
return predicateList;
}