-
Notifications
You must be signed in to change notification settings - Fork 0
06) helloSpringDataJpa
choi jae ho edited this page Jun 2, 2021
·
13 revisions
- λ€λ₯Έ entity λ₯Ό λ§λ€λ μ μ¬ν μ½λμ λ°λ³΅.
- DAO ν¨ν΄μ΄ κ°λ€λ μ .
- μ½λμ λ°λ³΅
- entity type , primary keyλ§ λ°κΏμ£Όλ©΄ λ€λ₯Έ entity λ λ€λ₯Ό κ²μ΄ μλ€.
- νμ λ°νμλ DAO codeλ₯Ό μ΅μν,
- DAOλ₯Ό μ€μ λ‘ λ§λ€μ΄μ€ νμκ° μλ€
- Spring DATA JPA λ repository interfaces λ₯Ό μ 곡νλ€.
-
- CrudRepository
-
- PagingAndSortingRepository
-
- JpaRepository
- idλ₯Ό κΈ°λ°μΌλ‘ ν΄μ μ‘°ννκΈ° 보λ€λ, κ²μμ ν΅ν΄μ μ‘°νν κ²½μ°κ° μλ€.
- κ·Έ λΆλΆμ λν΄μλ μΆκ°μ μΌλ‘ ꡬνν΄μΌ νλ€.
- Spring Data JPA λ CRUD operationsλ₯Ό μ 곡νλ€.
- method namesμ λ°λΌ λμ μΌλ‘ 쿼리λ₯Ό μ 곡νλ€.
ex)
- User findByEmail(String email), μ΄λ©μΌμ κΈ°μ€μΌλ‘ μ‘°ννλ λ©μλ λ΄λΆμ μΌλ‘ SQL μλμ μΌλ‘ λ§λ€μ΄μ§λ€. μ΄λ¦μ λ°νμΌλ‘ λ©μλλ₯Ό μλμΌλ‘ λ§λ€μ΄μ€λ€λμ .
- User findByEmailAndPassword(String email, String password) λ λ§μ°¬κ°μ§λ‘ λ§λ€μ΄μ€λ€.
- return νμ μ΄ νλ μ΄μμ΄λ©΄, List or Page
- find By + λ³μμ΄λ¦ μΌλ‘ λ£μ΄ μ£Όλ©΄ μλμμ±λλ€.
- ν€μλλ₯Ό μ¬μ©, κ²μν λ μ°Έμ‘°
1) λ€μκ°μ 쑰건 κΈ°μ , κ²μνμμ title νΉμ contentμμ νΉμ λ¨μ΄κ° ν¬ν¨λ κΈ λͺ©λ‘ μ‘°ν
public interface BoardRepository extends JpaRepositoty<Board, Long> {
List<Board> findByTitleContainingOrContentContaining(String title, String content);
}
@SpringBootTest
public class QueryMethodTest {
@Autowired
private BoardRepository repo;
@Test
public void testFindByTitleContainingOrContentContaining() {
List<Board> boardList = repo.findByTitleContainingOrContentContaining(β17β, β17β);
for(Board board: boardList) { System.out.println(βο β + board.toString() )};
}
}
2) Pagination, κ²μνμμ titleλ³μμ βνμ±λβ λΌλ κ²μμ΄κ° ν¬ν¨λ κ²μκΈ λͺ©λ‘ κ²μ νμ΄μ§ λ¨μλ‘ μ‘°ν
public interface BoardRepository extends JpaRepositoty<Board, Long> {
List<Board> findByTitleContaining(String searchKeyword, Pageable paging);
}
@Test
public void testFindByTitleContaining() {
Pageable paging = PageRequest.of(0, 5);
List<Board> boardList= repo.findByTitleContaining(βνμ±λβ, paging);
for(Board board: boardList) {
System.out.println(β->β + board.toString() );
}
}
3) Pagination and Sort, κ²μνμμ titleλ³μμ βνμ±λβμ΄λΌλ κ²μμ΄κ° ν¬ν¨λ κ²μκΈ λͺ©λ‘ κ²μ βseqβ λ³μμ λ°λΌ λ΄λ¦Ό μ°¨μμΌλ‘ μ λ ¬
public interface BoardRepository extends JpaRepositoty<Board, Long> {
List<Board> findByTitleContaining(String searchKeyword, Pageable paging);
}
@Test
public void testFindByTitleContaining( ) {
Pageable paging = PageRequest.of(0,5, Sort.Direction.DESC, βseqβ);
List<Board> boardList= repo.findByTitleContaining(βνμ±λβ, paging);
for(Board board: boardList) {
System.out.println(β->β + board.toString() );
}
5) Return Type: List -> Page , Page κ°μ²΄λ νμ΄μ§ μ²λ¦¬ν λ λ€μν μ 보λ₯Ό μΆκ°λ‘ μ 곡
public interface BoardRepository extends CrudRepositoty<Board, Long> {
Page<Board> findByTitleContaining(String searchKeyword, Pageable paging);
}
@Test
public void testFindByTitleContaining() {
Pageable paging = PageRequest.of(0,5, Sort.Direction.DESC, βseqβ);
Page<Board> pageInfo= repo.findByTitleContaining(βνμ±λβ, paging);
System.out.println(βPage size: β + pageInfo.getSize() );
System.out.println(βTotal Pages: β + pageInfo.getTotalPages() );
System.out.println(βTotal Count: β + pageInfo.getTotalElements() );
List<Board> boardList=pageInfo.getContent();
for(Board board: boardList) {
System.out.println(βο β + board.toString() );
}
}