์ค๋์ ์ปจ๋์ ์ด ์ข ๋ซ๋น
ํ ์ ์ข ์ฌ๋ ค๋ณด์์ผ๊ฒ ๋น
ํ์ด์ง๊ธฐ์ ๊ตฌํ ์ 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[
____
]]>
์ฌ์ง ์ญ์
'java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์คํ๋ง๋ถํธ ํ๋ก์ ํธ - ์์ํ๊ธฐ (0) | 2021.02.27 |
---|---|
์คํ๋ง ๋ ๊ฑฐ์ -๊ฒ์ํ ๋ง๋ค๊ธฐ(3) ํ์ด์ง (0) | 2021.02.27 |
JSTL ํฌ๋งท ํ์ (0) | 2021.02.27 |
์คํ๋ง ๋ ๊ฑฐ์ - ๊ฒ์ํ๋ง๋ค๊ธฐ(1) ํ๊ฒฝ์ค์ (0) | 2021.02.27 |
์คํ๋ง AOP (0) | 2021.02.27 |