JpaRepository ์์ฑํ๊ธฐ
Repository๋?
entity์ ์ํด ์์ฑ๋ dbํ ์ด๋ธ์ ์ ๊ทผํ๋ ํจ์(ex: findAll, save ๋ฑ)๋ค์ ์ฌ์ฉํ๊ธฐ ์ํ ์ธํฐํ์ด์ค์ด๋ค. CRUD๋ฅผ ์ด๋ป๊ฒ ์ฒ๋ฆฌํ ์ง ์ ์ํ๋ ๊ณ์ธต์ด๋ค.
PlaceRepository
์ธํฐํ์ด์ค๋ฅผ ์์ฑํ๋ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ๋ค.
package com.goingto.back;
import org.springframework.data.jpa.repository.JpaRepository;
public interface PlaceRepository extends JpaRepository<Place, Integer>{
}
๋ฆฌํฌ์งํฐ๋ฆฌ๋ก ๋ง๋ค๊ธฐ ์ํด JpaRepository
์ธํฐํ์ด์ค๋ฅผ ์์ํ๋ค.
์์ํ ๋๋ ์ ๋ค๋ฆญ์ค ํ์ ์ผ๋ก ์ํฐํฐ์ ํ์ ๊ณผ ํด๋น ์ํฐํฐ์ PK ์์ฑ ํ์ ์ ์ง์ ํด์ผ ํ๋ค.
Place
์ํฐํฐ๋ Integer(id ๊ฐ) ์์ฑ์ PK ํ์
์ ๊ฐ์ก๊ธฐ ๋๋ฌธ์ <Place, Integer>
๋ผ๊ณ ์์ฑํ๋ค.
์์ฑํ Repository๋ฅผ ํ ์คํธํ๊ธฐ ์ํด JUnit ๊ธฐ๋ฐ์ ํ ์คํธ ํ๋ ์์ํฌ๋ฅผ ์ฌ์ฉํ๋ค.
src/test/java/com/goingto/back/BackApplicationsTests.java
ํ์ผ์ ํ
์คํธํ๊ณ ์ถ์ ์ฝ๋๋ฅผ ์์ฑํด์ค๋ค.
package com.goingto.back;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.beans.factory.annotation.Autowired;;
@SpringBootTest
class BackApplicationTests {
@Autowired
private PlaceRepository placeRepository;
@Test
void testJpa() {
Place p = new Place();
p.setName("์์ด์ฌ๋๋");
p.setSeason("๋ด");
p.setBudget(500);
p.setIsDomestic(false);
p.setPrefer("ํด์");
this.placeRepository.save(p);
}
}
์ ์ฝ๋๋ Place
๊ฐ์ฒด๋ฅผ ์์ฑํ์ฌ Setter
๋ก ์์ฑ์ ์ง์ ํ ํ ์ ์ฅํ๋ค.
@Autowired
๋ ์คํ๋ง์ DI(Dependency Injection) ๊ธฐ๋ฅ์ผ๋ก placeRepository
๊ฐ์ฒด๋ฅผ ์๋์ผ๋ก ์์ฑํด์ค๋ค.
์ค์ ์ฝ๋๋ฅผ ์์ฑํ ๋์๋ ์ํ์ฐธ์กฐ์ ๊ฐ์ ๋ฌธ์ ๋ก ์์ฑ์
ํน์ Setter
๋ฅผ ํตํ ๊ฐ์ฒด ์์ฑ์ด ๊ถ์ฅ๋๋ค. ํ์ง๋ง ํ
์คํธ ์ฝ๋์ ๊ฒฝ์ฐ์๋ ์์ฑ์๋ฅผ ํตํ ๊ฐ์ฒด ์ฃผ์
์ด ๋ถ๊ฐ๋ฅํ๊ธฐ ๋๋ฌธ์ @Autowired
๋ฅผ ์ฌ์ฉํ๋ค.
๋ฐ์ดํฐ ์กฐํํ๊ธฐ
findAll() : ๋ชจ๋ ๋ฐ์ดํฐ ์กฐํ
package com.goingto.back;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.beans.factory.annotation.Autowired;;
@SpringBootTest
class BackApplicationTests {
@Autowired
private PlaceRepository placeRepository;
@Test
void testJpa() {
List<Place> all = this.placeRepository.findAll();
assertEquals(3, all.size());//db์ฌ์ด์ฆ๊ฐ 3์ธ์ง
}
}
assertEquals(๊ธฐ๋๊ฐ, ์ค์ ๊ฐ)
์ ๊ธฐ๋๊ฐ๊ณผ ์ค์ ๊ฐ์ด ๋์ผํ์ง ์กฐ์ฌํ๋ค. ๋ง์ฝ ๋์ผํ์ง ์๋ค๋ฉด test๋ ์คํจ๋ก ์ฒ๋ฆฌ๋๋ค.
findById() : Id ๊ฐ์ผ๋ก ๋ฐ์ดํฐ ์กฐํ
ํจ์์ ๋ฆฌํด ํ์
์ด Place
๊ฐ ์๋ Optional
์ด๋ผ๋ ์ ์ ์ฃผ์ํด์ผํ๋ค.
Optional
์ null ์ฒ๋ฆฌ๋ฅผ ์ ์ฐํ๊ฒ ํ๊ธฐ ์ํด ์ฌ์ฉํ๋ ํด๋์ค๋ก, isPresent()
๋ก null์ด ์๋์ง ํ์ธํ ํ์ get()
์ผ๋ก ์ค์ Place
๊ฐ์ฒด๋ฅผ ์ป์ด์ผ ํ๋ค.
package com.goingto.back;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.beans.factory.annotation.Autowired;;
@SpringBootTest
class BackApplicationTests {
@Autowired
private PlaceRepository placeRepository;
@Test
void testJpa() {
Optional<Place> op = this.placeRepository.findById(2);
if(op.isPresent()){
Place p = op.get();
assertEquals("๋ฒ ํธ๋จ", p.getName());
}
}
}
findByName(), findBySeason(), findByBudget()...
์์ ํจ์๋ค์ ์ํฐํฐ์ ํน์ ๋ ์กฐํ ๊ธฐ๋ฅ์ด๋ค. ๋ฐ๋ผ์ PlaceRepository
์ธํฐํ์ด์ค์ ์ง์ ์์ฑํด์ค์ผ ์ฌ์ฉํ ์ ์๋ค.
๋ค์๊ณผ ๊ฐ์ด PlaceRepository.java
ํ์ผ์ ์์ฑํ๋ค. ๋ฐํ ๊ฐ์ด ์ฌ๋ฌ ๊ฐ์ธ ๊ฒฝ์ฐ์๋ ํจ์์ ๋ฆฌํด ํ์
์ List๋ก ํด์ผ ํ๋ค.
package com.goingto.back;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
public interface PlaceRepository extends JpaRepository<Place, Integer>{
List<Place> findByBudget(Integer budget);
Place findByImg(String img);
List<Place> findByIsDomestic(Boolean isDomestic);
Place findByName(String name);
List<Place> findByPrefer(String prefer);
List<Place> findByScenery(String scenery);
List<Place> findBySeason(String season);
}
์ด์ ์ธํฐํ์ด์ค์ ์์ฑํ ํจ์๋ฅผ ํตํด ์ํ๋ ํน์ ํ ์ด๋ธ ๋ฐ์ดํฐ๋ฅผ ์กฐํํ ์ ์๋ค!
๋๋ season
์์ฑ์ด "๋ด"
์ธ ๋ฐ์ดํฐ๋ฅผ ์กฐํํ๊ธฐ ์ํด ๋ค์๊ณผ ๊ฐ์ด ํ
์คํธ ์ฝ๋๋ฅผ ์์ฑํ๊ณ ํ
์คํธ๋ ์์ฃผ ์ ํต๊ณผ๋๋ค.๐
package com.goingto.back;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.beans.factory.annotation.Autowired;;
@SpringBootTest
class BackApplicationTests {
@Autowired
private PlaceRepository placeRepository;
@Test
void testJpa() {
List<Place> p= this.placeRepository.findBySeason("๋ด");
for (int i=0;i<p.size();i++){
System.out.println(p.get(i).getName());
}
}
}
๋ง์ฝ ๋ ๊ฐ์ง ์์ฑ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์กฐํํ๊ณ ์ถ๋ค๋ฉด findBy<์์ฑ1>And<์์ฑ2>
ํจ์๋ฅผ Repository์ ์ถ๊ฐํ๋ฉด ๋๋ค.
์ด์ ๊ฐ์ด JpaRepository
์ ํจ์๋ช
์ ๊ท์น์ ๋ง๊ฒ ์์ฑํ์ ๋ SQL ๊ตฌ๋ฌธ์ where ์กฐ๊ฑด์ ๊ฒฐ์ ์ง๋ ์ญํ ์ ํด์ค๋ค.
And ์ธ์๋ Or, Between ๋ฑ์ผ๋ก ์ฝ๊ฒ ์ํ๋ ๋ฐ์ดํฐ๋ฅผ ์กฐํํ ์ ์๋ค.
ํจ์ ๋ช ์ ์์ฑ ๊ท์น์ ๋ํ ์์ธํ ๋ด์ฉ์ ๊ณต์๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ๋ฉด ์ข์ ๊ฒ ๊ฐ๋ค.
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.query-creation
Spring Data JPA - Reference Documentation
Example 109. Using @Transactional at query methods @Transactional(readOnly = true) interface UserRepository extends JpaRepository { List findByLastname(String lastname); @Modifying @Transactional @Query("delete from User u where u.active = false") void del
docs.spring.io
'๐ป Web' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[React] redux ์ ๋ฆฌ (0) | 2022.08.11 |
---|---|
[SpringBoot] H2 ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฌ์ฉ ๋ฐฉ๋ฒ (+์ค๋ฅ ํด๊ฒฐ) (0) | 2022.07.28 |
[React] Context API, useContext Hook ์ฌ์ฉํ๊ธฐ (2) | 2022.07.08 |