<!-- Data Source -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
<property name="username" value="spring" />
<property name="password" value="spring123" />
</bean>
<!-- 스프링 jdbc 즉 스프링으로 oracle 디비 연결 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:util/configuration.xml" />
<property name="mapperLocations" value="classpath:sql/*.xml" />
</bean>
<bean id="session" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
package myspring.dao;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class BoardDao {
@Autowired
private SqlSession session;
}
views-board폴더 생성하여 boardform.jsp 저장
index.jsp
<script>
// location.href="home.do";
location.href="boardform.do";
</script>
// 글작성 폼
@RequestMapping("boardform.do")
public String boardform() {
return "board/boardform";
}
// 글작성
@RequestMapping("boardwrite.do")
public String boardwrite(Board board, Model model) {
System.out.println("writer:"+ board.getWriter());
int result = service.insert(board);
System.out.println("result:"+result);
model.addAttribute("result", result);
return "board/insertresult";
}
public int insert(Board board) {
return dao.insert(board);
}
public int insert(Board board) {
return session.insert("", board);
}
<!-- 글작성 -->
<insert id="insert" parameterType="board">
insert into myboard values(myboard_seq.nextval,#{writer},
#{passwd},#{subject},#{content},0,sysdate)
</insert>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:if test="${result == 1 }">
<script>
alert("글작성 성공");
location.href="boardlist.do";
</script>
</c:if>
<c:if test="${result != 1 }">
<script>
alert("글작성 실패");
history.go(-1);
</script>
</c:if>
</body>
</html>
// 글목록
@RequestMapping("boardlist.do")
public String boardlist(HttpServletRequest request, Model model) {
int page = 1; // 현재 페이지 번호
int limit = 10; // 한 페이지에 출력할 데이터 갯수
if(request.getParameter("page") != null) {
page = Integer.parseInt(request.getParameter("page"));
}
// int startRow=(page-1)*limit+1;
// int endRow=page*limit;
// 총 데이터 갯수
int listcount = service.getCount();
System.out.println("listcount:"+listcount);
return "board/boardlist";
}
public int getCount() {
return dao.getCount();
}
public int getCount() {
return session.selectOne("count");
}
<!-- 글갯수 -->
<select id="count" resultType="int">
select count(*) from myboard
</select>
// 총 데이터 갯수
int listcount = service.getCount();
System.out.println("listcount:"+listcount);
List<Board> boardlist = service.getBoardList(page);
System.out.println("boardlist:"+boardlist);
public List<Board> getBoardList(int page) {
return dao.getBoardList(page);
}
public List<Board> getBoardList(int page) {
return session.selectOne("list", page);
}
<!-- 글목록 -->
<!-- 방법 1 : <![CDATA[ ]]> -->
<!-- 방법2 : > : > , < : < -->
<select id="list" parameterType="int" resultType="board">
<![CDATA[
select * from ( select rownum rnum, board.* from (
select * from myboard order by no desc) board )
where rnum >= ((#{page}-1) * 10+1) and rnum <= (#{page}*10)
]]>
</select>
// 총 페이지
int pageCount = listcount/limit+((listcount%limit==0)? 0 : 1);
int startPage = ((page-1)/10 * limit + 1); // 1, 11, 21...
int endPage = startPage + 10 -1; // 10, 20, 30...
if(endPage > pageCount)
endPage = pageCount;
model.addAttribute("page", page);
model.addAttribute("listcount", listcount);
model.addAttribute("boardlist", boardlist);
model.addAttribute("pageCount", pageCount);
model.addAttribute("startPage", startPage);
model.addAttribute("endPage", endPage);
return "board/boardlist";
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>게시판 목록</title>
</head>
<body>
<a href="boardform.do">글작성</a>
글갯수 : ${listcount }
<table border=1 align="center" width=800>
<caption>게시판 목록</caption>
<tr>
<th>번호</th>
<th>제목</th>
<th>작성자</th>
<th>날짜</th>
<th>조회수</th>
</tr>
<!-- 화면 출력 번호 -->
<c:set var="num" value="${listcount-(page-1)*10 }"/>
<c:forEach var="b" items="${boardlist}">
<tr>
<td>${num}
<c:set var="num" value="${num-1}"/>
</td>
<td>
<a href="boardcontent.do?no=${b.no}&page=${page}">
${b.subject}
</a>
</td>
<td>${b.writer}</td>
<td>
<fmt:formatDate value="${b.register}"
pattern="yyyy-MM-dd HH:mm:ss"/>
</td>
<td>${b.readcount}</td>
</tr>
</c:forEach>
</table>
<!-- 페이지 처리 -->
<center>
<c:if test="${listcount > 0}">
<!-- 1page로 이동 -->
<a href="boardlist.do?page=1" style="text-decoration:none"> << </a>
<!-- 이전 블럭으로 이동 -->
<c:if test="${startPage > 10 }">
<a href="boardlist.do?page=${startPage-10}">[이전]</a>
</c:if>
<!-- 각 블럭에 10개의 페이지 출력 -->
<c:forEach var="i" begin="${startPage }" end="${endPage }">
<c:if test="${i == page }"> <!-- 현재 페이지 -->
[${i}]
</c:if>
<c:if test="${i != page }"> <!-- 현재 페이지가 아닌 경우 -->
<a href="boardlist.do?page=${i}">[${i}]</a>
</c:if>
</c:forEach>
<!-- 다음 블럭으로 이동 -->
<c:if test="${endPage < pageCount }">
<a href="boardlist.do?page=${startPage+10}">[다음]</a>
</c:if>
<!-- 마지막 페이지로 이동 -->
<a href="boardlist.do?page=${pageCount }"
style="text-decoration:none"> >> </a>
</c:if>
</center>
</body>
</html>
// 상세 페이지 : 조회수 1증가 + 상세정보 구하기
@RequestMapping("boardcontent.do")
public String boardcontent(int no, String page, Model model) {
service.updatecount(no); // 조회수 1증가
return "board/boardcontent";
}
public void updatecount(int no) {
dao.updatecount(no);
}
public void updatecount(int no) {
session.update("hit", no);
}
<!-- 조회수 증가 -->
<update id="hit" parameterType="int">
update myboard set readcount=readcount+1 where no=#{no}
</update>
// 상세 페이지 : 조회수 1증가 + 상세정보 구하기
@RequestMapping("boardcontent.do")
public String boardcontent(int no, String page, Model model) {
service.updatecount(no); // 조회수 1증가
Board board = service.getBoard(no); // 상세 정보 구하기
return "board/boardcontent";
}
public Board getBoard(int no) {
return dao.getBoard(no);
}
public Board getBoard(int no) {
return session.selectOne("content", no);
}
코드를 입력하세요