java

์Šคํ”„๋ง ๋ ˆ๊ฑฐ์‹œ - ๊ฒŒ์‹œํŒ๋งŒ๋“ค๊ธฐ(2) ๊ธฐ๋Šฅ ๊ตฌํ˜„

dev_summer 2021. 2. 27. 14:30

 

  •  

์˜ค๋Š˜์€ ์ปจ๋””์…˜์ด ์ข€ ๋‚ซ๋‹น

ํ…์…˜ ์ข€ ์˜ฌ๋ ค๋ณด์•„์•ผ๊ฒ ๋‹น

 

 

 

ํŽ˜์ด์ง•๊ธฐ์ˆ  ๊ตฌํ˜„ ์‹œ mysql์€ limit์„ ์‚ฌ์šฉํ•˜์—ฌ ์†์‰ฝ๊ฒŒ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ์˜ค๋ผํด๊ตฌ๋ฒ„์ „์—๋Š” ์ด๊ฒƒ์ด ์—†๋‹ค.

๊ทธ๋Ÿฌ๋‹ˆ๊นŒ ์ด๋ฒˆ์—” ์˜ค๋ผํด

 

 

1. ์˜ค๋ผํด ์…‹ํŒ…์œผ๋กœ ๋ณ€๊ฒฝ

- pom.xml ์ˆ˜์ •

mysql์ด์˜€๋˜ ๊ฒƒ์„ ์˜ค๋ผํด ์…‹ํŒ…์œผ๋กœ ๋ณ€๊ฒฝ

	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
		<property name="url"
			value="jdbc:oracle:thin:@localhost:1521:XE" />
		<property name="username" value="seoha" />
		<property name="password" value="1234" />
	</bean>

 

- jdbc๋นŒ๋“œํŒจ์Šค

 

 

 

 

- ์˜ค๋ผํด database ์ƒ์„ฑ

 CREATE TABLE "SEOHA"."BOARD" 
   (	"BNO" NUMBER, 
	"TITLE" VARCHAR2(100 BYTE), 
	"CONTENT" VARCHAR2(2000 BYTE), 
	"WRITER" VARCHAR2(100 BYTE), 
	"REGDATE" DATE DEFAULT sysdate, 
	"READCOUNT" NUMBER DEFAULT 0
   ) SEGMENT CREATION IMMEDIATE 
  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "SYSTEM" ;

 

 

 

 

2. ๊ธฐ๋ณธ ํŽ˜์ด์ง€ ์…‹ํŒ…

 

- BoardMapper.xml

