UserRepository ์ฝ๋
package com.example.projectpicker.user.repository;
import com.example.projectpicker.user.entity.UserEntity;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository <UserEntity,String>{
UserEntity findByUserEmail(String email);
boolean existsByUserEmail(String email);
}
๋ ํฌ์งํ ๋ฆฌ๋ class๊ฐ ์๋ interface๋ก ํ์ผ์ ์์ฑํด์ผํ๋ค. ์? (์ ๋ชฐ๋ผ์ ๊ตฌ๊ธ๋ง!)
=> Repository๋ฅผ interface๋ก ์ฌ์ฉํ๋ ์ด์ ๋ ๊ฐ๋จํ๊ฒ ๊ฐ๋ฐฉํ์ ์์น(OCP)๋ฅผ ๋ฐ๋ฅด๊ธฐ ๋๋ฌธ์ ๋๋ค. Repository์ ๊ตฌํ์ฒด์์ ๋ณ๊ฒฝ์ด ๋ฐ์ํ๋๋ผ๋ Repository๋ฅผ ์ฌ์ฉํ๋ ๋ถ๋ถ์๋ ์ํฅ์ด ๋ผ์น์ง ์๋๋ค.
<https://www.quora.com/Why-is-a-repository-in-Spring-Boot-an-interface-and-not-a-class>
public interface UserRepository extends JpaRepository <UserEntity,String>{
1. JpaRepository ์ธํฐํ์ด์ค๋ฅผ ์์ํ๊ธฐ๋ง ํ๋ฉด ์ธํฐํ์ด์ค์ ๋ฐ๋ก @Repository๋ฑ์ ์ด๋
ธํ
์ด์
์ ์ถ๊ฐํ ํ์ ์๋ค.
+๊ธฐ๋ณธ์ ์ธ Create, Read, Update, Delete๊ฐ ์๋์ผ๋ก ์์ฑ๋๋ค.
2. JpaRepository๋ฅผ ์์๋ฐ์๋๋ ์ฌ์ฉ๋ Entity ํด๋์ค์ ๊ธฐ๋ณธ ํค ํ์
์ด ๋ค์ด๊ฐ๊ฒ๋๋ค.
<์ฌ๊ธฐ์ ๋๋ UserEntity๋ฅผ ์์๋ฐ์๊ฒ์ด๊ณ , ๊ธฐ๋ณธํค์ธ ID์ ํ์
์ String ์ด๋ค> ==> JpaRepository<T, ID>
UserEntity findByUserEmail(String email);
// ์ด๋ฉ์ผ๋ก ํ์์ ์กฐํ
// ์๋ ์ฝ๋๋ sql๋ฌธ์ผ๋ก ๋ค์๊ณผ ๊ฐ๋ค. (select * from tbl_user where email = ? )
boolean existsByUserEmail(String email);
// ์ด๋ฉ์ผ ์ค๋ณต ๊ฒ์ฌ
// select count(*) from tbl_user where email = ?
'Spring๐ > ๊ฐ๋จํ ๊ฒ์ํ ๋ง๋ค๊ธฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JPA] ๊ฐ๋จํ ๊ฒ์ํ ๋ง๋ค๊ธฐ - user (UserService) (0) | 2023.02.12 |
---|---|
[JPA] ๊ฐ๋จํ ๊ฒ์ํ ๋ง๋ค๊ธฐ - user (LoginRequestDTO, LoginResponseDTO) (0) | 2023.02.11 |
[JPA] ๊ฐ๋จํ ๊ฒ์ํ ๋ง๋ค๊ธฐ - user (UserSignupResponse DTO) (0) | 2023.02.11 |
[JPA] ๊ฐ๋จํ ๊ฒ์ํ ๋ง๋ค๊ธฐ - user (DTO) (1) | 2023.02.11 |
[JPA] ๊ฐ๋จํ ๊ฒ์ํ ๋ง๋ค๊ธฐ - user (UserEntity ์ํฐํฐ) (0) | 2023.02.11 |