์ ํ๋ก์ ํธ ์์ฑ - my_board
pom_xml์์ ๋ฒ์ ์์ ํ์
์ฌ์ฉ ๋ฒ์

web_xml ํ๊ธ ์ธ์ฝ๋ฉ ํํฐ ์ค์ ์ถ๊ฐ

home.jsp์

UTF-8 ์ถ๊ฐํจ.

DB์ฐ๊ฒฐ - ํ ์ด๋ธ ์์ฑ
ํ์ํ ํ ์ด๋ธ board, user,reply ์ธ ๊ฐ
CREATE TABLE board(
bno int not null auto_increment,
title varchar(50) not null,
id varchar(50) not null,
content text not null,
regDate timestamp not null default now(),
viewCnt int default 0,
primary key(bno),
FOREIGN KEY(id) REFERENCES user(id)
);
CREATE TABLE user(
id varchar(50) not null,
password varchar(50) not null,
regDate timestamp not null default now(),
primary key(id)
);
CREATE TABLE reply (
rno int not null auto_increment,
bno int not null,
id varchar(50) not null,
content text not null,
regDate timestamp not null default now(),
likeCnt int default 0,
PRIMARY KEY(rno, bno),
FOREIGN KEY(bno) REFERENCES board(bno),
FOREIGN KEY(id) REFERENCES user(id)
);
pom.xml - db์ฐ๊ฒฐ์ ์ํด ์ค์ ์ถ๊ฐ
<!-- database -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- commons-dbcp2 : ์ปค๋ฅ์
ํ -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.7.0</version>
</dependency>
<!-- log4jdbc-log4j2-jdbc4 : SQL ์คํ ์ ๋ก๊ทธ -->
<dependency>
<groupId>org.bgee.log4jdbc-log4j2</groupId>
<artifactId>log4jdbc-log4j2-jdbc4</artifactId>
<version>1.16</version>
</dependency>
<!-- mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
root-context.xml

<!-- ๋ฐ์ดํฐ์์ค์ DB์ฐ๊ฒฐ ์ ๋ณด๋ฅผ ์
๋ ฅ -->
<bean id="dataSource"
class="org.apache.commons.dbcp2.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="com.mysql.cj.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/my_board?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8" />
<property name="username" value="root" />
<property name="password" value="1234" />
</bean>
<!-- MyBatis sql์ธ์
ํฉํ ๋ฆฌ ๊ฐ์ฒด(๋ฐ์ดํฐ ์์ค ์ฃผ์
) ์์ datasource๊ฐ ์ฃผ์
๋จ -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations"
value="classpath:/mappers/**/*.xml" />
</bean>
<!-- sqlํฉํ ๋ฆฌ ์ฃผ์
ํ์ฌ sql๋ฉ์๋ ์คํ ๊ฐ์ฒด -->
<bean id="sqlSessionTemplate"
class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>

DBํ ์ด๋ธ my_board ์ ๋ ฅํจ .
sql ์ฟผ๋ฆฌ๋ฌธ ์์ฑ ํ mappers ํด๋ ์์ฑ - ๋ง์ฝ ํด๋ ๋ช ์ ๋ค๋ฅด๊ฒ ํ๊ณ ์ถ์ผ๋ฉด ์๋์ value ๊ฒฝ๋ก ๋ณ๊ฒฝ ํ์

java/resources์์ mappersํด๋ ์์ฑ ํ
์ฟผ๋ฆฌ๋ฌธ ์์ฑํ xmlํ์ผ ์์ฑํจ

๊ฐ xml ํ์ผ์์ ์ฟผ๋ฆฌ๋ฌธ์ ์์ฑํ๋ค๊ณ ํด์ ๋ฌด์กฐ๊ฑด myBatis์ฌ์ฉ ๊ฐ๋ฅํ๊ฒ์ด ์๋
์๋์ ์ฝ๋ ์ถ๊ฐ ํmapper ์์ ์์ฑํด์ผํ๋ค.
<?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="board">
</mapper>
mapper namespace๋ ๊ฐ xml ํ์ผ์ ์ด๋ฆ์ ๋ํ๋ด๋๊ฑฐ๋ผ
๊ฐ๊ฒ ์ ๋ ฅํ๋ฉด ๊ฐ์ xmlํ์ผ๋ก ์ธ์ํจ. ๊ทธ๋์ ์ด๋ฆ์ ๋ฌ๋ฆฌ ์ค์ ํ๋ค.
board ์ฟผ๋ฆฌ๋ฌธ ์์ฑํ xmlํ์ผ์ board ,,
user๋ user ๋ฑ
ํ์ฌ๋ ์์ง ์ฟผ๋ฆฌ๋ฌธ ์์ฑ์ํ ๊ฑฐ๋ผ์ ์์ ์ฝ๋๋ง ๋ฃ์ด์ค
VO์์ฑ

