테이블 생성
create table users(
id number primary key,
username varchar2(20),
password varchar2(20),
email varchar2(50),
createdAt TIMESTAMP
);
CREATE SEQUENCE users_seq
INCREMENT BY 1
START WITH 1;
create table boards(
id number primary key,
title varchar2(150),
content clob,
usersId number,
createdAt TIMESTAMP,
CONSTRAINT fk_users_id foreign key(usersId) references users (id)
);
CREATE SEQUENCE boards_seq
INCREMENT BY 1
START WITH 1;
더미데이터 추가
insert into users(id, username, password, email, createdAt) values(users_seq.nextval, 'ssar', '1234', 'ssar@nate.com', sysdate);
insert into users(id, username, password, email, createdAt) values(users_seq.nextval, 'cos', '1234', 'cos@nate.com', sysdate);
insert into users(id, username, password, email, createdAt) values(users_seq.nextval, 'hong', '1234', 'hong@nate.com', sysdate);
commit;
clob : 오라클에서 가장 큰 데이터 타입 clob(4GB 저장)
username에 unique설정을 해줘야 동일한게 안생김 지금은 넘기고 만들었다
clob과 number는 ()값이 없음
package site.metacoding.red.domain.boards;
import java.sql.Timestamp;
import lombok.Getter;
@Getter
public class Boards {
private Integer id;
private String title;
private String content;
private Integer userId;
private Timestamp createdAt;
}
package site.metacoding.red.domain.boards;
import java.util.List;
import site.metacoding.red.web.dto.reqest.boards.WriteDto;
public interface BoardsDao {
public void insert(WriteDto writeDto);
public Boards findById(Integer id);
public List<Boards> findAll();
public void update(Boards boards);
public void delete(Integer id);
}
일단은 Users처럼 파라미터를 받아놓도록 하자. 나중에 만들면서 수정한다.
<?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="site.metacoding.red.domain.boards.BoardsDao">
<insert id="insert">
INSERT INTO boards(id, title, content, usersId, createdAt)
VALUES(boards_seq.nextval, #{title}, #{content}, #{usersId}, sysdate)
</insert>
<select id="findById" resultType="site.metacoding.red.domain.boards.Boards">
SELECT * FROM boards WHERE id = #{id}
</select>
<select id="findAll" resultType="site.metacoding.red.domain.boards.Boards">
SELECT * FROM boards ORDER BY id DESC
</select>
<update id="update">
UPDATE boards
SET title = #{title},
content = #{content}
WHERE id = #{id}
</update>
<delete id="delete">
DELETE FROM boards WHERE id = #{id}
</delete>
</mapper>
package site.metacoding.red.web;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;
import lombok.RequiredArgsConstructor;
import site.metacoding.red.domain.boards.Boards;
import site.metacoding.red.domain.boards.BoardsDao;
import site.metacoding.red.web.dto.reqest.boards.UpdateDto;
import site.metacoding.red.web.dto.reqest.boards.WriteDto;
import site.metacoding.red.web.dto.response.RespDto;
@RequiredArgsConstructor
@RestController
public class BoardsController {
private final BoardsDao boardsDao;
//private : 모든 상태는 직접 접근을 허용하면 안 됨
@PostMapping("/boards")
//다른 controller의 메서드 이름과 동일성을 부여해주는게 좋음
//그래서 write대신 usersController에 있는 insert를 사용했음
public RespDto<?> insert(WriteDto writeDto){ // x-www머시기(key=value)
boardsDao.insert(writeDto);
return new RespDto<>(1, "글쓰기 성공", null);
}
@GetMapping("/boards")
public RespDto<?> getAll(){
return new RespDto<>(1,"글전체조회성공",boardsDao.findAll());
}
@GetMapping("/boards/{id}")
public RespDto<?> getOne(@PathVariable Integer id){
return new RespDto<>(1,"한건조회성공",boardsDao.findById(id));
}
@PutMapping("/boards/{id}")
public RespDto<?> update(@PathVariable Integer id, UpdateDto updateDto){
//1.영속화
Boards boards = boardsDao.findById(id);
//2.변경
boards.글전체수정(updateDto);
//3.전체 업데이트
return new RespDto<>(1, "글수정성공", null);
}
@DeleteMapping("/boards/{id}")
public RespDto<?> delete(@PathVariable Integer id){
boardsDao.delete(id);
return new RespDto<>(1, "글삭제성공", null);
//return new RespDto<>(1, "글삭제성공", boardsDao.delete(id)); => 오류남
//delete는 return이 없는 void타입 메서드라 body에 들어가지 못한다.
}
}
왜 x-www머시기 타입이냐? 메세지 컨버터가 아니고?(물어보기)
package site.metacoding.red.web.dto.reqest.boards;
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
public class WriteDto {
private String title;
private String content;
private Integer usersId;
}
package site.metacoding.red.web.dto.reqest.boards;
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
public class UpdateDto {
private String title;
private String content;
}
package site.metacoding.red.domain.boards;
import java.sql.Timestamp;
import lombok.Getter;
import site.metacoding.red.web.dto.reqest.boards.UpdateDto;
@Getter
public class Boards {
private Integer id;
private String title;
private String content;
private Integer userId;
private Timestamp createdAt;
public void 전체업데이트(UpdateDto updateDto) {
this.title = updateDto.getTitle();
this.content = updateDto.getContent();
}
}