Spring Data JPA

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์ผ๋ก ๋ฑ๋กํด์ค๋ค. ์ด๋ ์ ํด์ง ๊ท์น์ ์๋ ํ๋ฅผ ์ฐธ์กฐํ๋ฉด ์ข๋ค.
Keyword
Sample
JPQL snippet
And
findByLastnameAndFirstname
โฆ where x.lastname = ?1 and x.firstname = ?2
Or
findByLastnameOrFirstname
โฆ where x.lastname = ?1 or x.firstname = ?2
Is
, Equals
findByFirstname
,findByFirstnameIs
,findByFirstnameEquals
โฆ where x.firstname = ?1
Between
findByStartDateBetween
โฆ where x.startDate between ?1 and ?2
LessThan
findByAgeLessThan
โฆ where x.age < ?1
LessThanEqual
findByAgeLessThanEqual
โฆ where x.age <= ?1
GreaterThan
findByAgeGreaterThan
โฆ where x.age > ?1
GreaterThanEqual
findByAgeGreaterThanEqual
โฆ where x.age >= ?1
After
findByStartDateAfter
โฆ where x.startDate > ?1
Before
findByStartDateBefore
โฆ where x.startDate < ?1
IsNull
, Null
findByAge(Is)Null
โฆ where x.age is null
IsNotNull
, NotNull
findByAge(Is)NotNull
โฆ where x.age not null
Like
findByFirstnameLike
โฆ where x.firstname like ?1
NotLike
findByFirstnameNotLike
โฆ where x.firstname not like ?1
StartingWith
findByFirstnameStartingWith
โฆ where x.firstname like ?1
(parameter bound with appended %
)
EndingWith
findByFirstnameEndingWith
โฆ where x.firstname like ?1
(parameter bound with prepended %
)
Containing
findByFirstnameContaining
โฆ where x.firstname like ?1
(parameter bound wrapped in %
)
OrderBy
findByAgeOrderByLastnameDesc
โฆ where x.age = ?1 order by x.lastname desc
Not
findByLastnameNot
โฆ where x.lastname <> ?1
In
findByAgeIn(Collection ages)
โฆ where x.age in ?1
NotIn
findByAgeNotIn(Collection ages)
โฆ where x.age not in ?1
True
findByActiveTrue()
โฆ where x.active = true
False
findByActiveFalse()
โฆ where x.active = false
IgnoreCase
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;
}
}
์ฐธ์กฐ
Last updated
Was this helpful?