JPA ์ฒ๋ฆฌ๋ฅผ ๋ด๋นํ๋ Repository๋ ๊ธฐ๋ณธ์ ์ผ๋ก 4๊ฐ์ง๊ฐ ์๋ค.
Repository๋ CRUD ๊ธฐ๋ฅ์ธ์ ์ฒ๋ฆฌ๋ฅผ ํ ๋ ์ฌ์ฉํ๋ฉด ์ข๋ค.
@Indexed
public interface Repository<T, ID> {
}
CrudRepository๋ ๊ธฐ๋ณธ์ ์ธ CRUD ๊ธฐ๋ฅ์ ์ ๊ณตํด์ค๋ค.
public interface CrudRepository<T, ID> extends Repository<T, ID> {
<S extends T> S save(S entity);
Optional<T> findById(ID primaryKey);
Iterable<T> findAll();
long count();
void delete(T entity);
boolean existsById(ID primaryKey);
// โฆ more functionality omitted.
}
PagingAndSortingRepository : CRUD + ํ์ด์ง๊ณผ sorting ๊ธฐ๋ฅ ์ ๊ณต
public interface PagingAndSortingRepository<T, ID> extends CrudRepository<T, ID> {
Iterable<T> findAll(Sort sort);
Page<T> findAll(Pageable pageable);
}
JpaRepository๋ PagingAndSortingRepository + JPA๊ด๋ จ ํนํ ๊ธฐ๋ฅ๋ค(flusing, ๋ฐฐ์น์ฑ ์์
) ๊ธฐ๋ฅ์ ์ ๊ณตํด์ค๋ค.
Query Creation
์ฌ์ฉ์๊ฐ Repository ์ธํฐํ์ด์ค์ ์ ํด์ง ๊ท์น๋๋ก ๋ฉ์๋๋ฅผ ์
๋ ฅํ๋ฉด, Spring์ด ์์์ ํด๋น ๋ฉ์๋ ์ด๋ฆ์ ์ ํฉํ ์ฟผ๋ฆฌ๋ฅผ ๋ ๋ฆฌ๋ ๊ตฌํ์ฒด๋ฅผ ๋ง๋ค์ด Bean์ผ๋ก ๋ฑ๋กํด์ค๋ค. ์ด๋ ์ ํด์ง ๊ท์น์ ์๋ ํ๋ฅผ ์ฐธ์กฐํ๋ฉด ์ข๋ค.
| | |
| findByLastnameAndFirstname
| โฆ where x.lastname = ?1 and x.firstname = ?2
|
| findByLastnameOrFirstname
| โฆ where x.lastname = ?1 or x.firstname = ?2
|
| findByFirstname ,findByFirstnameIs ,findByFirstnameEquals
| โฆ where x.firstname = ?1
|
| | โฆ where x.startDate between ?1 and ?2
|
| | |
| | |
| | |
| findByAgeGreaterThanEqual
| |
| | โฆ where x.startDate > ?1
|
| | โฆ where x.startDate < ?1
|
| | |
| | |
| | โฆ where x.firstname like ?1
|
| | โฆ where x.firstname not like ?1
|
| findByFirstnameStartingWith
| โฆ where x.firstname like ?1 (parameter bound with appended % )
|
| findByFirstnameEndingWith
| โฆ where x.firstname like ?1 (parameter bound with prepended % )
|
| findByFirstnameContaining
| โฆ where x.firstname like ?1 (parameter bound wrapped in % )
|
| findByAgeOrderByLastnameDesc
| โฆ where x.age = ?1 order by x.lastname desc
|
| | โฆ where x.lastname <> ?1
|
| findByAgeIn(Collection ages)
| |
| findByAgeNotIn(Collection ages)
| โฆ where x.age not in ?1
|
| | โฆ where x.active = true
|
| | โฆ where x.active = false
|
| findByFirstnameIgnoreCase
| โฆ where UPPER(x.firstame) = UPPER(?1)
|
Native Queries
@Query
์ด๋
ธํ
์ด์
์ผ๋ก ์๋ ์ฟผ๋ฆฌ๋ฌธ์ ๊ทธ๋๋ก ์ฌ์ฉํ ์ ์๋ค.
public interface UserRepository extends JpaRepository<User, Long> {
@Query(value = "SELECT * FROM USERS WHERE EMAIL_ADDRESS = ?1", nativeQuery = true)
User findByEmailAddress(String emailAddress);
}
public interface UserRepository extends JpaRepository<User, Long> {
@Query(value = "SELECT * FROM USERS WHERE LASTNAME = ?1",
countQuery = "SELECT count(*) FROM USERS WHERE LASTNAME = ?1",
nativeQuery = true)
Page<User> findByLastname(String lastname, Pageable pageable);
}
์์
public interface MemberRepository extends CrudRepository<Member, String>{
}
์ด๋ ๊ฒ ์์๋ง ํ๋ฉด, ๊ธฐ๋ณธ์ ์ธ CrudRepository์ ๊ธฐ๋ฅ์ ๋ค ์ฌ์ฉํ ์ ์๋ค. CRUD์ ์ถ๊ฐ์ ์ผ๋ก ๊ตฌํํ๊ณ ์ถ์ ๊ธฐ๋ฅ์ด ์์ผ๋ฉด ๊ตฌํํ ์ ์๋ค.
์ด๋ ๋ณ๋๋ก @Repository
์ด๋
ธํ
์ด์
์ ์ถ๊ฐํ์ง ์์๋ ๋๋ค.
@Service
public class MemeberServiceImpl implements MemberService {
@Autowired
MemberRepository memberRepository;
public boolean isEnrolledMember(String mbrId) {
// mbrId(ID) ๊ธฐ์ค์ผ๋ก ํ์ ์ฌ๋ถ ํ์ธ(์์ผ๋ฉด true, ์์ผ๋ฉด false)
boolean isEnrolledMember = memberRepository.existsById(mbrId);
return isEnrolledMember;
}
}
์ฐธ์กฐ