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>
[오프라인 개강예정강좌, 오프라인교육장에 오시면 보다 자세히 배울 수 있습니다.]
오라클자바커뮤니티에서 운영하는 개발자 전문교육 ,개인80%환급(www.onjprogramming.co.kr)
[주간]
[12/09]iPhone 하이브리드 앱 개발 실무과정
[12/09][기업100%환급]Spring Framework,MyBatis,Hibernate실무프로젝트과 정
[12/16][기업100%환급]PL/SQL,ORACLE HINT,TUNING
[12/16]웹퍼블리싱 마스터
[12/16]안드로이드개발자과정
[12/23][기업100%환급]자바기초에서 JDBC, Servlet/JSP까지
[01/06][기업100%환급]SQL기초에서 Schema Object까지
[평일야간]
[12/03]웹퍼블리싱 마스터
[12/05]자바초보에서안드로이드까지
[12/09]C#,ASP.NET마스터
[12/09]iPhone하이브리드앱개발실무과정
[12/09]JAVA&WEB프레임워크실무과정
[12/10]Spring3.X, MyBatis, Hibernate실무과정
[12/10]SQL초보에서실전전문가까지
[주말]
[12/14]Spring3.X,MyBatis,Hibernate실무과정
[12/14]SQL초보에서전문가까지
[12/14]웹퍼블리싱 마스터
[12/21]자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis까지
[12/21]자바초보에서안드로이드까지
[12/28]C#,ASP.NET마스터
오라클자바커뮤니티에서 운영하는 개발자 전문교육 ,개인80%환급(www.onjprogramming.co.kr)
[주간]
[12/09]iPhone 하이브리드 앱 개발 실무과정
[12/09][기업100%환급]Spring Framework,MyBatis,Hibernate실무프로젝트과 정
[12/16][기업100%환급]PL/SQL,ORACLE HINT,TUNING
[12/16]웹퍼블리싱 마스터
[12/16]안드로이드개발자과정
[12/23][기업100%환급]자바기초에서 JDBC, Servlet/JSP까지
[01/06][기업100%환급]SQL기초에서 Schema Object까지
[평일야간]
[12/03]웹퍼블리싱 마스터
[12/05]자바초보에서안드로이드까지
[12/09]C#,ASP.NET마스터
[12/09]iPhone하이브리드앱개발실무과정
[12/09]JAVA&WEB프레임워크실무과정
[12/10]Spring3.X, MyBatis, Hibernate실무과정
[12/10]SQL초보에서실전전문가까지
[주말]
[12/14]Spring3.X,MyBatis,Hibernate실무과정
[12/14]SQL초보에서전문가까지
[12/14]웹퍼블리싱 마스터
[12/21]자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis까지
[12/21]자바초보에서안드로이드까지
[12/28]C#,ASP.NET마스터
댓글 없음:
댓글 쓰기