목차
1. 스프링 프로젝트 pom.xml 라이브러리 추가
2. WEB-INF/spring/root-context.xml 빈 추가
3. src/main/resources/mapper/mapper.xml DOCTYPE 및 아래 형식 사용
4. UTF-8 filter
스프링프로젝트 pom.xml 라이브러리 추가
1. Mybatis 라이브러리 다운로드
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
2. 오라클 데이터 베이스 라이브러리 다운로드
<repositories>
<repository>
<id>codelds</id>
https://code.lds.org/nexus/content/groups/main-repo
</repository>
</repositories>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
※ 해당 버전 에러 발생 시 아래 파일로 사용
- src/main/WEB_INF/lib/ojdbc6 복사
- 외부 라이브러리 추가
WEB-INF/spring/root-context.xml 빈 추가
1. 스프링 프로젝트에 오라클 연결
<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:@아이피:1521:XE"/>
<property name="username" value="아이디"/>
<property name="password" value="비밀번호"/>
</bean>
2. 스프링 프로젝트에 Mybatis 연결
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:/mapper/*.xml" />
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSession"/>
</bean>
src/main/resources/mapper/mapper.xml 파일에 DOCTYPE 및 아래 형식 사용
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-apper.dtd">
<mapper namespace="mapper">
<!-- result 형식 -->
<resultMap type="map" id="mapList">
<result column="SEQ" property="seq"/>
<result column="MEM_NAME" property="memName"/>
<result column="MEM_ID" property="memId"/>
<result column="BOARD_SUBJECT" property="boardSubject"/>
<result column="BOARD_CONTENT" property="boardContent"/>
<result column="REG_DATE" property="regDate"/>
<result column="UPT_DATE" property="uptDate"/>
<result column="VIEW_CNT" property="viewCnt"/>
</resultMap>
<!-- sql 형식 -->
<select id="readByRow" parameterType="map" resultMap="mapList">
SELECT
SEQ
,MEM_NAME
,MEM_ID
,BOARD_SUBJECT
,BOARD_CONTENT
,REG_DATE
,UPT_DATE
,VIEW_CNT
FROM BOARD_STUDY
WHERE SEQ = #{seq}
</select>
<select id="list" resultMap="mapList">
select
rnum
,SEQ
,MEM_NAME
,MEM_ID
,BOARD_SUBJECT
,BOARD_CONTENT
,REG_DATE
,UPT_DATE
,VIEW_CNT
from (
select
row_number() over(order by seq desc) rnum
,SEQ
,MEM_NAME
,MEM_ID
,BOARD_SUBJECT
,BOARD_CONTENT
,REG_DATE
,UPT_DATE
,VIEW_CNT
from board_study
where 1=1
<choose>
<when test="choose == 'name'">
and mem_name like '%' || #{searchTxt} || '%'
</when>
<when test="choose == 'subject'">
and BOARD_SUBJECT like '%' || #{searchTxt} || '%'
</when>
<when test="choose == 'subCont'">
and (BOARD_SUBJECT like '%' || #{searchTxt} || '%'
or BOARD_CONTENT like '%' || #{searchTxt} || '%')
</when>
</choose>
<if test="cal1 != null and cal1 != ''">
and to_char(reg_date, 'yyyy-MM-dd') between #{cal1} and #{cal2}
</if>
) where rnum between ${pageBegin} and ${pageEnd}
</select>
<select id="pageMap" parameterType="map" resultType="integer">
select count(seq)
from BOARD_STUDY
where 1=1
<choose>
<when test="choose == 'name'">
and mem_name like '%' || #{searchTxt} || '%'
</when>
<when test="choose == 'subject'">
and BOARD_SUBJECT like '%' || #{searchTxt} || '%'
</when>
<when test="choose == 'subCont'">
and (BOARD_SUBJECT like '%' || #{searchTxt} || '%'
or BOARD_CONTENT like '%' || #{searchTxt} || '%')
</when>
</choose>
<if test="cal1 != null and cal1 != ''">
and to_char(reg_date, 'yyyy-MM-dd') between #{cal1} and #{cal2}
</if>
</select>
<insert id="insert">
INSERT INTO BOARD_STUDY (
SEQ
,MEM_NAME
,MEM_ID
,BOARD_SUBJECT
,BOARD_CONTENT
,REG_DATE
,VIEW_CNT
) VALUES (
(SELECT NVL(max(SEQ) + 1, 0) FROM BOARD_STUDY)
,#{writer}
,#{id}
,#{title}
,#{text}
,SYSDATE
,0)
</insert>
<update id="updateRow" parameterType="map">
UPDATE
BOARD_STUDY
SET
MEM_NAME = #{writer}
,MEM_ID = #{id}
,BOARD_SUBJECT = #{title}
,BOARD_CONTENT = #{text}
,UPT_DATE = SYSDATE
WHERE SEQ = #{seq}
</update>
<update id="viewUpdate" parameterType="integer">
UPDATE
BOARD_STUDY
SET VIEW_CNT = VIEW_CNT + 1
WHERE SEQ = #{seq}
</update>
<delete id="delete" parameterType="integer">
DELETE FROM BOARD_STUDY
WHERE SEQ IN
<foreach collection="list" separator="," open="(" close=")" item="checkedList">
#{checkedList}
</foreach>
</delete>
UTF-8 filter
1. web.xml 아래 내용 추가
<!-- UTF-8 인코딩 설정 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2. Tomcat Server
server.xml → Connector → URIEncoding="UTF-8" 추가
'Spring' 카테고리의 다른 글
[Spring] 파일 업로드 & 다운로드 (0) | 2021.06.16 |
---|---|
[Spring] ajax 사용법 (0) | 2021.06.16 |
[Spring] 게시판 페이징 처리 (2) | 2021.06.15 |
[Spring] 달력 datepicker (0) | 2021.06.15 |
[Spring] Spring Tools 4 설치 및 초기설정 (0) | 2021.05.16 |