Spring/additional

[Spring] spring data jpa between

sujin7837 2021. 1. 29. 16:32
반응형

1. LocalDate & LocalTime

LocalDate: 적용 기간

LocalTime: 적용 시간

LocalDateTime: 적용 기간 및 시간

//적용 기간
@Convert(converter=LocalDateConverter.class)
private LocalDate startDate;

@Convert(converter=LocalDateConverter.class)
private LocalDate endDate;

//적용 시간
@Column(columnDefinition="TIME")
@Convert(converter=LocalTimeConverter.class)
private LocalTime startTime;

@Column(columnDefinition="TIME")
@Convert(converter=LocalTimeConverter.class)
private LocalTime endTime;

 

2. Data JPA Between 

24시간 후에 사라지는 글 생성에 이용 가능합니다.

 

@Entity(name="test_timeservice")
@Getter
@NoArgsConstructor(access=AccessLevel.PROTECTED)
public class Timeservice {
	@Id
    @GeneratedValue
    @Column(name="time_id")
    private Long timeId;
    
    @Column
    private LocalDateTime regDate;
}
public interface TimeserviceRepository extends JpaRepository<Timeservice, Long> {
	List<Timeservice> findAllByRegDateBetween(LocalDateTime start, LocalDateTime end);
}

 

LocalDateTime.now(): 지금

LocalDateTime.now().minusDays(1): 하루 전

public void test() {
	LocalDateTime startDateTime=LocalDateTime.of(LocalDate.now(), minusDays(1), LocalTime.of(0, 0, 0));
    LocalDateTime endDateTime=LocalDateTime.of(LocalDate.now(), LocalTime.of(23, 59, 59));
    
    timeserviceRepository.findAllByRegDateBetween(startDateTime, endDateTime);
}
반응형