Spring프레임워크에서 MyBatis로 작성하기
1. xml 설정하기
위치)webapp/WEB-INF/web.xml[ 전 ]
1234567891011 <servlet> <servlet-name>shop-3</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>shop-3</servlet-name> <url-pattern>*.shop</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> cs
Spring프레임워크에서 MyBatis로 작성하기
1. xml 설정하기
위치)webapp/WEB-INF/web.xml
[ 전 ]
1 2 3 4 5 6 7 8 9 10 11 | <servlet> <servlet-name>shop-3</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>shop-3</servlet-name> <url-pattern>*.shop</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> | cs |
[ 후 ]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <servlet> <servlet-name>shop-4</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:spring-mvc.xml classpath:spring-db.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>shop-4</servlet-name> <url-pattern>*.shop</url-pattern> </servlet-mapping> | cs |
기존의 servlet을 설정하는 web.xml에서 classpath
spring-mvc.xml 과 spring-db.xml을 설정해줍니다.
그리고 listener를 삭제합니다.
listner는 기존 DB와 연결하는 Connection객체였는데
이것에 대한 설정을 다 classpath:.. 로 바꿔줍니다.
2. servlet.xml 변경 => spring-mvc.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | <?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:websocket="http://www.springframework.org/schema/websocket" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket-4.1.xsd"> <context:component-scan base-package="controller,logic,dao,aop"/> <aop:aspectj-autoproxy/> <mvc:annotation-driven/> <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> <list><value>messages</value></list> </property> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass"> <value>org.springframework.web.servlet.view.JstlView</value><!-- 요것의 의미는 무엇일까??? --> </property> <property name="prefix"><value>/WEB-INF/view/</value></property> <property name="suffix"><value>.jsp</value></property> </bean> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:maxUploadSize="104857600" p:maxInMemorySize="10485760"/> <bean id="exceptionHandler" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"><!-- /WEB-INF/view/exception으로 경로를 --> <value>exception.CartEmptyException=exception exception.LoginException=exception exception.ShopException=exception</value> </property> </bean> <bean id="echoHandler" class="websocket.EchoHandler"/> <websocket:handlers> <websocket:mapping handler="echoHandler" path="/chatting.shop"/> </websocket:handlers> </beans> | cs |
3. applicationContext.xml => spring-db.xml (DBConnection Pool 방식)
[ 전 ] 1) 에서 설명한,
listener는 여기(Connection객체)를 통해서 DB와 연동할수 있었다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><!-- DBConnection이 되는 부분 --> <property name="driverClassName"><value>org.mariadb.jdbc.Driver</value></property> <property name="url"><value>jdbc:mariadb://localhost:3306/bigdb</value></property> <property name="username"><value>scott</value></property> <property name="password"><value>tiger</value></property> </bean> </beans> | cs |
[ 후 ] 2) DataPool 방식을 이용하였다. (<=클릭)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- dataSource : Connection 객체입니다. --> <!-- ConnectionPool(커넥션 풀) 사용하기 커넥션 풀 : 미리 db와 연결된 객체(Connection 객체)를 저장하고 있는 공간 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass"><value>org.mariadb.jdbc.Driver</value></property> <property name="jdbcUrl"><value>jdbc:mariadb://localhost:3306/bigdb</value></property> <property name="user"><value>scott</value></property> <property name="password"><value>tiger</value></property> <property name="maxPoolSize"><value>20</value></property> <property name="minPoolSize"><value>3</value></property> <!-- 연결이 없을때, 최소 Connection객체 갯수. --> <property name="initialPoolSize"><value>5</value></property> <!-- 아무것도 없을 때, 초기 Connection객체 갯수. 처음 만들때 5개의 Connection을 만들어라..--> <property name="acquireIncrement"><value>5</value></property> <!-- 증가 Connection갯수 --> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!-- Dao에 있는 SessionTemplate에 주입이됨. --> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg ref="sqlSessionFactory"/> </bean> </beans> | cs |
반응형