package cn.exrick.xboot.core.serviceimpl; import cn.exrick.xboot.core.dao.RoleDao; import cn.exrick.xboot.core.entity.Role; import cn.exrick.xboot.core.service.RoleService; import cn.hutool.core.util.StrUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.domain.Specification; import org.springframework.lang.Nullable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.persistence.criteria.*; import java.util.ArrayList; import java.util.List; /** * 角色接口实现 * @author Exrickx */ @Slf4j @Service @Transactional public class RoleServiceImpl implements RoleService { @Autowired private RoleDao roleDao; @Override public RoleDao getRepository() { return roleDao; } @Override public List findByDefaultRole(Boolean defaultRole) { return roleDao.findByDefaultRole(defaultRole); } @Override public Page findByCondition(String key, Pageable pageable) { return roleDao.findAll(new Specification() { @Nullable @Override public Predicate toPredicate(Root root, CriteriaQuery cq, CriteriaBuilder cb) { Path nameField = root.get("name"); Path descriptionField = root.get("description"); List list = new ArrayList<>(); // 模糊搜素 if (StrUtil.isNotBlank(key)) { Predicate p1 = cb.like(nameField, '%' + key + '%'); Predicate p2 = cb.like(descriptionField, '%' + key + '%'); list.add(cb.or(p1, p2)); } Predicate[] arr = new Predicate[list.size()]; cq.where(list.toArray(arr)); return null; } }, pageable); } }