[Spring] SpringMVC + iBatis 사용하기(스프링에서 지원하는 template 사용하기)|
2010. 5. 25. 17:08ㆍFrameWork/Spring
전체적으로 iBatis의 기본설정은 크게 바뀌지 않는다. 다만 SpringFramework는 DI를 지원하기 때문에 이 설정만 들어가게 되면 다른게 없다.
1.web.xml 의 context-param 부분에 ibatis의 설정이 들어가 있는 xml 파일의 위치를 정해준다.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/config/applicationContext.xml,
/WEB-INF/config/applicationContext-iBatis.xml
</param-value>
</context-param>
2. applicationContext-iBatis.xml 파일에 DataSource를 설정하고 DataSource를 사용하는 sqlMapClient,sqlMapClient를 사용하는 template를 만든다.
template를 사용하기 위해서는 spring-orm.jar 파일을 라이브러리에 추가해야 한다.
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 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="user01"/>
<property name="password" value="user01"/>
<property name="initialSize" value="10"/>
<property name="maxActive" value="100"/>
<property name="maxIdle" value="16"/>
<property name="maxWait" value="2000"/>
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="WEB-INF/config/sqlMapConfig.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="template" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient" ref="sqlMapClient"/>
</bean>
위의 Datasource를 구현방법은 DBCP를 사용하는 방법이기 때문에 www.apache.org 에 들어가서 commonms --> dbcp 에서 다운 받은 라이브러리를 추가하여야 사용할 수 있다.
3. sqlMapClient를 사용하기 위해서 로딩한 configLocation인 sqlMapConfig.xml 파일을 설정한다.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0 //EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings
cacheModelsEnabled="true"
enhancementEnabled="true"
lazyLoadingEnabled="true"
maxRequests="32"
maxSessions="10"
maxTransactions="5"
useStatementNamespaces="true"
/>
<sqlMap resource="kr/co/springboard/dao/sqlmap/sqlMapBoard.xml"/>
</sqlMapConfig>
4. 위의 빨간색의 sqlMapBoard.xml 파일을 생성한다.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap>
<typeAlias alias="Board" type="kr.co.springboard.dto.BoardDTO"/>
<resultMap class="Board" id="boardResult">
<result property="seq" column="SEQ"/>
<result property="category" column="CATEGORY"/>
<result property="title" column="TITLE"/>
<result property="readCount" column="READ_COUNT"/>
<result property="writer" column="WRITER"/>
<result property="regDate" column="REG_DATE"/>
</resultMap>
<resultMap class="Board" id="boardFullResult">
<result property="seq" column="SEQ"/>
<result property="category" column="CATEGORY"/>
<result property="title" column="TITLE"/>
<result property="readCount" column="READ_COUNT"/>
<result property="writer" column="WRITER"/>
<result property="regDate" column="REG_DATE"/>
<result property="contents" column="CONTENTS"/>
</resultMap>
<select id="selectBoardTotalCount" resultClass="int">
SELECT COUNT(0)
FROM MY_BOARD
</select>
<select id="selectBoardList" parameterClass="Board" resultMap="boardResult">
<![CDATA[
select
*
from
(
select *
from
( select * from MY_BOARD order by SEQ ASC )
where rownum <= #startNum#
order by SEQ desc
)
where rownum <= #countPerPage#
]]>
</select>
<insert id="insertBoard" parameterClass="Board">
INSERT INTO
MY_BOARD
(SEQ,CATEGORY,TITLE,WRITER,REG_DATE,READ_COUNT,CONTENTS)
VALUES
(MY_BOARD_SEQ.NEXTVAL,#category#,#title#,#writer#,sysdate,0,#contents#)
</insert>
<select id="selectBoardItem" parameterClass="int" resultMap="boardFullResult">
SELECT
*
FROM MY_BOARD
WHERE SEQ=#seq#
</select>
<update id="updateBoardReadCount" parameterClass="int">
UPDATE
MY_BOARD
SET
READ_COUNT = READ_COUNT + 1
WHERE SEQ = #seq#
</update>
<update id="updateBoardItem" parameterClass="Board">
UPDATE
MY_BOARD
SET
CATEGORY = #category#,
WRITER = #writer#,
TITLE = #title#,
CONTENTS = #contents#
WHERE SEQ = #seq#
</update>
<delete id="deleteBoardItem" parameterClass="int">
DELETE
FROM MY_BOARD
WHERE SEQ = #seq#
</delete>
</sqlMap>
1.web.xml 의 context-param 부분에 ibatis의 설정이 들어가 있는 xml 파일의 위치를 정해준다.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/config/applicationContext.xml,
/WEB-INF/config/applicationContext-iBatis.xml
</param-value>
</context-param>
2. applicationContext-iBatis.xml 파일에 DataSource를 설정하고 DataSource를 사용하는 sqlMapClient,sqlMapClient를 사용하는 template를 만든다.
template를 사용하기 위해서는 spring-orm.jar 파일을 라이브러리에 추가해야 한다.
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 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="user01"/>
<property name="password" value="user01"/>
<property name="initialSize" value="10"/>
<property name="maxActive" value="100"/>
<property name="maxIdle" value="16"/>
<property name="maxWait" value="2000"/>
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="WEB-INF/config/sqlMapConfig.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="template" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient" ref="sqlMapClient"/>
</bean>
위의 Datasource를 구현방법은 DBCP를 사용하는 방법이기 때문에 www.apache.org 에 들어가서 commonms --> dbcp 에서 다운 받은 라이브러리를 추가하여야 사용할 수 있다.
3. sqlMapClient를 사용하기 위해서 로딩한 configLocation인 sqlMapConfig.xml 파일을 설정한다.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0 //EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings
cacheModelsEnabled="true"
enhancementEnabled="true"
lazyLoadingEnabled="true"
maxRequests="32"
maxSessions="10"
maxTransactions="5"
useStatementNamespaces="true"
/>
<sqlMap resource="kr/co/springboard/dao/sqlmap/sqlMapBoard.xml"/>
</sqlMapConfig>
4. 위의 빨간색의 sqlMapBoard.xml 파일을 생성한다.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap>
<typeAlias alias="Board" type="kr.co.springboard.dto.BoardDTO"/>
<resultMap class="Board" id="boardResult">
<result property="seq" column="SEQ"/>
<result property="category" column="CATEGORY"/>
<result property="title" column="TITLE"/>
<result property="readCount" column="READ_COUNT"/>
<result property="writer" column="WRITER"/>
<result property="regDate" column="REG_DATE"/>
</resultMap>
<resultMap class="Board" id="boardFullResult">
<result property="seq" column="SEQ"/>
<result property="category" column="CATEGORY"/>
<result property="title" column="TITLE"/>
<result property="readCount" column="READ_COUNT"/>
<result property="writer" column="WRITER"/>
<result property="regDate" column="REG_DATE"/>
<result property="contents" column="CONTENTS"/>
</resultMap>
<select id="selectBoardTotalCount" resultClass="int">
SELECT COUNT(0)
FROM MY_BOARD
</select>
<select id="selectBoardList" parameterClass="Board" resultMap="boardResult">
<![CDATA[
select
*
from
(
select *
from
( select * from MY_BOARD order by SEQ ASC )
where rownum <= #startNum#
order by SEQ desc
)
where rownum <= #countPerPage#
]]>
</select>
<insert id="insertBoard" parameterClass="Board">
INSERT INTO
MY_BOARD
(SEQ,CATEGORY,TITLE,WRITER,REG_DATE,READ_COUNT,CONTENTS)
VALUES
(MY_BOARD_SEQ.NEXTVAL,#category#,#title#,#writer#,sysdate,0,#contents#)
</insert>
<select id="selectBoardItem" parameterClass="int" resultMap="boardFullResult">
SELECT
*
FROM MY_BOARD
WHERE SEQ=#seq#
</select>
<update id="updateBoardReadCount" parameterClass="int">
UPDATE
MY_BOARD
SET
READ_COUNT = READ_COUNT + 1
WHERE SEQ = #seq#
</update>
<update id="updateBoardItem" parameterClass="Board">
UPDATE
MY_BOARD
SET
CATEGORY = #category#,
WRITER = #writer#,
TITLE = #title#,
CONTENTS = #contents#
WHERE SEQ = #seq#
</update>
<delete id="deleteBoardItem" parameterClass="int">
DELETE
FROM MY_BOARD
WHERE SEQ = #seq#
</delete>
</sqlMap>
'FrameWork > Spring' 카테고리의 다른 글
[Spring] Spring의 Controller들. 2.SimpleFormController (0) | 2010.05.25 |
---|---|
[Spring] Spring의 Controller들. 1.AbastractCommandController (0) | 2010.05.25 |
[Spring] Spring FrameWork 와 Log4J 사용해서 Log출력하기 (0) | 2010.05.25 |
[Spring] 초기설정2 (0) | 2010.05.25 |
[Spring] 초기설정 (0) | 2010.05.25 |