https://kkminseok.github.io/posts/Spring_semina/
Spring DAO๋?, DTO๋?, VO๋?, DTO vs VO, DTO vs Domain, DAO vs Repository
๊ท์ผ๋ฅ์ด ํ๋ฐฐ๋ค๊ณผ ์คํฐ๋๋ฅผ ํ๋ฉด์ ์ธ๋ฏธ๋๋ฅผ ํ๊ธฐ๋ก ํ๋ค.
kkminseok.github.io
VO(Value Object)
๊ฐ ๊ณ์ธต๊ฐ ๋ฐ์ดํฐ ๊ตํ์ ์ํ ์๋ฐ ๊ฐ์ฒด๋ฅผ ์๋ฏธ.
๋ ์ด์ด ๊ฐ์ ์ ๋ฌํ๋ ๋ชฉ์ ์ ๊ฐ์ง๊ณ ์์ผ๋ฉฐ, ๊ฐ์ฒด์ ์์ฑ๊ณผ getter, setter๋ง์ ๊ฐ์ง๊ณ ์์.
๊ณ์ธต๊ฐ ๋ฐ์ดํฐ ๊ตํ์ ์ํ ์๋ฐ ๋น์ฆ(Java Beans)์ด๋ค.
- ์๋ฐ ๋น์ฆ?
- ์๋ฐ๋ก ์์ฑ๋ ์ํํธ์จ์ด ์ปดํฌ๋ํธ๋ค์ ์ง์นญํ๋ ๋จ์ด
- MVC๋ชจ๋ธ ๊ธฐ์ค Model์ด JavaBeans
- JSP์์๋ ์ฐ์ด๋๋ฐ JSP๋ฌธ๋ฒ์ ๋ชจ๋ฅด๋ pass → JSP ํ์ผ ๋ด์์ ์ฌ์ฉ์ด ๊ฐ๋ฅํ ๊ฐ์ฒด๋ผ๊ณ ์๊ฐ.
์ฆ DTO๋ Database์์ Data๋ฅผ ์ป์ด Service๋ Controller ๋ฑ์ผ๋ก ๋ณด๋ผ ๋ ์ฌ์ฉํ๋ ๊ฐ์ฒด.
- VO(Value Object) vs DTO
VO๋ DTO์ ๋์ผํ ๊ฐ๋ ์ด์ง๋ง read only ์์ฑ์ ๊ฐ์.
VO๋ ํน์ ํ ๋น์ฆ๋์ค ๊ฐ์ ๋ด๋ ๊ฐ์ฒด์ด๊ณ , DTO๋Layer๊ฐ์ ํต์ ์ฉ๋๋ก ์ค๊ณ ๊ฐ๋ ๊ฐ์ฒด๋ฅผ ๋งํจ.
BoardVO
package com.myboard.domain;
import java.util.Date;
public class BoardVO {
// CREATE TABLE board(
// bno int not null auto_increment,
// title varchar(50) not null,
// id varchar(50) not null,
// content text not null,
// regDate timestamp not null default now(),
// viewCnt int default 0,
// primary key(bno),
// FOREIGN KEY(id) REFERENCES user(id)
// );
private int bno;
private String title;
private String text;
private String id;
private String content;
private Date regDate;
private int viewCnt;
//๊ฒํฐ ์ธํฐ
public int getBno() {
return bno;
}
public void setBno(int bno) {
this.bno = bno;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
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 Date getRegDate() {
return regDate;
}
public void setRegDate(Date regDate) {
this.regDate = regDate;
}
public int getViewCnt() {
return viewCnt;
}
public void setViewCnt(int viewCnt) {
this.viewCnt = viewCnt;
}
}
ReplyVO
package com.myboard.domain;
import java.util.Date;
public class ReplyVO {
// CREATE TABLE reply (
// rno int not null auto_increment,
// bno int not null,
// id varchar(50) not null,
// content text not null,
// regDate timestamp not null default now(),
// likeCnt int default 0,
// PRIMARY KEY(rno, bno),
// FOREIGN KEY(bno) REFERENCES board(bno),
// FOREIGN KEY(id) REFERENCES user(id)
// );
private int rno;
private int bno;
private String id;
private String content;
private Date regDate;
private int likeCnt;
//getter setter
public int getRno() {
return rno;
}
public void setRno(int rno) {
this.rno = rno;
}
public int getBno() {
return bno;
}
public void setBno(int bno) {
this.bno = bno;
}
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 Date getRegDate() {
return regDate;
}
public void setRegDate(Date regDate) {
this.regDate = regDate;
}
public int getLikeCnt() {
return likeCnt;
}
public void setLikeCnt(int likeCnt) {
this.likeCnt = likeCnt;
}
}
UserVO
package com.myboard.domain;
import java.util.Date;
public class UserVO {
// CREATE TABLE user(
// id varchar(50) not null,
// password varchar(50) not null,
// regDate timestamp not null default now(),
// primary key(id)
// );
private String id;
private String password;
private Date regDate;
//getter setter
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getRegDate() {
return regDate;
}
public void setRegDate(Date regDate) {
this.regDate = regDate;
}
}
์ผ๋จ ์ ์ db์ ์ ์ ์์ฑ๋ง ํด๋์
INSERT INTO user(id,password)
VALUES('user1234',1234);
SELECT * FROM my_board.user;'BACKEND > Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| ๊ฒ์ํ ๋ง๋ค๊ธฐ 2 - ๊ธฐ๋ณธ ์ค์ ๋ฐ include(nav,footer ๋ถ๋ฆฌ) (0) | 2023.10.26 |
|---|---|
| ๊ฒ์ํ ๋ง๋ค๊ธฐ 2 - MVC์ ๊ตฌ์กฐ (0) | 2023.10.26 |
| ๊ฒ์ํ ๋ง๋ค๊ธฐ - ๋๊ธ ์ญ์ ํ๊ธฐ (0) | 2023.10.25 |
| ๊ฒ์ํ ๋ง๋ค๊ธฐ - ๋๊ธ ์์ ํ๊ธฐ (0) | 2023.10.25 |
| ๊ฒ์ํ ๋ง๋ค๊ธฐ - ๋๊ธ ์์ฑ(write) (0) | 2023.10.25 |