2017. 3. 8.

[Spring] spring domain sort

Spring Domain Sort


A domain model in problem solving and software engineering is a conceptual model of all the topics related to a specific problem. It describes the various entities, their attributes, roles, and relationships, plus the constraints that govern the problem domain. It does not describe solutions to the problem.

즉, 도메인은 프로그램을 위한 요구사항과 기능들을 명시해두는 영역이다. 따라서 스프링에서 제공하는 도메인 객체에서도 이러한 기능들을 명시해놨다고 보면 된다. Sort 또한 스프링 도메인에서 제공하는 기능들 중 하나이다. Sort의 코드를 까보면 아래와 같이 시작한다.

1
2
3
4
5
6
public class Sort implements Iterable<org.springframework.data.domain.Sort.Order>, Serializable {
 private static final long serialVersionUID = 5737186511678863905L;
 public static final Direction DEFAULT_DIRECTION = Direction.ASC;
 private final List<Order> orders;
...
}
cs

이처럼 스프링 도메인의 Order를 상속받아 작성된 것이 Sort.class이다.
Sort.class implements Order object of spring domain.


- Usage

1
2
3
4
Order order1 = new Order(Sort.Direction.DESC, "keyColum");
Order order2 = new Order(Sort.Direction.ASC, "keyColum2");
Iterable<something> list = 
someRepository.findAll(qSomething.id.in(0), new Sort(order1, order2));
cs

여기서는 queryDSL로 쓰는 법만 볼 것이다. queryDSL도 스프링 도메인 객체를 상속받아 사용하고 있으므로 여기서 쓰는 order도 스프링 도메인의 것과 같다. 이렇게 Order형 객체를 만들고, Direction으로 정렬방식을 준다. keyColum은 정렬할 기준컬럼을 의미한다. 만든 정렬객체로 조건을 줄 때는 queryDSL에서 제공하는 findAll함수의 파라미터로 전달하여 쉽게 사용할 수 있다. 즉, (where조건, order by방식) 으로 넘긴다.

I will write only queryDSL here. queryDSL is also implements spring domain object, so order way is same with it. Like upper, make Order object and give 'order by' with Direction. keyColumn means the column as the key to order. when you want to give 'where' something, can deliver in parameters of findAll function. that is, function(WHERE, ORDER BY).

혹은 order객체를 만들지 않고 아래처럼 and로 정렬방식을 줄 수도 있다.
Or can using '.and' that supported from queryDSL.

1
new Sort(order1).and(new Sort(order2);;
cs



댓글 없음:

댓글 쓰기