Home
>
Spring
>
π[Spring] SimpleJpaRepositoryλ 무μμΌκΉμ?
Spring
Framework
π[Spring] SimpleJpaRepositoryλ 무μμΌκΉμ?
SimpleJpaRepositoryλ Spring Data JPA(Java Persistence API)μμ μ 곡νλ κΈ°λ³Έ Repository ꡬν ν΄λμ€ μ
λλ€.
μ΄ ν΄λμ€λ Spring Data JPAμ Repository μΈν°νμ΄μ€(CrudRepository, JpaRepository λ±)λ₯Ό ꡬννλ©°, μΌλ°μ μΌλ‘ μ¬μ©λλ CRUD(μμ±(Create), μ‘°ν(Read), μμ (Update), μμ (Delete)) κΈ°λ₯μ ν¬ν¨νκ³ μμ΅λλ€.
1οΈβ£ μν .
SimpleJpaRepositoryλ κ°λ°μκ° μ μν Repository μΈν°νμ΄μ€μ ꡬν체λ‘, λ°μ΄ν°λ² μ΄μ€μμ μνΈμμ©μ κ°νΈνκ² μ²λ¦¬ν μ μλλ‘ λ€μν λ©μλλ₯Ό μ 곡ν©λλ€.
κ°λ°μλ μ§μ CRUD λ‘μ§μ ꡬννμ§ μκ³ λ, SimpleJpaRepositoryλ₯Ό ν΅ν΄ μλμΌλ‘ μ 곡λλ κΈ°λ³Έ CRUD λ©μλλ₯Ό νμ©ν μ μμ΅λλ€.
2οΈβ£ SimpleJpaRepositoryμ λμ λ°©μ.
1οΈβ£ κΈ°λ³Έ CRUD λ©μλ μ 곡.
SimpleJpaRepositoryλ save, findById, findAll, delete λ±κ³Ό κ°μ κΈ°λ³Έμ μΈ CRUD(μμ±(Create), μ‘°ν(Read), μμ (Update), μμ (Delete)) λ©μλλ₯Ό ꡬνν©λλ€.
κ°λ°μκ° JpaRepository μΈν°νμ΄μ€λ₯Ό νμ₯νμ¬ μ»€μ€ν
Repository μΈν°νμ΄μ€λ₯Ό μ μνλ©΄, Spring Data JPAκ° λ°νμ μμ μ SimpleJpaRepositoryλ₯Ό μ¬μ©ν΄ ν΄λΉ μΈν°νμ΄μ€μ ꡬν체λ₯Ό μλμΌλ‘ μμ±ν©λλ€.
2οΈβ£ λ°μ΄ν°λ² μ΄μ€μμ μλ μνΈμμ©.
SimpleJpaRepositoryλ Hibernate μ κ°μ JPA(Java Persistence API) ꡬν체μ μνΈμμ©νμ¬, λ°μ΄ν°λ₯Ό λ°μ΄ν°λ² μ΄μ€μ μ μ₯νκ±°λ μ‘°νν λ νμν SQL(Structured Query Language) 쿼리λ₯Ό μλμΌλ‘ μμ±νκ³ μ€νν©λλ€.
κ°λ°μλ λ°μ΄ν°λ² μ΄μ€μμ μ§μ μ μΈ μνΈμμ©μ ꡬνν νμκ° μλ―λ©°, Spring Data JPAκ° μ΄ λͺ¨λ κ²μ μλμΌλ‘ μ²λ¦¬ν©λλ€.
3οΈβ£ 쿼리 λ©μλ μ§μ
SimpleJpaRepositoryλ λ©μλ μ΄λ¦μ λΆμνμ¬ μΏΌλ¦¬ λ©μλ λ₯Ό μλμΌλ‘ μμ±νλ κΈ°λ₯μ μ§μν©λλ€.
μλ₯Ό λ€μ΄, findByNameκ³Ό κ°μ λ©μλλ₯Ό μ μνλ©΄, Spring Data JPAλ μλμΌλ‘ name νλλ₯Ό κΈ°μ€μΌλ‘ λ°μ΄ν°λ₯Ό μ‘°ννλ 쿼리λ₯Ό μμ±ν©λλ€.
3οΈβ£ SimpleJpaRepositoryμ κΈ°λ³Έμ μΈ CRUD λ©μλ.
SimpleJpaRepositoryλ λ€μκ³Ό κ°μ κΈ°λ³Έ λ©μλλ₯Ό μ 곡ν©λλ€.
save(S entity)
μν°ν°λ₯Ό λ°μ΄ν°λ² μ΄μ€μ μ μ₯ν©λλ€. μν°ν°κ° μ΄λ―Έ μ‘΄μ¬νλ©΄ μ
λ°μ΄νΈνκ³ , μ‘΄μ¬νμ§ μμΌλ©΄ μλ‘ μ½μ
ν©λλ€.
findById(ID id)
μ§μ λ IDλ‘ λ°μ΄ν°λ² μ΄μ€μμ μν°ν°λ₯Ό μ‘°νν©λλ€.
findAll()
λ°μ΄ν°λ² μ΄μ€μ μ μ₯λ λͺ¨λ μν°ν°λ₯Ό μ‘°νν©λλ€.
delete(T entity)
λ°μ΄ν°λ² μ΄μ€μμ μν°ν°λ₯Ό μμ ν©λλ€.
deleteById(ID id)
μ§μ λ IDλ‘ λ°μ΄ν°λ² μ΄μ€μμ μν°ν°λ₯Ό μμ ν©λλ€.
count()
λ°μ΄ν°λ² μ΄μ€μ μ μ₯λ μν°ν°μ μ΄ κ°μλ₯Ό λ°νν©λλ€.
4οΈβ£ μ¬μ© μμ.
π UserRepository μΈν°νμ΄μ€.
import org.springframework.data.jpa.repository.JpaRepository ;
public interface UseRepository extends JpaRepository < User , Long > {
User findByName ( String name );
}
μ μμμμ UserRepositoryλ JpaRepositoryλ₯Ό μμνκ³ μμΌλ©°, μ΄λ₯Ό ν΅ν΄ SimpleJpaRepositoryκ° UserRepositoryμ ꡬνμ²΄λ‘ λμνκ² λ©λλ€.
findByName, save, findById λ±μ λ©μλλ₯Ό λ³λλ‘ κ΅¬ννμ§ μμλ, Spring Data JPAκ° SimpleJpaRepositoryλ₯Ό μ¬μ©νμ¬ μ΄λ€μ μλμΌλ‘ μ 곡ν΄μ€λλ€.
π μλΉμ€ ν΄λμ€μμ μ¬μ©.
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.stereotype.Service ;
import java.util.Optional ;
@Sevice
public class UserService {
private final UserRepository userRepository ;
@Autowired
public UserService ( UserRepository userRepository ) {
this . userRepository = userRepository ;
}
public User createUser ( String name , Integer age ) {
User user = new User ( name , age );
return userRepository . save ( user ); // SimpleJpaRepositoryμ save λ©μλ μ¬μ©
}
public Optional < User > getUserById ( Long id ) {
return userRepository . findById ( id ); // SimpleJpaRepositoryμ findById λ©μλ μ¬μ©
}
public void deleteUser ( Long id ) {
userRepository . deleteById ( id ); // SimpleJpaRepositoryμ deleteById λ©μλ μ¬μ©
}
}
5οΈβ£ SimpleJpaRepositoryμ νμ₯ λ° μ»€μ€ν°λ§μ΄μ§.
1οΈβ£ κΈ°λ³Έ κΈ°λ₯ νμ₯.
κ°λ°μλ SimpleJpaRepositoryκ° μ 곡νλ κΈ°λ³Έ κΈ°λ₯ μΈμ μΆκ°μ μΈ μ»€μ€ν
κΈ°λ₯μ Repositoryμ ꡬνν μ μμ΅λλ€.
μλ₯Ό λ€μ΄, 볡μ‘ν 쿼리λ₯Ό μ§μ μ μνκ±°λ, μΆκ°μ μΈ λΉμ¦λμ€ λ‘μ§μ ꡬνν μ μμ΅λλ€.
2οΈβ£ @Query μ΄λ
Έν
μ΄μ
μ¬μ©.
@Query μ΄λ
Έν
μ΄μ
μ μ¬μ©νλ©΄ 볡μ‘ν JPQL(Java Persistence Query Language) λλ λ€μ΄ν°λΈ SQL 쿼리λ₯Ό μ§μ μμ±νμ¬ λ©μλμ μ°κ²°ν μ μμ΅λλ€.
import org.springframework.data.jpa.repository.Query ;
import org.springframework.data.repository.query.Param ;
public interface UserRepository extends JpaRepository < User , Long > {
@Query ( "SELECT u FROM User u WHERE u.name = :name AND u.age > :age" )
List < User > findByNameAndAgeGreaterThan ( @Param ( "name" ) String name , @Param ( "age" ) Integer age );
}
6οΈβ£ μμ½.
SimpleJpaRepositoryλ Spring Data JPA μμ κΈ°λ³Έμ μΌλ‘ μ 곡νλ CRUD κΈ°λ₯μ ꡬν체λ‘, JpaRepository μΈν°νμ΄μ€λ₯Ό ν΅ν΄ μλμΌλ‘ μμ±λ©λλ€.
κ°λ°μλ JpaRepositoryλ₯Ό μμλ°λ Repository μΈν°νμ΄μ€λ₯Ό μ μν¨μΌλ‘μ¨ SimpleJpaRepositoryκ° μ 곡νλ μλ CRUD κΈ°λ₯ μ νμ©ν μ μμΌλ©°, λ©μλ μ΄λ¦ κΈ°λ° μΏΌλ¦¬, 컀μ€ν
쿼리 λ±μ ν΅ν΄ λ°μ΄ν°λ² μ΄μ€μμ μνΈμμ©μ κ°νΈνκ² μ²λ¦¬ν μ μμ΅λλ€.
Spring Data JPAλ λ°μ΄ν°λ² μ΄μ€μμ μνΈμμ©μ λ¨μν νκ³ , μ½λμ κ°λ
μ±κ³Ό μ μ§λ³΄μμ± μ λμ΄λ λ° ν° μν μ ν©λλ€.
SimpleJpaRepositoryλ μ΄λ¬ν Spring Data JPAμ ν΅μ¬μ λ΄λΉνλ κΈ°λ³Έ ꡬν체μ
λλ€.