[JSCODE] 3ํšŒ์ฐจ

Hanjmoยท2023๋…„ 5์›” 17์ผ
0

๐ŸŽฏ ๋ชฉํ‘œ

  • ์ •๋ ฌ ๊ธฐ๋Šฅ
  • ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ
  • ๋ฐ์ดํ„ฐ ์กฐํšŒ ์‹œ limit ๊ฑธ๊ธฐ
  • createdAt ์ปฌ๋Ÿผ ๋ฐฐ์šฐ๊ธฐ

โœ๏ธ ๋‚ด์šฉ

1. @createdDate

Level 3์— ๊ฒŒ์‹œ๊ธ€์— ์ƒ์„ฑ ์‹œ๊ฐ„์„ ์ €์žฅํ•˜๋ผ๋Š” ์š”๊ตฌ ์‚ฌํ•ญ์ด ์ถ”๊ฐ€๋˜์—ˆ๋‹ค.
๊ทธ๋ž˜์„œ createdTime ํ•„๋“œ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ–ˆ๋‹ค.

@Column(name = "created_time", updatable = false)
    private LocalDateTime createdTime;

updatable์„ false๋กœ ๋‘์–ด ์ˆ˜์ •์„ ๋ชปํ•˜๊ฒŒ ๋ง‰์•˜๊ณ , LocalDateTime.now()๋ฅผ ํ†ตํ•ด์„œ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ์‹œ์ ์— ์ดˆ๊ธฐํ™”๋ฅผ ํ•ด์ฃผ์—ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ํ›„์— @createdDate๋ฅผ ์•Œ๊ฒŒ ๋˜์—ˆ๋Š”๋ฐ, ์ด ์• ๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด ๋”ฐ๋กœ ์ดˆ๊ธฐํ™” ํ•ด์ค„ ํ•„์š” ์—†์ด ์ž๋™์œผ๋กœ ์ƒ์„ฑ ์‹œ๊ฐ„์ด ์ €์žฅ๋œ๋‹ค.

@createdDate๋ฅผ ์•Œ๊ฒŒ ๋˜๋ฉด์„œ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ Auditing์ด๋ผ๋Š” ๊ฐœ๋…์ด ์žˆ๋‹ค๋Š” ๊ฒƒ๋„ ์•Œ๊ฒŒ ๋˜์—ˆ๊ณ , ์ด์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ•œ ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฆฌํŒฉํ† ๋งํ•  ์˜ˆ์ •์ด๋‹ค.