<?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.seoha.bbs.mapper.BoardMapper">

	<select id="getList" resultType="com.seoha.bbs.dto.BoardVO">
			select * from board
	</select>

	<insert id="insert">
		insert into board (bno, title, content, writer)
		values(board_seq.nextval, #{title}, #{content}, #{writer})
	</insert>

	<insert id="insertSelectKey">
		<selectKey keyProperty="bno" order="BEFORE" resultType="int">
			select board_seq.nextval from dual
		</selectKey>
		insert into board (bno, title, content, writer)
		values(#{bno}, #{title}, #{content}, #{writer})
	</insert>

	<select id="read" resultType="com.seoha.bbs.dto.BoardVO">
		select * from board where bno = #{bno}
	</select>
	
	<update id="update">
		update board set title=#{title},content=#{content} where bno = #{bno}
	</update>
	
	<delete id="delete">
		delete from board where bno =#{bno}
	</delete>

</mapper>

 

 

-BoardService.java

package com.seoha.bbs.service;

import java.util.List;

import com.seoha.bbs.dto.BoardVO;

public interface BoardService {
	public List<BoardVO> selectAll();
	public void insert(BoardVO board);
	public BoardVO selectOne(int bno);
	public void update(BoardVO board);
	public void delete(int bno);
	
}

 

 

 

- BoardServiceImpl.java

package com.seoha.bbs.service;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.seoha.bbs.dto.BoardVO;
import com.seoha.bbs.mapper.BoardMapper;

@Service
public class BoardServiceImpl implements BoardService {

	@Inject
	private BoardMapper mapper;

	@Override
	public List<BoardVO> selectAll() {
		return mapper.getList();
	}

	@Override
	public void insert(BoardVO board) {
		mapper.insertSelectKey(board);
	}

	@Override
	public BoardVO selectOne(int bno) {
		return mapper.read(bno);
	}

	@Override
	public void update(BoardVO board) {
		mapper.update(board);
	}

	@Override
	public void delete(int bno) {
		mapper.delete(bno);
	}

}

 

- boardController.java

package com.seoha.bbs.controller;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.seoha.bbs.dto.BoardVO;
import com.seoha.bbs.service.BoardService;

@Controller
public class BoardController {
	
	@Inject
	BoardService service;
	
	@RequestMapping("/list")
	public String list(Model model) {		
		model.addAttribute("list", service.selectAll());
		return "list";
	}
	
	@GetMapping("input")
	public String insert() {
		return "input";
	}
	
	@PostMapping("/input")
	public String insert(@ModelAttribute("board") BoardVO board) {
		service.insert(board);
		return "redirect:list";
	}
	
	@GetMapping("/update")
	public String update(@RequestParam("bno") int bno, Model model) {
		model.addAttribute("board",service.selectOne(bno));	
		return "update";
	}
	
	@PostMapping("/update")
	public String update(@ModelAttribute("board") BoardVO board) {
		service.update(board);;	
		return "redirect:list";
	}
	
	@GetMapping("/delete")
	public String delete(@RequestParam("bno") int bno) {
		service.delete(bno);
		return "redirect:list";
	}
	
	
}

 

 

 

 

- input.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="input" method="post">
		<table>
			<tr>
				<th>์ œ๋ชฉ</th>
				<td><input type="text" name="title"></td>
			</tr>
			<tr>
				<th>๋‚ด์šฉ</th>
				<td><textarea rows="5" cols="50" name="content"></textarea></td>
			</tr>
			<tr>
				<th>์ž‘์„ฑ์ž</th>
				<td><input type="text" name="writer"></td>
			</tr>
			<tr>
				<td colspan="2" align="center">
					<input type="submit" value="์ €์žฅ">
					<input type="reset" value="์ทจ์†Œ">
					<input type="button" value="๋ชฉ๋ก๋ณด๊ธฐ" onclick="location.href='list'">
				</td>
			</tr>

		</table>
	</form>
</body>
</html>

 

-list.jsp

<%@ 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>seoha</title>
</head>
<body>

	<table border="1">
	
		<tr>
			<td colspan="5" align="right">
			<a href ="input">๊ธ€์“ฐ๊ธฐ</a>
			</td>
	
		<tr>
			<th>๋ฒˆํ˜ธ</th>
			<th>์ œ๋ชฉ</th>
			<th>์ž‘์„ฑ์ž</th>
			<th>๋‚ ์งœ</th>
			<th>์กฐํšŒ์ˆ˜</th>
		</tr>
			<c:forEach items="${list}" var="board">
				<tr> 
					<td>${board.bno }</td>
					<td><a href="update?bno=${board.bno}">${board.title }</a></td>
					<td>${board.writer}</td>
					<td><fmt:formatDate pattern="yy.MM.dd hh:mm" value="${board.regdate }" /></td>
					<td>${board.readcount }</td>
				</tr>
			</c:forEach>
	</table>
</body>
</html>

 

 

 

 

 

 

 

 

* ์ฟผ๋ฆฌ๋ฌธ์— ํŠน์ˆ˜๋ฌธ์ž๊ฐ€ ์žˆ์„๋•Œ ํ•˜๋‚˜์˜ ์ŠคํŠธ๋ง์œผ๋กœ ์ณ๋‹ฌ๋ผ๋Š” ๊ธฐํ˜ธ

<![CDATA[

____

]]>

์‚ฌ์ง„ ์‚ญ์ œ