위와 같이 따로 분리해놓은 이유는 HTTP Request를 Web Application이 받게 되면 Thread를 생성하게 되는데 비지니스 로직이 DB로부터 데이터를 얻어오기 위해 매번 Driver를 로드하고 Connection 객체를 생성하게 되면 너무 많은 커넥션이 일어나게 되기 때문에 DAO를 하나 만들어 DB 전용 객체로만 사용하는 것이다.
: 사용자의 요청(request)을 처리
@Autowired : 자동 생성. 해당 변수 및 메서드에 스프링이 관리하는 Bean을 자동으로 매핑. singleton
@Controller
public class HelloController {
// Service 접근(생성)
@Autowired
HelloService service;
}
: 비즈니스 로직을 처리
📌 interface로 분리하는 이유?
구현체에 대한 권한을 구현클래스에게 넘겨서 클래스 사이에 의존성을 줄이기 위함이다
public interface HelloService {
...
}
@Service
public class HelloServiceImpl implements HelloService {
// Dao 접근(생성)
@Autowired
HelloDao dao;
}
: DB의 데이터에 접근하기 위한 객체
public interface HelloDao {
...
}
@Repository // == 저장소
public class HelloDaoImpl implements HelloDao {
// MyBatis 접근(생성)
@Autowired
SqlSession session;
}
더 자세하게 알고 싶다면 아래의 블로그 참조
https://linked2ev.github.io/spring/2019/08/14/Spring-2-%EC%8A%A4%ED%94%84%EB%A7%81-MVC-%ED%8C%A8%ED%84%B4/