자동으로 어노테이션방식으로 하는법
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 컴포넌트 자동 탐생 등록 -->
<context:component-scan base-package="kr.co.jhta.web.*"></context:component-scan>
<!-- mvc 컴포넌트 활성화 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- view resolver -->
<mvc:view-resolvers>
<mvc:jsp prefix="/views/" suffix=".jsp"/>
</mvc:view-resolvers>
<!-- 기본 서블릿 핸들러 -->
<mvc:default-servlet-handler/>
<!-- myBaits : dataSource -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
<property name="username" value="scott"/>
<property name="password" value="tiger"/>
</bean>
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="/WEB-INF/sqlMapConfig.xml"/>
<property name="mapperLocations" value="/WEB-INF/boardMapper.xml"/>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="factory"></constructor-arg>
</bean>
</beans>
<mvc:annotation-driven></mvc:annotation-driven>방식과
<context:annotation-config></context:annotation-config> 방식 2가지가 있는데
mvc방식을 사용하면 된다 내부적으로 context방식을 포함하고 있다
수동으로 구현하는 방법
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 스프링 컨테이너에게 위임할 빈을 등록 : app.xml -->
<!-- Handler Mapping -->
<bean id="beanNameUrlHandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!-- 등록할 컨트롤러 -->
<bean id="/hello.do" class="spring_web01.HelloController"></bean>
<bean id="/welcom.do" class="spring_web01.WelcomController"></bean>
<!-- view Resolver -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
컨트롤러를 구현하는 방식으로 처리
package spring_web01;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
// Controller : 비지니스 로직이 담겨있는 클래스
// Controller 인터페이스를 구현한 클래스
public class HelloController implements Controller{
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
//전달할 데이터 : req.setAttribute()
//전달할 데이터 : Model
//찾아갈 뷰 객체 : View
ModelAndView mav = new ModelAndView();
//req.setAttribute("data","안녕하세요");
mav.addObject("data","안녕하세요");
mav.setViewName("hello");
return mav;
}
}
프로퍼티를 따로 두고 커넥션풀도 설정할때 방식
<!-- db 설정파일을 읽는 객체 -->
<bean id="property" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/db.properties"/>
</bean>
<!-- dbcp : DataBase Connection Pooling -->
<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp2.BasicDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean>
<!-- 공장 객체 -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="/WEB-INF/sqlMapConfig.xml"></property>
</bean>
이름 : db.properties
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:orcl
username=scott
password=tiger
이름 : sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias type="kr.co.jhta.web.dto.DeptDTO" alias="ddto"/>
</typeAliases>
<environments default="dev">
<environment id="dev">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${drver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="kr/co/jhta/web/mapper/dept.xml"/>
</mappers>
</configuration>
'SPRING' 카테고리의 다른 글
UTF-8 필터 xml 처리 (0) | 2023.07.05 |
---|---|
시큐리티 간단 설정 (0) | 2023.07.05 |
DI의 종류 (0) | 2023.07.05 |
aop적용방식 (0) | 2023.07.05 |
단위테스트 (0) | 2023.07.05 |