单表查询
//select * from s_user where username like '%zt%' and id > 3
List<SUser> sUserList = sUserDao.findAll((root, query, cb) -> {
//root.get("username")表示获取username这个字段名称,like表示执行like查询,%zt%表示值
Predicate p1 = cb.like(root.get("username"), "%zt%");
Predicate p2 = cb.greaterThan(root.get("id"), 3);
//将两个查询条件联合起来之后返回Predicate对象,username模糊查询,id>3
return cb.and(p1, p2);
});
//select * from s_user where (id = 2 or id = 3) and (email like 'zt%' or username like 'foo%')
//第一个Specification定义了两个or的组合
Specification<SUser> s1 = (root, query, cb) -> {
Predicate p1 = cb.equal(root.get("id"), 2);
Predicate p2 = cb.equal(root.get("id"), 3);
return cb.or(p1, p2);
};
//第二个Specification定义了两个or的组合
Specification<SUser> s2 = (root, query, cb) -> {
Predicate p1 = cb.like(root.get("email"), "kk%");
Predicate p2 = cb.like(root.get("username"), "foo%");
return cb.or(p1, p2);
};
//通过Specifications将两个Specification连接起来,第一个条件加where,第二个是and
List<SUser> sUserList = sUserDao.findAll(Specifications.where(s1).and(s2));
//实例根据订单状态筛选订单列表
Specification<Orders> spec = (root, query, cb) -> {
List<Predicate> predicates = Lists.newArrayList();
if (StringUtils.isNotEmpty(state)) {
if ("return".equals(state)) {
predicates.add(cb.and(
cb.isTrue(root.get("returned")),
cb.or(
cb.equal(root.get("state"), String.valueOf(STATE_SEND)),
cb.equal(root.get("state"), String.valueOf(STATE_RECEIVE))
)));
} else {
predicates.add(cb.and(cb.equal(root.get("state"), state)));
}
}
if (StringUtils.isNotEmpty(id)) {
predicates.add(cb.like(root.get("id"), "%" + id.trim() + "%"));
}
predicates.add(cb.equal(root.get("shopId"), shop.getId()));
return cb.and(predicates.toArray(new Predicate[predicates.size()]));
};
Pageable pageable = new PageRequest(pageNumber, pageSize, new Sort(Sort.Direction.DESC, "createTime"));
Page<Orders> ordersList = ordersService.findAll(spec, pageable);
多表查询
/**
* 构建查询条件 三张表内联查询实例
*
* @param categoryId 分类id
* @param name 商品名称
* @param shopId 店铺id
* @return 查询条件
*/
public Specification<ProductGroup> buildProductGroupSpec(Integer shopId, String name, Integer categoryId, Integer shopKindId, Boolean health) {
return (root, query, cb) -> {
Join<ProductGroup, Shop> shopJoin = root.join("shop", JoinType.INNER);
Join<Shop, ShopDatum> shopDatumJoin = shopJoin.join("shopDatum", JoinType.INNER);
Join<Shop, ShopConfig> shopConfigJoin = shopJoin.join("shopConfig", JoinType.INNER);
List<Predicate> predicates = Lists.newArrayList();
if (shopId != null) {
predicates.add(cb.equal(root.get("shop"), shopId));
}
if (StringUtils.isNotEmpty(name)) {
predicates.add(cb.like(root.get("name"), "%" + name.trim() + "%"));
}
if (categoryId != null) {
predicates.add(cb.equal(root.get("category"), categoryId));
}
if (shopKindId != null && shopKindId != 0) {
predicates.add(cb.like(root.get("shopKindIds"), "," + shopKindId + ","));
}
predicates.add(cb.equal(root.get("auditState"), AUDIT_STATE_YES.getKey()));
predicates.add(cb.equal(root.get("offline"), Boolean.FALSE));
predicates.add(cb.greaterThanOrEqualTo(shopDatumJoin.get("openShopExpire"), new Date()));
predicates.add(cb.equal(shopConfigJoin.get("openShop"), Boolean.TRUE));
Predicate p1 = cb.equal(root.get("health"), Boolean.FALSE);
Predicate p2 = cb.equal(root.get("health"), Boolean.TRUE);
Predicate p3 = cb.greaterThanOrEqualTo(shopDatumJoin.get("openHealthShopExpire"), new Date());
if (health == null) {
predicates.add(cb.or(p1, cb.and(p2, p3)));
}
if (health != null) {
if (health) {
predicates.add(cb.and(p2, p3));
} else {
predicates.add(cb.and(p1));
}
}
return cb.and(predicates.toArray(new Predicate[predicates.size()]));
};
}
//两张表内联,排序我的是
Specification<User> spec = (root, query, cb) -> {
Join<User, UserAsset> userJoin = root.join("userAsset", JoinType.INNER);
List<Predicate> predicates = Lists.newArrayList();
predicates.add(cb.gt(userJoin.get("wpoint"), 0));
cb.and(predicates.toArray(new Predicate[predicates.size()]));
query.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
query.orderBy(cb.desc(userJoin.get("wpoint")));
return query.getRestriction();
};