2014년 1월 28일 화요일

K. Spring Framework 게시판 만들기 (MyBatis적용) , 스프링프레임워크 게시판 소스

K. Spring Framework 게시판 만들기 (MyBatis적용) , 스프링프레임워크 게시판 소스

 



1. 시작하기 
- MyBatis란? 
  개발자가 지정한 SQL, 저장프로시저, 그리고 고급맵핑을 지원하는 프레임워크

2. /spring/appServlet/servlet-context.xml
<?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 http://www.springframework.org/schema/mvc/spring-mvc.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.xsd
">

<!-- com.onj.board 패키지의 Class들을 Scan한다. [Controller]-->
<context:component-scan base-package="com.onj.board" /> 
<!-- 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/" />

<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:@localhost:1521:ex"/>
<beans:property name="username" value="study"/> 
<beans:property name="password" value="study"/>
</beans:bean>
<beans:bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource"/>
<beans:property name="configLocation" value="classpath:myBatis/board/modelConfig/model-config.xml"/>
<beans:property name="mapperLocations" value="classpath:myBatis/board/sql/*.xml"/> 
</beans:bean>
 
<beans:bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<beans:constructor-arg index="0" ref="sqlSessionFactoryBean"/>
</beans:bean>

<!-- 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/jsp/"/>
<beans:property name="suffix" value=".jsp" /> 
</beans:bean>
</beans:beans>

3. BoardDAOImple.java

package com.board.dao;


import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;

import com.board.model.BoardDTO;
import com.board.model.CommentDTO;

