이제부터 게시판을 만들어볼 계획이담!!
오늘은 일단 게시판 데이터가 들어갈 DB Table을 만들어주자!
게시판에는
no 게시글 번호
title 제목
content 내용
id 아이디
view_cnt 조회수
reg_date 날짜
up_date 업데이트날짜
comment_cnt 댓글수
요렇게 들어가게 만들었다
그리고 각 data에 맞는 DTO 클래스를 만들어준다!
생성자는 기본생성자와 title, content, id만 있는 생성자를 만들어준다
(어차피 글쓸땐 제목 내용 글쓴이만 있으면 됨)
BoardDto
package com.holiday.hi.domain;
import java.util.Date;
public class BoardDto {
private Integer no;
private String title;
private String id;
private String content;
private Integer view_cnt;
private Date reg_date;
private Date up_date;
private Integer comment_cnt;
public BoardDto() {}
public BoardDto(String title, String id, String content) {
this.title = title;
this.id = id;
this.content = content;
}
public Integer getNo() {
return no;
}
public void setNo(Integer no) {
this.no = no;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Integer getView_cnt() {
return view_cnt;
}
public void setView_cnt(Integer view_cnt) {
this.view_cnt = view_cnt;
}
public Date getReg_date() {
return reg_date;
}
public void setReg_date(Date reg_date) {
this.reg_date = reg_date;
}
public Date getUp_date() {
return up_date;
}
public void setUp_date(Date up_date) {
this.up_date = up_date;
}
public Integer getComment_cnt() {
return comment_cnt;
}
public void setComment_cnt(Integer comment_cnt) {
this.comment_cnt = comment_cnt;
}
@Override
public String toString() {
return "BoardDto{" +
"no=" + no +
", title='" + title + '\'' +
", id='" + id + '\'' +
", content='" + content + '\'' +
", view_cnt=" + view_cnt +
", reg_date=" + reg_date +
", up_date=" + up_date +
", comment_cnt=" + comment_cnt +
'}';
}
}
그리고 DB에 직접 관여할 Dao 인터페이스도 만들어준다!
BoardDao
package com.holiday.hi.dao;
import com.holiday.hi.domain.BoardDto;
import java.util.List;
public interface BoardDao {
BoardDto select(String id) throws Exception;
List<BoardDto> selectAll() throws Exception;
int insert(BoardDto dto) throws Exception;
int delete(String id, Integer no) throws Exception;
int update(BoardDto dto) throws Exception;
int count() throws Exception;
int deleteAll() throws Exception;
}
그리고
BoardMapper.xml 에 SQL문을 추가해주고
mybatis-config.xml 에 typeAlias도 추가해준다
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.holiday.hi.dao.BoardMapper">
<select id="select" parameterType="String" resultType="BoardDto">
SELECT *
FROM board
WHERE id= #{id}
</select>
<select id="selectAll" resultType="BoardDto">
SELECT no, title, content, id, view_cnt, comment_cnt, reg_date
FROM board
ORDER BY reg_date DESC, no DESC
</select>
<insert id="insert" parameterType="BoardDto">
INSERT INTO board (title, content, id)
VALUES ( #{title}, #{content}, #{id} )
</insert>
<delete id="delete" parameterType="map">
DELETE FROM board WHERE no = #{no} AND id = #{id}
</delete>
<delete id="deleteAll">
DELETE FROM board
</delete>
<update id="update" parameterType="BoardDto">
UPDATE board
SET title = #{title}, content = #{content}, up_date = now()
WHERE no = #{no} AND id = #{id}
</update>
<select id="count" resultType="int">
SELECT count(*) FROM board
</select>
</mapper>
Mapper를 작성한 후
작성한 sql문을 하나하나 쿼리콘솔에 집어넣고 값을 임의로 줘서
오타가 있는지, 틀린 문장은 없는지 확인했다!
mybatis-config.xml
<typeAliases>
<typeAlias alias="BoardDto" type="com.holiday.hi.domain.BoardDto"/>
</typeAliases>
내일은 Dao를 테스트해보아야겠담 !
728x90
'혼자서 개발새발' 카테고리의 다른 글
Spring) 게시판 쓰기를 만들어보자! (0) | 2022.11.22 |
---|---|
Spring) 게시판 페이지와 읽기 기능을 만들어보자! (0) | 2022.11.22 |
Spring) 회원가입 비밀번호 암호화를 도전해보자! (0) | 2022.11.16 |
Spring) 로그인 기능을 구현해보자! (0) | 2022.11.16 |
Spring) 아이디 중복 확인 기능을 구현해보자! (0) | 2022.11.16 |