๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ–ฅ๏ธ ์›น(Web)/์Šคํ”„๋ง

์Šคํ”„๋ง ๋ ˆ์ด์–ด ๊ตฌ์กฐ

by bekki 2025. 4. 18.

๐Ÿงฑ 1. Controller (์ปจํŠธ๋กค๋Ÿฌ)

๐Ÿ“Œ ์—ญํ• 

  • ์‚ฌ์šฉ์ž์˜ ์š”์ฒญ์„ ๋ฐ›์•„์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ์ž…๊ตฌ ์—ญํ• 
  • ์ฃผ๋กœ HTTP ์š”์ฒญ(GET, POST, PUT, DELETE)์„ ๋ฐ›๊ณ ,
  • ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•  Service๋ฅผ ํ˜ธ์ถœํ•˜๊ณ ,
  • ๊ฒฐ๊ณผ๋ฅผ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์‘๋‹ต์œผ๋กœ ๋ฐ˜ํ™˜ํ•จ.

๐ŸŽฏ ์‚ฌ์šฉ ์˜ˆ

@RestController
@RequestMapping("/users")
public class UserController {

    private final UserService userService;
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/{id}")
    public UserDto getUser(@PathVariable Long id) {
        return userService.getUserById(id);
    }
}

 

๐Ÿง  2. Service (์„œ๋น„์Šค)

๐Ÿ“Œ ์—ญํ• 

  • ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์ด ๋“ค์–ด์žˆ๋Š” ์ค‘๊ฐ„ ๊ด€๋ฆฌ์ž
  • ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ์ˆœ์„œ, ๊ณ„์‚ฐ, ๊ฒ€์ฆ ๋“ฑ ์ฃผ์š” ๋กœ์ง ์ˆ˜ํ–‰
  • ์ปจํŠธ๋กค๋Ÿฌ๋Š” ์„œ๋น„์Šคํ•œํ…Œ "์ด๊ฑฐ ํ•ด์ค˜"๋ผ๊ณ  ์š”์ฒญํ•จ
  • ์„œ๋น„์Šค๋Š” DB์™€ ์ง์ ‘ ์ ‘์ด‰ํ•˜์ง€ ์•Š๊ณ  Repository๋ฅผ ํ˜ธ์ถœํ•จ

๐ŸŽฏ ์‚ฌ์šฉ ์˜ˆ

@Service
public class UserService {

    private final UserRepository userRepository;
    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public UserDto getUserById(Long id) {
        User user = userRepository.findById(id)
            .orElseThrow(() -> new RuntimeException("User not found"));
        return new UserDto(user.getId(), user.getName());
    }
}

 

๐Ÿ—ƒ๏ธ 3. Repository (๋ ˆํฌ์ง€ํ† ๋ฆฌ)

๐Ÿ“Œ ์—ญํ• 

  • DB์— ์ง์ ‘ ์ ‘๊ทผํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ ์ €์žฅํ•˜๋Š” ๊ณ„์ธต
  • ๋ณดํ†ต JPA, MyBatis, JDBC ๋“ฑ์„ ์‚ฌ์šฉํ•ด์„œ ๊ตฌํ˜„
  • ์ธํ„ฐํŽ˜์ด์Šค๋งŒ ๋งŒ๋“ค์–ด๋„ JPA๊ฐ€ ์•Œ์•„์„œ ๊ตฌํ˜„ํ•ด์คŒ 

๐ŸŽฏ ์‚ฌ์šฉ ์˜ˆ

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
    // ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์ปค์Šคํ…€ ์ฟผ๋ฆฌ๋„ ์ถ”๊ฐ€ ๊ฐ€๋Šฅ
    Optional<User> findByEmail(String email);
}

 

๐Ÿงฌ 4. Domain (๋„๋ฉ”์ธ)

๐Ÿ“Œ ์—ญํ• 

  • **๋น„์ฆˆ๋‹ˆ์Šค ๋Œ€์ƒ์ด ๋˜๋Š” ์‹ค์ œ ๊ฐ์ฒด(์—”ํ‹ฐํ‹ฐ)**๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ํด๋ž˜์Šค
  • ์˜ˆ: User, Product, Order ๊ฐ™์€ ์‹ค์ œ ๋น„์ฆˆ๋‹ˆ์Šค ๊ฐ์ฒด๋“ค
  • DB ํ…Œ์ด๋ธ”๊ณผ ๋งคํ•‘๋˜๋ฉฐ, JPA์—์„œ @Entity๋กœ ํ‘œ์‹œํ•จ

๐ŸŽฏ ์‚ฌ์šฉ ์˜ˆ

@Entity
public class User {

    @Id @GeneratedValue
    private Long id;

    private String name;
    private String email;

    // getter, setter, ์ƒ์„ฑ์ž ๋“ฑ ์ƒ๋žต
}

 

 

๐Ÿ”„ ์ „์ฒด ํ๋ฆ„ ์š”์•ฝ

[Client] → ์š”์ฒญ
   ↓
[Controller] → ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ ๋ฐ›๊ณ , Service ํ˜ธ์ถœ
   ↓
[Service] → ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ์ˆ˜ํ–‰, Repository ํ˜ธ์ถœ
   ↓
[Repository] → DB์—์„œ ๋ฐ์ดํ„ฐ ์กฐํšŒ or ์ €์žฅ
   ↓
[Domain] → ์‹ค์ œ ๋ฐ์ดํ„ฐ ๊ฐ์ฒด(Entity)
   ↓
๊ฒฐ๊ณผ๋ฅผ ๋‹ค์‹œ ์œ„๋กœ ๋ฐ˜ํ™˜ → ์ตœ์ข… ์‘๋‹ต

 

๐Ÿ’ก ์‹ค์ œ ์˜ˆ: ์œ ์ € ์กฐํšŒ API

[๋ธŒ๋ผ์šฐ์ € ์š”์ฒญ] GET /users/1
     ↓
[UserController] → userService.getUserById(1)
     ↓
[UserService] → userRepository.findById(1)
     ↓
[UserRepository] → DB์—์„œ User ์—”ํ‹ฐํ‹ฐ ์กฐํšŒ
     ↓
[User] → ์—”ํ‹ฐํ‹ฐ → DTO๋กœ ๋ณ€ํ™˜ → ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์‘๋‹ต

 

๐Ÿ“Ž ์ •๋ฆฌํ‘œ

๊ณ„์ธต ์—ญํ•  ์„ค๋ช… ์–ด๋…ธํ…Œ์ด์…˜ ์ฃผ์š” ํ‚ค์›Œ๋“œ

Controller ์š”์ฒญ ์ฒ˜๋ฆฌ & ์‘๋‹ต ๋ฐ˜ํ™˜ @Controller, @RestController HTTP
Service ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง @Service ๋กœ์ง
Repository ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ ‘๊ทผ @Repository or JpaRepository DB
Domain DB ํ…Œ์ด๋ธ”๊ณผ ๋งคํ•‘๋˜๋Š” ๋ชจ๋ธ ํด๋ž˜์Šค @Entity ๊ฐ์ฒด