@Getter
@MappedSuperClass
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseTimeEntity {
	@CreatedDate
    @Column(updatable = false)
    private LocalDateTime createdDate;
    
    @LastModifiedDate
    private LocalDateTime modifiedDate;

2. ์ •๋ ฌ ๊ธฐ๋Šฅ

์•„์ง SpringDataJPA๊ฐ€ ์•„๋‹Œ JPQL์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ •๋ ฌ ๊ธฐ๋Šฅ์„ jpql๋กœ ์ง์ ‘ ์ž‘์„ฑํ–ˆ๋‹ค.
์ง์ ‘ ์ž‘์„ฑํ•œ String jpql = "select p from Post p";์—
jpql += " order by p.createdTime desc";๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ •๋ ฌ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ–ˆ๋‹ค.

3. ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ

๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ๋„ jpql์„ ์‚ฌ์šฉํ•ด์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ–ˆ๋‹ค.

public List<Post> findAll(PostSearch postSearch) {
	String jpql = "select p from Post p";

	if (postSearch.getTitle() != null) {
		jpql += " where p.title like :title";
	}

	jpql += " order by p.createdTime desc";

	TypedQuery<Post> query = em.createQuery(jpql, Post.class);

	if (postSearch.getTitle() != null) {
		query.setParameter("title", "%" + postSearch.getTitle() + "%");
	}

	return query.setMaxResults(100).getResultList();
}

๊ฐ„๋žตํ•˜๊ฒŒ ์„ค๋ช…ํ•˜์ž๋ฉด, ์šฐ์„  ์ปจํŠธ๋กค๋Ÿฌ์—์„œ @RequestParam์„ ํ†ตํ•ด ํ‚ค์›Œ๋“œ๋ฅผ ๊ฐ€์ ธ์˜จ ๋‹ค์Œ PostSearch ํด๋ž˜์Šค์˜ title ํ•„๋“œ์— ๊ฐ’์„ ์ฃผ์ž…ํ•œ๋‹ค.
๊ทธ๋ฆฌ๊ณ ๋‚˜์„œ PostSearch์—์„œ title์„ ๊ฐ€์ ธ์™€ setParameter()๋กœ ํŒŒ๋ผ๋ฏธํ„ฐ ๋งคํ•‘๊นŒ์ง€ ํ•ด์ฃผ์—ˆ๋‹ค.

4. ๊ฐœ์ˆ˜ ์ œํ•œ

์ฒ˜์Œ์—๋Š” ์ฟผ๋ฆฌ์— limit์„ ์ง์ ‘ ์ž‘์„ฑํ•˜์˜€๋‹ค.
ํ•˜์ง€๋งŒ ์ด๋ ‡๊ฒŒ ๋ฌธ์ž์—ด์„ ์ง์ ‘ ์ž‘์„ฑํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด, ์˜คํƒ€๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์–ด๋””์„œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋Š”์ง€ ์ฐพ๊ธฐ๊ฐ€ ์–ด๋ ต๋‹ค๋Š” ๋‹จ์ ์ด ์กด์žฌํ•œ๋‹ค.

๋”ฐ๋ผ์„œ setMaxResults()๋ฅผ ํ†ตํ•ด์„œ ๊ฐœ์ˆ˜ ์ œํ•œ์„ ๊ฑฐ๋Š” ๋ฐฉ์‹์œผ๋กœ ์ˆ˜์ •ํ•˜์˜€๋‹ค.
์ด๋ ‡๊ฒŒ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ์˜คํƒ€๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ IDE์—์„œ ์ปดํŒŒ์ผ ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ธฐ ๋•Œ๋ฌธ์— ์–ด๋””์„œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋Š”์ง€ ์‰ฝ๊ฒŒ ์ฐพ์„ ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

"๊ฐœ๋ฐœ์ž์—๊ฒŒ ๊ฐ€์žฅ ์ข‹์€ ์—๋Ÿฌ๋Š” ์ปดํŒŒ์ผ ์—๋Ÿฌ์ด๋‹ค."

๐Ÿ’ฌ ํšŒ๊ณ 

jpql์„ ์ง์ ‘ ์ž‘์„ฑํ•˜๋ฉด์„œ ๊ตฌํ˜„ํ•˜๋‹ค๊ฐ€ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ๋ฐœ์ƒ ์ง€์ ์„ ์ฐพ๋Š” ๊ฒƒ์ด ์‰ฝ์ง€ ์•Š์•˜๋‹ค.
SpringDataJPA๋ฅผ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ๋ฅผ ๋ดค๋Š”๋ฐ, ํ™•์‹คํžˆ ์ฝ”๋“œ๊ฐ€ ํ›จ์”ฌ ๊ฐ„๋‹จํ•ด์ง€๋Š”๋ฐ๋‹ค๊ฐ€ ๊ฐ€๋…์„ฑ๊นŒ์ง€ ์ข‹์•„์ง„๋‹ค๋Š” ์žฅ์ ์„ ๊นจ๋‹ซ๊ฒŒ ๋˜์—ˆ๋‹ค.

SpringDataJPA์™€ ์ƒ์„ฑ ์‹œ๊ฐ„์— ๋Œ€ํ•œ ๊ฐœ๋…์€ ๋น ๋ฅธ ์‹œ์ผ ๋‚ด๋กœ ํ•™์Šตํ•˜๊ณ , ๋ฆฌํŒฉํ† ๋งํ•ด์•ผ๊ฒ ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€