@Component("boardDAO")
public class BoardDAOImple implements BoardDAO {

@Resource(name="sqlSession")
private SqlSession sqlSessionTemplate;
private String sql = "";
// 게시글 수
public int boardCount(Map<String, Object>searchMap)throws DataAccessException{
if(searchMap.get("boardListSearchText") == null || searchMap.get("boardListSearchText").equals("")){
sql = "select count(*) from board02";
}else{
String boardListSelect = (String) searchMap.get("boardListSelect");
String boardListSearchText = (String) searchMap.get("boardListSearchText");
sql = "select count(*) from board02 where "+boardListSelect+" like '%"+boardListSearchText+"%'";
}
BoardDAO dao = sqlSessionTemplate.getMapper(BoardDAO.class);
int count = dao.boardCount(searchMap);

return count; 
}
// 게시판 리스트
public List<BoardDTO> boardList(Map<String, Object>searchMap) throws DataAccessException {
if(searchMap.get("boardListSearchText") == null || searchMap.get("boardListSearchText").equals("")){ 
sql = "select * from ("
+ " select  ROWNUM r,seq ,name,title ,TO_CHAR(regdate,'YYYY/MM/DD')as regdate, readcount,"
+ "        reply, reply_step, reply_level "
+ " from "
+ " (select * from board02 "
+ " order by reply desc, reply_step asc"
+ " )"
+ " )"
+ " where r BETWEEN "+ searchMap.get("startRow") +" AND "+ searchMap.get("endRow") +"";
}else{
String boardListSelect = (String) searchMap.get("boardListSelect");
String boardListSearchText = (String) searchMap.get("boardListSearchText");
sql = "select * from ("
+ " select  ROWNUM r,seq ,name,title ,TO_CHAR(regdate,'YYYY/MM/DD')as regdate, readcount, "
+ "        reply, reply_step, reply_level "
+ " from " 
+ " (select * from board02 "
+ " where "
+ "  "+boardListSelect+" like '%"+boardListSearchText+"%'"
+ " order by reply desc, reply_step asc"
+ " )" 
+ " )"
+ " where r BETWEEN "+ searchMap.get("startRow") +" AND "+ searchMap.get("endRow") +"";
}
BoardDAO dao = sqlSessionTemplate.getMapper(BoardDAO.class);
List<BoardDTO> boardList = dao.boardList(searchMap);

return boardList;
}
// 게시물 본문내용 미리보기
public String preView(String seq) throws DataAccessException{
sql = "select content from board02 where seq = "+seq+"";
BoardDAO dao = sqlSessionTemplate.getMapper(BoardDAO.class);
String preContent = dao.preView(seq);
return preContent;
}
// 게시글 조회수 1씩증가
public int updateReadCount(String seq)throws DataAccessException{
sql = " update board02 set readcount = nvl(readcount,0)+1 where seq = "+seq+"";
BoardDAO dao = sqlSessionTemplate.getMapper(BoardDAO.class);
int re = dao.updateReadCount(seq);
return re;
}
// 게시글 상세보기
public BoardDTO readContent(String seq)throws DataAccessException{
// 조회수 1증가 메소드 호출
this.updateReadCount(seq);
String sql = "select * from board02 where seq = "+seq+"";
BoardDAO dao = sqlSessionTemplate.getMapper(BoardDAO.class);
BoardDTO boardDTO = dao.readContent(seq);
return boardDTO;
}

// 코멘트 저장
public int insertComment(CommentDTO commentDTO) throws DataAccessException {
sql = "insert into comment_t02 values(sequence_comment_seq.nextval,"+commentDTO.getComment_name()+","+commentDTO.getComment_comm()+","+commentDTO.getSeq()+")";
BoardDAO dao = sqlSessionTemplate.getMapper(BoardDAO.class);
int re = dao.insertComment(commentDTO);
return re;
}

// 코멘트 조회
public List<CommentDTO> ListComment(String seq) throws DataAccessException {
sql = "select * from comment_t02 where seq = "+seq+" order by comment_seq desc";
BoardDAO dao = sqlSessionTemplate.getMapper(BoardDAO.class);
List<CommentDTO> list = dao.ListComment(seq);
return list;
}
// 게시글 입력
public int insertBoard(BoardDTO boardDTO)throws DataAccessException{
sql = "insert into board02 values(sequence_board_seq.nextval,"
+ ""+boardDTO.getName()+","
+ ""+boardDTO.getPasswd()+","
+ ""+boardDTO.getTitle()+","
+ ""+boardDTO.getContent()+","
+ ""+boardDTO.getFilename()+","
+ "sysdate,0,sequence_board_seq.currval,0,0)";
BoardDAO dao = sqlSessionTemplate.getMapper(BoardDAO.class);
int re = dao.insertBoard(boardDTO);
return re;
}
// 글 수정
public int updateBoard(BoardDTO boardDTO)throws DataAccessException{
sql = "update board02 set name = "+boardDTO.getName()+" ,"
+"title = "+boardDTO.getTitle()+","
+"content = "+boardDTO.getContent()+" "
+ "where seq = "+boardDTO.getSeq()+"";
BoardDAO dao = sqlSessionTemplate.getMapper(BoardDAO.class);
int re = dao.updateBoard(boardDTO);
return re;
}
// 게시글 삭제
public int deleteBoard(BoardDTO boardDTO)throws DataAccessException{
if(boardDTO.getReply_level() == 0){
sql = "delete from board02 where reply = "+boardDTO.getSeq()+"";
}else{ 
sql = "delete from board02 where seq = "+boardDTO.getSeq()+"";
}
this.AllDeleteComment(boardDTO.getSeq());
 
BoardDAO dao = sqlSessionTemplate.getMapper(BoardDAO.class);
int re = dao.deleteBoard(boardDTO);
return re;
}
 
// 코멘트 전체 삭제
public int AllDeleteComment(String seq)throws DataAccessException{
sql = "delete from comment_t02 where seq = "+seq+"";
BoardDAO dao = sqlSessionTemplate.getMapper(BoardDAO.class);
int re = dao.AllDeleteComment(seq);
return re;
}
// 답변 스텝 증가
public int replyUpdate(BoardDTO boardDTO)throws DataAccessException{
sql ="update board02 set reply_step = reply_step + 1 "
   +"where reply = "+boardDTO.getReply()+" and reply_step > "+boardDTO.getReply_step()+""; 
BoardDAO dao = sqlSessionTemplate.getMapper(BoardDAO.class);
int re = dao.replyUpdate(boardDTO);
return  re;
}

// 답글 달기
public int replyBoard(BoardDTO boardDTO)throws DataAccessException{

this.replyUpdate(boardDTO); 
 
sql = "insert into board02 values(sequence_board_seq.nextval,"
+ ""+boardDTO.getName()+","
+ ""+boardDTO.getPasswd()+","
+ ""+boardDTO.getTitle()+","
+ ""+boardDTO.getContent()+","
+ ""+boardDTO.getFilename()+","
+ "sysdate,0,sequence_board_seq.currval,"
+ ""+boardDTO.getReply_step()+1+","+boardDTO.getReply_level()+1+")";

BoardDAO dao = sqlSessionTemplate.getMapper(BoardDAO.class);
int re = dao.replyBoard(boardDTO);
return re;
// SQL리턴용
public String getSql()throws DataAccessException{
return sql;
}

}


4. [ /resource/myBatis/board/modelConfig/modelConfig.xml ] ,
    [ /resource/myBatis/board/sql/boardSql.xml ]
- xml File로 만들어야됨.

<?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>
<typeAliases>
<typeAlias type="com.board.model.BoardDTO" alias="boardDTO"/>
<typeAlias type="com.board.model.CommentDTO" alias="commentDTO"/>
<typeAlias type="com.board.model.BoardLogDTO" alias="boardLogDTO"/>
</typeAliases>
</configuration>


------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.board.dao.BoardDAO">
<resultMap type="boardDTO" id="boardDTO">
<result property="seq"         column="seq"/>
<result property="name"        column="name"/>
<result property="passwd"      column="passwd"/>
<result property="title"       column="title"/>
<result property="content"     column="content"/>
<result property="filename"    column="filename"/>
<result property="regdate"     column="regdate"/>
<result property="readcount"   column="readcount"/>
<result property="reply"       column="reply"/>
<result property="reply_step"  column="reply_step"/>
<result property="reply_level" column="reply_level"/> 
</resultMap>
<resultMap type="commentDTO" id="commentDTO">
<result property="comment_seq"  column="comment_seq"/>
<result property="comment_name" column="comment_name"/>
<result property="comment_comm" column="comment_comm"/>
<result property="seq"          column="seq"/>
</resultMap>
<!-- 게시글 수 -->
<select id="boardCount" resultType="java.lang.Integer">
select count(*) from board02
<if test="boardListSearchText != null ">
<if test="!boardListSearchText.equals('')">
where ${boardListSelect} like '%' || #{boardListSearchText} || '%'
</if>
</if> 
</select>    
<!-- 게시판 리스트 -->
<select id="boardList" resultMap="boardDTO">
select * from (
select ROWNUM r,seq ,
  name,title ,TO_CHAR(regdate,'YYYY/MM/DD')as regdate, readcount,
  reply, reply_step, reply_level
   from
    (select * from board02
     
    <if test="boardListSearchText != null">
    <if test="!boardListSearchText.equals('')">
    where ${boardListSelect} like '%' || #{boardListSearchText} || '%'
    </if>
    </if>
     
     order by reply desc, reply_step asc
    )
 )
where r BETWEEN #{startRow} AND #{endRow}
</select>
<!-- 게시물 본문내용 미리보기 -->
<select id="preView" parameterType="java.lang.String" resultType="java.lang.String">
select content from board02 where seq = #{seq}
</select>
<!-- 게시글 조회수 1씩 증가 -->
<update id="updateReadCount" parameterType="java.lang.String">
update board02 set readcount = nvl(readcount,0)+1 where seq = #{seq}
</update>
<!-- 게시글 상세보기 -->
<select id="readContent" parameterType="java.lang.String" resultMap="boardDTO">
select * from board02 where seq = #{seq}
</select>
<!-- 코멘트 저장 -->
<insert id="insertComment" parameterType="boardDTO">
insert into comment_t02  values(sequence_comment_seq.nextval,#{comment_name},#{comment_comm},#{seq})
</insert>
<!-- 코멘트 조회 -->
<select id="ListComment" parameterType="java.lang.String" resultMap="commentDTO">
select * from comment_t02 where seq = #{seq} order by comment_seq desc
</select> 
 
<!-- 게시글 입력 -->
<insert id="insertBoard" parameterType="boardDTO">
insert into board02 values(sequence_board_seq.nextval,#{name},#{passwd},#{title},#{content},#{filename},sysdate,0,
  sequence_board_seq.currval,0,0)
</insert>
<!-- 글 수정 -->
<update id="updateBoard" parameterType="boardDTO">
update board02 set name = #{name}, title = #{title}, content = #{content} where seq = #{seq}
</update>
<!-- 게시글 삭제 -->
<delete id="deleteBoard" parameterType="boardDTO">
delete from board02 where
<choose>
<when test="reply_level == 0"> 
reply = #{seq}
</when>
<otherwise>
seq = #{seq}
</otherwise>
</choose>
</delete>
<!-- 코멘트 전체 삭제 -->
<delete id="AllDeleteComment" parameterType="java.lang.String">
delete from comment_t02 where seq = #{seq}
</delete>
<!-- 답글 달기 -->
<insert id="replyBoard" parameterType="boardDTO">
insert into board02 values(sequence_board_seq.nextval,#{name},#{passwd},#{title},#{content},#{filename},sysdate,0,
  #{reply},#{reply_step}+1,#{reply_level}+1)
</insert>
<!-- step up -->
<update id="replyUpdate" parameterType="boardDTO"> 
update board02 set reply_step = reply_step + 1
where reply = #{reply} and reply_step > #{reply_step}+1
</update>
</mapper>

5. AOP부분도 DAO랑 동일하게 바꾸주면됨.




댓글 없음:

댓글 쓰기