Spring Data JPA 多条件查询

Spring Data JPA 查询很方便,但做搜索功能时,条件都是未知的,并不能用方法命名的方式查询,自己写JPQL,用表达式的方式处理也不靠谱,条件多了简直就是作死,以前写过下面的代码:

SELECT * FROM UserModel AS u WHERE u.sex = :sex AND u.age = :age
变化后
SELECT * FROM UserModel AS u WHERE (u.sex = :sex OR :sex == null) AND (u.age = :age OR :age == null)

正确解决办法是 JpaSpecificationExecutor 接口,这个接口中有5个方法

T findOne(Specification<T> spec);
List<T> findAll(Specification<T> spec);
Page<T> findAll(Specification<T> spec, Pageable pageable);
List<T> findAll(Specification<T> spec, Sort sort);
long count(Specification<T> spec);

Dao

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

import com.github.ckwen.je.spring.boot.security.model.EmployeeModel;

public interface EmployeeRepository extends JpaRepository, JpaSpecificationExecutor {

}

Service

@Override
public List findSearch(EmployeeModel model) {

    Assert.notNull(model);

    List result = employeeRepository.findAll(new Specification() {
        @Override
        public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
            List list = new ArrayList();

            if (StringUtils.isNoneBlank(model.getName())) {
                list.add(cb.like(root.get("name").as(String.class), "%" + model.getName() + "%"));
            }

            if (model.getGender() != null) {
                list.add(cb.equal(root.get("gender").as(GenderType.class), model.getGender()));
            }

            if (StringUtils.isNotBlank(model.getTelPhone())) {
                list.add(cb.like(root.get("telPhone").as(String.class), "%" + model.getTelPhone() + "%"));
            }

            if (model.getDepartment() != null && model.getDepartment().getCode() != null) {
                list.add(cb.equal(root.get("department").as(DepartmentModel.class), model.getDepartment()));
            }

            Predicate[] p = new Predicate[list.size()];
            return cb.and(list.toArray(p));
        }

    });

    return result;
}

Junit

@Autowired
private EmployeeService employeeService;

@Test
public void test() {

    EmployeeModel model = new EmployeeModel();
    model.setName("33");
    model.setGender(GenderType.MALE);

    List rs = employeeService.findSearch(model);

    for (EmployeeModel employeeModel : rs) {
        System.out.println(employeeModel);
    }
}

@Test
public void test1() {

    EmployeeModel model = new EmployeeModel();
    model.setDepartment(new DepartmentModel("AAA"));

    List rs = employeeService.findSearch(model);

    for (EmployeeModel employeeModel : rs) {
        System.out.println(employeeModel);
    }
}

你可能感兴趣的:(Spring)