2013년 12월 1일 일요일

Spring Framework3.2게시판(MyBatis적용), spring mybatis 게시판, 스프링마이바티스게시판 22번까지 작성된 게시판에 MyBatis를 적용하여 구현

Spring Framework3.2게시판(MyBatis적용), spring mybatis 게시판, 스프링마이바티스게시판
 
22번까지 작성된 게시판에  MyBatis를 적용하여 구현
 
BoardDAO.java
 
package onj.board.dao;
import java.util.List;
import org.springframework.dao.DataAccessException;
import onj.board.model.BoardDTO;
import onj.board.model.CommentDTO;
public interface BoardDAO {
 // 게시물 리스트 보기
 public List<BoardDTO> boardList() throws DataAccessException;
 
 // 게시물 본문 미리보기
 public String preView(String seq) throws DataAccessException;
 
 // 게시물 본문 읽기
 public BoardDTO readContent(String seq) throws DataAccessException;
 
 // 읽은 글의 조회수 1증가
 public int updateReadCount(String seq) throws DataAccessException;
 
 // Comment저장
 public int insertComment(CommentDTO commentDTO) throws DataAccessException;
 
 // Comment조회
 public List<CommentDTO> commentList(String seq) throws DataAccessException;
 
 // 게시글 입력
 public int insertBoard(BoardDTO board) throws DataAccessException;
 
 // 글 수정
 public int updateBoard(BoardDTO board) throws DataAccessException;
 
 // 글 삭제
 public int deleteBoard(BoardDTO boardDto) throws DataAccessException;
 // 답글 step plus
 public void replyBoardStep(BoardDTO boardDto) throws DataAccessException;
 
 // 답글 달기
 public int replyBoard(BoardDTO board)throws DataAccessException;
 
 // SQL리턴
 public String getSql();
}
 
---------------------------------------------------------------------------------------
 
SpringBoardDAO.java
 
package onj.board.dao;
import java.util.List;
import javax.annotation.Resource;
import onj.board.model.BoardDTO;
import onj.board.model.CommentDTO;
import org.apache.ibatis.session.SqlSession;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;
@Component("BoardDAO")
public class SpringBoardDAO implements BoardDAO{
 @Resource(name="sqlSession")
 private SqlSession sqlSessionTemplate;
 String sql ="";
 public String getSql(){return sql;}
 
 // 게시판 전체 리스트 보기
 public List<BoardDTO> boardList() throws DataAccessException{
  
  List<BoardDTO> boardList = null;
  
  BoardDAO boardDao = sqlSessionTemplate.getMapper(BoardDAO.class);
  
  boardList = boardDao.boardList;
  
  return boardList;
 }
 // 게시물 본문내용 미리보기(/preView)
 public String preView(String seq) throws DataAccessException {
  
  String preContent;
  
  BoardDAO boardDao = sqlSessionTemplate.getMapper(BoardDAO.class);
  preContent = boardDao.preView(seq);
  
  return preContent;
 }
 
 // 게시판 상세보기, 게시글 읽기
 public BoardDTO readContent(String seq) throws DataAccessException{
  BoardDTO boardDto = null;
  
  BoardDAO boardDao = sqlSessionTemplate.getMapper(BoardDAO.class);
  boardDto = boardDao.readContent();
  
  // 글 조회 1증가
  this.updateReadCount(new Integer(seq).toString());
  
  return boardDto;
 }
 
 
 // 읽은 글의 조회수를 1증가
 public int updateReadCount(String seq) throws DataAccessException{
   
  int count = 0;
  
  BoardDAO boardDao = sqlSessionTemplate.getMapper(BoardDAO.class);
  
  count = boardDao.updateReadCount(seq);
  
  return count;
  
 }
 
 
 // 커멘트 입력
 public int insertComment(CommentDTO commentDTO)throws DataAccessException{
  
  int re = 0;
  
  BoardDAO boardDao = sqlSessionTemplate.getMapper(BoardDAO.class);
  
  re = boardDao.insertComment(commentDTO);
   
  return re;
 }
  
 // 커멘트 조회
 public List<CommentDTO> commentList(String seq) throws DataAccessException{
   List<CommentDTO> list = null; 
   
   BoardDAO boardDao = sqlSessionTemplate.getMapper(BoardDAO.class);
   
   list = boardDao.commentList(seq);
   
   return list; 
 }
  
 // 글쓰기
 public int insertBoard(BoardDTO board) throws DataAccessException{
  
  int re = 0;
  
  BoardDAO boardDao = sqlSessionTemplate.getMapper(BoardDAO.class);
  
  re = boardDao.insertBoard(board);
  
  return re;
 }
 // 글 수정 
 public int updateBoard(BoardDTO board) throws DataAccessException {
  
  int re = 0;
  
  BoardDAO boardDao = sqlSessionTemplate.getMapper(BoardDAO.class);
  
  re = boardDao.updateBoard(board);
  
  return re;
 }
 // 게시글 삭제
 public int deleteBoard(BoardDTO boardDto)
   throws DataAccessException {
  
  int re = 0;
  
  BoardDAO boardDao = sqlSessionTemplate.getMapper(BoardDAO.class);
  
  re = boardDao.deleteBoard(boardDto);
  
  return re;
 }
 
 // 답글 step plus
 public void replyBoardStep(BoardDTO boardDto) throws DataAccessException {
  BoardDAO boardDao = sqlSessionTemplate.getMapper(BoardDAO.class);
  boardDao.replyBoardStep(boardDto);
 }
 
