bug
zhangzeli
2022-01-18 90d61b2122cfc96779bb658ca6f28e86540de128
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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<Role> findByDefaultRole(Boolean defaultRole) {
        return roleDao.findByDefaultRole(defaultRole);
    }
 
    @Override
    public Page<Role> findByCondition(String key, Pageable pageable) {
 
        return roleDao.findAll(new Specification<Role>() {
            @Nullable
            @Override
            public Predicate toPredicate(Root<Role> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
 
                Path<String> nameField = root.get("name");
                Path<String> descriptionField = root.get("description");
 
                List<Predicate> 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);
    }
}