用注解来代替jpa查询
// 实体
@Entity
class User {
private Long id;
private String name;
private String phone;
private Date createTime;
@OneToOne
private Address address;
}
@Entity
class Address {
private Long id;
private String city;
}
interface UserRepository extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {}
interface AddressRepository extends JpaRepository<Address, Long>, JpaSpecificationExecutor<Address> {}
@SortConfigs({
@SortConfig(column = "createTime", desc = true)
})
@FetchConfig({
@FetchConfig(column = "address", join = JoinType.INNER)
})
class Query {
// 模糊查询姓名
@QueryConfig(column = "name", type = QueryType.like)
private String name;
// 关联实体查询
@QueryConfig(column = "address.city", type = QueryType.eq)
private String city;
// 模糊查询姓名或者手机号
@QueryConfigs(value = {
@QueryConfig(column= "name", type = QueryType.like),
@QueryConfig(column= "phone", type = QueryType.like)
}, isAnd = false)
private String keyword;
}
class UserService {
private UserRepository userRepository;
public List<User> query(Query query){
List<User> userList = userRepository.findAll(QueryFactory.build(request);
return user;
}
public <T> List<T> query(Query query, Converter<User, T> converter){
List<T> userList = userRepository.findAll(QueryFactory.build(request))
.stream()
.map(converter)
.collect(Collections.toList());
return user;
}
}