note of the day, 21.10.06

๐ Repository / Dto / Entity ๊ฐ๋
https://haem-jsp.tistory.com/57
DAO(Repository), DTO, Entity ๋?
DAO(Data Access Object) Repository Package๋ฅผ ์๋ฏธํ๋ฉฐ, ํ๋ก์ ํธ์ XxxxRepository ๋ก ํํํ๋ ๊ฒฝ์ฐ๊ฐ ํ๋ค. DAO๋ ์ค์ ๋ก DB์ ์ ๊ทผํ๋ ๊ฐ์ฒด๋ฅผ ์๋ฏธํ๋ค. ์ ๋ฆฌํ์๋ฉด, XxxxRepository๋ DB์ ๋ฐ์ดํฐ๋ฅผ CRUD(..
haem-jsp.tistory.com
๊น๋จน๊ธฐ์ ์ ํธ๋ค๋ฅ ์์ฑํ๋,,
๐ Search/Create/Update (get/post/patch) API๋ฅผ ์ํด BE ๊ฐ๋ฐํด์ผํ๋ ๊ฒ / ์์ ๋ฆฌ๋ง์ธ๋
์ ๊ทผ ์์๋ 1->4 / ๊ฐ๋ฐํ ๋ ํธํ ์์๋ 4->1
1. Controller์ API๋ฅผ ์์ฒญ๋ฐ๋ ๋ฉ์๋ ์์ฑ
- @RequestMapping ์ด์ฉ
@RequestMapping(value = '/path',
method = RequestMethod.GET, // RequestMethod.POST, RequestMethod.PATCH
produces = 'application/json;charset=UTF-8)
- get ๋ฐฉ์์ผ๋ก ํธ์ถํ ๋๋ @RequestParam() ์ ์ด์ฉ, post/patch์๋ @RequestBody ์ด์ฉ
- @RequestBody๋ฅผ ์ด์ฉํ ๋์๋ ํ์์ ๋ง์ถฐ dto ํด๋์ค(@Data)๋ฅผ ์์ฑ
2. Service์ para/return ํ์ ๋ง์ถฐ Repository ํธ์ถํ๋ ๋ฉ์๋ ์์ฑ
- I/F๋ implementsํ๋ ํด๋์ค๋ฅผ ๋ถ๋ฆฌํด๋ ๊ด์ฐฎ์
- Optional<T>๋ ์กฐ๊ธ ๋ ๊ณต๋ถํด์ผํ๋ ๋ถ๋ถ
- Create/Update๋ @Transactional ์ถ๊ฐ, JpaRepository๋ฅผ ์ด์ฉํด save
return xxxRepository.save (Xxx.xxxBuilder()
....
.build());
3. Repository์์ queryFactory ์ด์ฉํด ์ฟผ๋ฆฌ ์คํํ๋ ๋ฉ์๋ ์์ฑ
- Search๋ Repository์ ์์ฑ
- Create/Update๋ฅผ ์ํด ์๋ก์ด Entity T์ ๊ฒฝ์ฐ, JpaRepository<T, String> ์ extends ํ๋ IF๋ฅผ ์ถ๊ฐ๋ก ์์ฑ
4. Entity์ Table ํ์์ ๋ง๋ ํด๋์ค ์์ฑ
- @Table(name = '', schema = '')
- @Builder(builderMethodName = '') ๋ Create/Update์ JpaRepository๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํจ
- PK๋ ~~PK๋ผ๋ Entity๋ฅผ ๋ง๋ค์ด @Id๋ฅผ ๋ถ์ฌ ๊ณ ์ ํ ์๋ณ์๋ก ์ฌ์ฉ
'My Story > note of the day' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
note of the day, 21.10.15 (0) | 2021.10.15 |
---|