기존에 JDBC로 구현한 게시판을 Spring ORM(Mybatis)를 이용하여 만들어보자
Spring Web MVC + MyBatis
1. SpringWebMybatis 프로젝트 생성(기존 JDBC 게시판 프로젝트를 복사하여 생성)
-properties에서 context root 수정
- 라이브러리 설치
- pom.xml 작성
<!-- mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.2</version> </dependency> <!-- mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> |
- 설치된 라이브러리 물리적 파일 확인
3. servlet-context.xml
제어의역전 구조 boardDAO2 > Session Template > SQL Session Factory Bean > dataSource
<?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="com.jdbc.springweb" /> <beans:bean id="boardDAO2" class="com.jdbc.dao.BoardDAO2"> <beans:property name="sessionTemplate" ref="sessionTemplate"/> </beans:bean>
<beans:bean id="sessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <beans:constructor-arg ref="sessionFactory"/> </beans:bean>
<beans:bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <beans:property name="dataSource" ref="dataSource"/> <!-- MyBatis 환경설정파일 : 경로 주의!! --> <beans:property name="configLocation" value="classpath:/myBatis-config.xml"/> </beans:bean>
<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <beans:property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> <beans:property name="url" value="jdbc:oracle:thin:@192.168.16.16:1521:TestDB"/> <beans:property name="username" value="SUZI"/> <beans:property name="password" value="A123"/> </beans:bean>
<beans:bean id="myUtil" class="com.jdbc.util.MyUtil"/> </beans:beans> |
4. myBatis-config.xml 작성
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <properties> <property name="driver" value="oracle.jdbc.driver.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@192.168.16.16:1521:TestDB"/> <property name="username" value="SUZI"/> <property name="password" value="A123"/> </properties> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/jdbc/mybatis/boardMapper.xml"/> </mappers> </configuration> |
5. boardMapper.xml 작성
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="boardMapper"> <select id="maxNum" resultType="int"> select nvl(max(num),0) from board </select> <insert id="insertData" parameterType="com.jdbc.dto.BoardDTO"> insert into board(num,name,pwd,email,subject,content,ipAddr,hitCount,created) values (#{num},#{name},#{pwd},#{email},#{subject},#{content},#{ipAddr},0,sysdate) </insert> <select id="getDataCount" parameterType="hashMap" resultType="int"> select nvl(count(*),0) from board where ${searchKey} like '%' || #{searchValue} || '%' </select> <select id="getLists" parameterType="hashMap" resultType="com.jdbc.dto.BoardDTO"> select * from ( select rownum rnum, data.* from( select num,name,subject,hitCount,to_char(created,'YYYY-MM-DD') created from board where ${searchKey} like '%' || #{searchValue} || '%' order by num desc) data) <![CDATA[ where rnum>=#{start} and rnum<=#{end} ]]> </select> <select id="getReadData" parameterType="int" resultType="com.jdbc.dto.BoardDTO"> select num,name,pwd,email,subject,content,ipAddr,hitCount,created from board where num=#{num} </select> <update id="updateHitCount" parameterType="int"> update board set hitCount = hitCount+1 where num=#{num} </update> <update id="updateData" parameterType="com.jdbc.dto.BoardDTO"> update board set name=#{name},pwd=#{pwd},email=#{email},subject=#{subject},content=#{content} where num=#{num} </update> <delete id="deleteData" parameterType="int"> delete board where num=#{num} </delete> </mapper> |
6. boardDAO2 작성
package com.jdbc.dao; import java.util.HashMap; import java.util.List; import org.mybatis.spring.SqlSessionTemplate; import com.jdbc.dto.BoardDTO; public class BoardDAO2 {
private SqlSessionTemplate sessionTemplate;
public void setSessionTemplate(SqlSessionTemplate sessionTemplate) throws Exception{ this.sessionTemplate = sessionTemplate; } public int getMaxNum(){ int maxNum = 0; maxNum = sessionTemplate.selectOne("boardMapper.maxNum"); return maxNum; }
public void insertData(BoardDTO dto){ sessionTemplate.insert("boardMapper.insertData",dto); }
public List<BoardDTO> getList(int start, int end,String searchKey, String searchValue){ HashMap<String, Object> params = new HashMap<String, Object>(); params.put("start", start); params.put("end", end); params.put("searchKey", searchKey); params.put("searchValue", searchValue);
List<BoardDTO> lists = sessionTemplate.selectList("boardMapper.getLists",params); return lists; }
public int getDataCount(String searchKey,String searchValue){ HashMap<String, Object> params = new HashMap<String, Object>(); params.put("searchKey", searchKey); params.put("searchValue", searchValue);
int result = sessionTemplate.selectOne("boardMapper.getDataCount",params); return result; }
public void updateHitCount(int num){ sessionTemplate.update("boardMapper.updateHitCount",num); }
public BoardDTO getReadData(int num){ BoardDTO dto = sessionTemplate.selectOne("boardMapper.getReadData", num); return dto; }
public void deleteData(int num){ sessionTemplate.delete("boardMapper.deleteData",num); }
public void updateData(BoardDTO dto){ sessionTemplate.update("boardMapper.updateData",dto); }
} |
- 게시판 작성 페이지
- 게시판 리스트 조회 페이지
- 게시판 수정 페이지
- 게시글 조회 페이지
'Dev > Spring' 카테고리의 다른 글
[Spring] Annotation 간략 정리 (0) | 2019.12.11 |
---|---|
Spring3.0 - Spring WEB MVC (0) | 2019.04.19 |
Spring3.0 - MVC web & JDBC 게시판만들기 (0) | 2019.04.18 |
Spring3.0 - AOP (0) | 2019.04.17 |
Spring3.0 - ORM(MyBatis) (0) | 2019.04.17 |