728x90
초기세팅하면서 어떻게 스프링 프레임워크를 사용해야되는지 연습해보았다
root-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<!-- Basic DataSource 등록 -->
<!-- MVN Repository에서 commons-dbcp 검색해서 메이븐에 추가해야 사용가능 -->
<!-- 이 DataSource를 SqlSession bean, DataSourceTransactionManager가 ref해서 사용하는 구조 -->
<bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource" destroy-method="close">
<!-- destroy-method : 닫아주는 메소드로 close를 사용하겠다 -->
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
<property name="username" value="Spring"/>
<property name="password" value="Spring"/>
</bean>
<!-- SqlSession 빈(bean) 등록 -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- mybatis라이브러리랑 mybatis Spring라이브러리를 미리 받아서 에러가 안드는 것 -->
<property name="dataSource" ref="dataSource"/> <!-- 참고할거임. 데이터 소스를 참고할 것. id로 썼던 데이터소스를 의미 -->
<!-- 마이바티스 컨피그에서 설정 정보를 설정했었음 -->
<!-- 마이바티스 컨피그.xml파일을 읽어와야함 -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- classpath는 src/main/resource폴더를 의미함 --><!-- mybatis-config.xml 안만들었으니 만들러 ㄱㄱ -->
</bean>
<!-- Template 생성 -->
<!-- SqlSessionFactoryBean를 ref함 -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSession"/> <!-- 매개변수 있는 생성자. 위의 1번 방법에 있는 그것-->
</bean>
<!-- transactionManager : 트랜잭션 집어넣을 수 있게 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 파일업로드 : multipartResolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="100000000"/> <!-- value : 용량 정하기 -->
<property name="maxInMemorySize" value="100000000"/>
</bean>
</beans>
servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="prac.prac.practice" />
</beans:beans>
컨트롤러
package prac.prac.practice.board.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import prac.prac.practice.board.model.service.BoardService;
@Controller
public class BoardController {
@Autowired
private BoardService boardService;
}
서비스/서비스Impl
package prac.prac.practice.board.model.service;
public interface BoardService {
}
///////////////////////////////////////////////
package prac.prac.practice.board.model.service;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import prac.prac.practice.board.model.dao.BoardDAO;
@Service("boardService")
public class BoardServiceImpl implements BoardService {
@Autowired
private BoardDAO boardDAO;
@Autowired
private SqlSessionTemplate SqlSession;
}
DAO
package prac.prac.practice.board.model.dao;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
@Repository("boardDAO")
public class BoardDAO {
}
728x90
반응형
'small steps > 1일 1코딩 - 코딩을 내 몸처럼' 카테고리의 다른 글
[1일 1코딩] (0) | 2022.06.10 |
---|---|
[1일 1코딩][Spring] 사용자정의 예외처리(Exception) & Checked/Unchecked Exception (0) | 2022.06.09 |
[1일 1코딩][Web] EL & JSTL : <c:url>,<c:param> 뷰페이지 코드분석 (0) | 2022.06.07 |
[1일 1코딩][MyBatis] config.xml 설정파일 (0) | 2022.06.06 |
[1일 1코딩][Web-Java] Filter 클래스 & 사용자정의 Exception 클래스 (0) | 2022.06.05 |