728x90
반응형
mybatis 설정은 앞에 있음



package com.bit.springBoard2.controller;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.bit.springBoard2.dao.IDao;
@Controller
public class Board2Controller {
// 주입
@Autowired
private SqlSession sqlSession; //servlet-context.xml 에서 생성된 객체를 주입
// writeView 폼
@RequestMapping(value = "writeView")
public String writeView() {
return "writeView";
}
// list
@RequestMapping(value = "list")
public String list (Model model) {
IDao dao = sqlSession.getMapper(IDao.class);
model.addAttribute("list", dao.list());
return "BoardList";
}
// contentView
@RequestMapping(value = "contentView")
public String contentView (int id, Model model) {
IDao dao = sqlSession.getMapper(IDao.class);
dao.upHit(id); // 조회수 증가를 먼저 호출
model.addAttribute("contentView",dao.contentView(id));
return "contentView";
}
// writeView
@RequestMapping(value = "write", method = RequestMethod.POST)
public String write(String name, String title, String content) {
IDao dao = sqlSession.getMapper(IDao.class);
dao.write(name,title,content);
return "redirect:list";
}
// upHit
// delete
@RequestMapping(value = "delete")
public String delete (int id) {
IDao dao = sqlSession.getMapper(IDao.class);
dao.delete(id);
return "redirect:list";
}
// modify
@RequestMapping(value = "modify", method = RequestMethod.POST)
public String modify (String name, String title, String content, int id) {
IDao dao = sqlSession.getMapper(IDao.class);
dao.modify(name, title, content, id);
return "redirect:list";
}
}

package com.bit.springBoard2.dao;
import java.util.ArrayList;
import com.bit.springBoard2.dto.BoardDto;
public interface IDao {
public void write(String name, String title, String content);
public ArrayList<BoardDto> list();
public BoardDto contentView(int id);
public void modify(String name, String title, String content, int id);
public void delete(int id);
public void upHit(int id);
}

<?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="com.bit.springBoard2.dao.IDao">
<select id="list" resultType="com.bit.springBoard2.dto.BoardDto">
select * from tblBoard order by id desc
</select>
<select id="contentView" resultType="com.bit.springBoard2.dto.BoardDto">
select * from tblBoard where id = #{param1}
</select>
<insert id="writeView">
insert into tblBoard (id, name, title, content, hit) values (tblBoardSeq.nextval, #{param1}, #{param2}, #{param3}, 0)
</insert>
<update id="modify">
update tblBoard set name = #{param1}, title = #{param2}, content = #{param3} where id = #{param4}
</update>
<delete id="delete">
delete from tblBoard where id = #{param1}
</delete>
<update id="upHit">
update tblBoard set hit = hit+1 where id = #{param1}
</update>
</mapper>

package com.bit.springBoard2.dto;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
public class BoardDto {
private int Id;
private String Name;
private String Title;
private String Content;
private String regdate;
private int hit;
}

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="writeView">글쓰기</a>
<table border="1">
<tr>
<th>ID</th>
<th>NAME</th>
<th>TITLE</th>
<th>REGDATE</th>
<th>HIT</th>
<th>삭제</th>
<th>수정</th>
</tr>
<c:forEach var="dto" items="${list }">
<tr>
<td>${dto.id }</td>
<td>${dto.name }</td>
<td><a href="contentView?id=${dto.id }">${dto.title }</a></td>
<td>${dto.regdate }</td>
<td>${dto.hit }</td>
<td><a href="delete?id=${dto.id }">삭제</a></td>
<td><a href="modify?id=${dto.id }">수정</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- for:each 를 사용하면 출력이 안된다 items 는 두개떄 출력가능 -->
<form action="modify" method="post">
<table width="500" border="1">
<tr>
<td> 번호 </td>
<td><input name="id" readonly value="${contentView.id }"></td>
</tr>
<tr>
<td> 히트 </td>
<td>${contentView.hit }</td>
</tr>
<tr>
<td> 이름 </td>
<td> <input name="name" value=" ${contentView.name }"></td>
</tr>
<tr>
<td> 제목 </td>
<td><input name="title" value="${contentView.title }"></td>
</tr>
<tr>
<td> 내용 </td>
<td><textarea rows="10" name="content" >${contentView.content }</textarea></td>
</tr>
<tr >
<td colspan="2"><input type="submit" value="수정">
<a href="list">목록보기</a>
<a href="delete?id=${contentView.id }">삭제</a> </td>
</tr>
</table>
</form>
</body>
</html>

<%@ page contentType="text/html; charset=UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>Spring Board</h2>
<form action="write" method="post">
<table width="500" border="1">
<tr>
<td> 이름 </td>
<td> <input name="name" size = "50"> </td>
</tr>
<tr>
<td> 제목 </td>
<td> <input name="title" size = "50"> </td>
</tr>
<tr>
<td> 내용 </td>
<td> <textarea name="content" rows="10" ></textarea> </td>
</tr>
<tr >
<td colspan="2">
<input type="submit" value="입력"> <a href="list">목록보기</a></td>
</tr>
</table>
</form>
</body>
</html>
결과창




ㅇㅇ
728x90
반응형
'Spring > Spring framework' 카테고리의 다른 글
| Spring Boot (0) | 2021.08.04 |
|---|---|
| 1 (0) | 2021.08.04 |
| Command 사용 Board (0) | 2021.08.03 |
| Board (0) | 2021.07.30 |
| Mybatis (2) | 2021.07.29 |
| DB연결 코드와 자주 쓰는 코드 지정해두기 (0) | 2021.07.29 |