 // 달급달기
 public int replyBoard(BoardDTO boardDTO) throws DataAccessException {
  
  int re = 0;
  
  BoardDAO boardDao = sqlSessionTemplate.getMapper(BoardDAO.class);
  re = boardDao.replyBoard(boardDTO);
  
  return re; 
 }
 
 
}
---------------------------------------------------------------------------------------
 
action-servlet.xml
 
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
     http://www.springframework.org/schema/conext 
     http://www.springframework.org/schema/context/spring-context-3.2.xsd
     ">   
 <!-- context -->  
 <context:component-scan base-package="board.controller"/>
 
 <bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
  <property name="driverClassName" value="oracle.jdbc.drver.OracleDriver"/>
  <property name="url" value="jdbc:oracle:thin:@localhost:1521:onj"/> 
  <property name="username" value="scott"/>
  <property name="password" value="tiger"/> 
 </bean>
   
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource"/>
  <property name="configLocation" value="classpath:onj/mybatis/model/sqlmap-config.xml "/>
  <property name="mapperLocations" value="classpath:onj/mybatis/*.xml"/> 
  
 </bean>   
         
 <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
  <constructor-arg index="0" ref="sqlSessionFactory"/> 
 </bean> 
 <!-- 뷰 리졸버 --> 
 <!-- 
   컨트롤러의 메소드에서 리턴되는 ModelAndView의 뷰 이름을 취하여 실제 클라이언트에 
   보여질 뷰와 매핑 하는데 prefix특성의 값과 suffix특성의 값을 각가 접두어와 접미어로서 붙인다. 
  -->
 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix">
   <value>/jsp/</value>
  </property>
  <property name="suffix">
   <value>.jsp</value>
  </property>
 </bean>
 
</beans>
 
---------------------------------------------------------------------------------------
 
sqlmap-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>
 <typeAliases>
  <typeAlias type="onj.board.model.BoardDTO" alias="boardDTO"/>
  <typeAlias type="onj.board.model.CommentDTO" alias="commentDTO"/>
 </typeAliases>
 
</configuration>
 
---------------------------------------------------------------------------------------
 
baordSql.xml
 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="onj.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>
 
 <!-- 게시판 전체 리스트 보기 -->
 <select id="boardList" resultMap="boardDTO">
  select * from 
  (select seq,name,passwd,title,content,filename,regdate,readcount,reply,reply_step,reply_level,rownum r from board order by reply desc,reply_step asc)
 </select> 
 
 <!-- 미리보기 -->
 <select id="preView" parameterType="java.lang.String" resultType="java.lang.String">
  select content from board where seq = #{seq} 
 </select>
 
 <!-- 게시글 상세보기 -->
 <select id="readContent" parameterType="java.lang.String" resultMap="boardDTO">
  select * from board where seq = #{seq} 
 </select>
 
 <!-- 읽은 글의 조회수를 1증가 -->
 <update id="updateReadCount" parameterType="java.lang.String">
  update board set readcount = nvl(readcount,0)+1 where seq = #{seq}
 </update>
 
 <!-- 게시글 입력  -->
 <insert id="insertBoard" parameterType="boardDTO">
  insert into board values(board_seq.nextval,#{name},#{passwd},#{title},#{content},#{fileName},sysdate,0,board_seq.currval,0,0)
 </insert>
 
 <!-- 글 수정 -->
 <update id="updateBoard" parameterType="boardDTO">
  update board set name = #{name}, title=#{title} , content = #{content} where seq = #{seq}
 </update>
 
 <!-- 글 삭제 -->
 <delete id="deleteBoard" parameterType="boardDTO" >  
  delete from board where seq = #{seq} and passwd=#{passwd}
 </delete>
 
 <!-- 답글달기 -->
 
 <!-- 현재 답변의 단 게시물 보다 더 높은 스텝의 게시물이 있다면 스탭을 하나씩 상승시킴 -->
 <update id="replyBoardStep" parameterType="java.lang.Integer" >
  update board set reply_step = reply_step+1 where reply = #{reply} and reply_step > reply_step
 </update>
 
 <!-- reply_step과 reply_level을 1씩 증가 시킨 후 내용을 저장  -->
 <insert id="replyBoard" parameterType="boardDTO">
  insert into board values(board_seq.nextval,#{name},#{passwd},#{title},#{content},#{fileName},sysdate,0,#{reply},#{reply_step}+1,#{reply_step}+1)
 </insert> 
 
 
</mapper> 
 
---------------------------------------------------------------------------------------
 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper3.0//EN" "http://mybatis.org/dtd/mybatis-1-mapper.dtd">
<mapper namespace="onj.board.dao.BoardDAO">
 
 <resultMap type="commentDTO" id="commentDTO">
  <result property="seq" column="seq"/>
  <result property="name" column="name"/>
  <result property="comment" column="comm"/>
 </resultMap>
 
 <!-- 커멘트 입력 -->
 <insert id="insertComment" parameterType="commentDTO">
  insert into comment_t(seq,name,comm) values(#{seq},#{name},#{comment})
 </insert> 
 
 <!-- 커멘트 출력 -->
 <select id="commentList" parameterType="java.lang.String" resultMap="commentDTO">
  select * from comment_t where seq = #{seq}
 </select>
 
 
 
</mapper> 

댓글 없음:

댓글 쓰기