Search

MyBatis Plus ์†Œ๊ฐœ

MyBatis Plus ์†Œ๊ฐœ

๊ธฐ๋ณธ CRUD, ์ž๋™ ๋งคํ•‘, ์ฟผ๋ฆฌ ๋นŒ๋” ๋“ฑ, SQL ์ฟผ๋ฆฌ ๋ฐ ์„ค์ • ์—†์ด๋„ MyBatis ํ”„๋ ˆ์ž„์›Œํฌ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ ๊ธฐ๋Šฅ์„ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” MyBatis ํ™•์žฅ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

์ฃผ์š” ๊ธฐ๋Šฅ

โ€ข
CRUD ์ž๋™ ์ƒ์„ฑ
โ€ข
ํŽ˜์ด์ง• ์ฒ˜๋ฆฌ
โ€ข
์กฐ๊ฑด ๋นŒ๋”

์ฃผ์š” ํŠน์ง•

โ€ข
CRUD ๋ฉ”์„œ๋“œ ์ œ๊ณต : BaseMapper๋กœ ๊ธฐ๋ณธ์ ์ธ CRUD ๊ธฐ๋Šฅ (selectById, insert, deleteById ๋“ฑ) ์ง€์›.
โ€ข
์กฐ๊ฑด ๋นŒ๋” ์ง€์› : QueryWrapper, LambdaQueryWrapper ๋“ฑ์œผ๋กœ ๋™์  ์ฟผ๋ฆฌ๋ฅผ ๊ฐ„๋‹จํžˆ ์ž‘์„ฑ ๊ฐ€๋Šฅ.
โ€ข
์ฝ”๋“œ ์ƒ์„ฑ๊ธฐ ์ œ๊ณต : ์—”ํ‹ฐํ‹ฐ, ๋งคํผ, XML, ์„œ๋น„์Šค ๋“ฑ์„ ์ž๋™ ์ƒ์„ฑํ•˜์—ฌ ์ดˆ๊ธฐ ๊ฐœ๋ฐœ ์‹œ๊ฐ„ ๋‹จ์ถ•.
โ€ข
ํŽ˜์ด์ง• ๊ธฐ๋Šฅ ๋‚ด์žฅ : Page ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด ๊ฐ„๋‹จํžˆ ํŽ˜์ด์ง• ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ.
โ€ข
ํ”Œ๋Ÿฌ๊ทธ์ธ ์‹œ์Šคํ…œ : ํŽ˜์ด์ง•, SQL ๋ถ„์„, ๋‹ค์ค‘ ํ…Œ๋„ŒํŠธ ๋“ฑ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ ํ™•์žฅ ๊ฐ€๋Šฅ.
โ€ข
Lambda ํ‘œํ˜„์‹ ์ง€์› : ํ•„๋“œ๋ช…์„ ์•ˆ์ „ํ•˜๊ฒŒ ์‚ฌ์šฉํ•ด ์œ ์ง€๋ณด์ˆ˜์„ฑ์„ ๋†’์ธ ์ฟผ๋ฆฌ ์ž‘์„ฑ ๊ฐ€๋Šฅ.
โ€ข
๋‹ค์ค‘ ํ…Œ๋„ŒํŠธ ์ง€์› : ๋‹ค์ค‘ ์‚ฌ์šฉ์ž ํ™˜๊ฒฝ์—์„œ ๋ฐ์ดํ„ฐ ๊ฒฉ๋ฆฌ ์ž๋™ ์ฒ˜๋ฆฌ.
โ€ข
SQL ์ž๋™ ์ƒ์„ฑ : XML ์—†์ด Java ์ฝ”๋“œ๋งŒ์œผ๋กœ ๋™์  SQL ์ž‘์„ฑ ๊ฐ€๋Šฅ.
โ€ข
์„œ๋น„์Šค ๊ณ„์ธต ํ†ตํ•ฉ : Service์™€ ServiceImpl๋กœ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง๊ณผ ๋ฐ์ดํ„ฐ ๊ณ„์ธต์„ ์‰ฝ๊ฒŒ ๊ด€๋ฆฌ.
โ€ข
๊ฐ•๋ ฅํ•œ ์ปค๋ฎค๋‹ˆํ‹ฐ : ํ’๋ถ€ํ•œ ๋ฌธ์„œ์™€ ์ƒ˜ํ”Œ ์ฝ”๋“œ๋กœ ํ•™์Šต๊ณผ ๋ฌธ์ œ ํ•ด๊ฒฐ ์ง€์›.

์ฃผ์š” ๊ธฐ๋Šฅ

โ€ข
CRUD ์ž๋™ ์ƒ์„ฑ
โ€ข
ํŽ˜์ด์ง• ์ฒ˜๋ฆฌ
โ€ข
์กฐ๊ฑด ๋นŒ๋”

CRUD ์ž๋™ ์ƒ์„ฑ

MyBatis Plus ์—์„œ ์ œ๊ณตํ•˜๋Š” ServiceImpl<M extends BaseMapper<T>, T> ํด๋ž˜์Šค์— CRUD์™€ ๊ด€๋ จ๋œ ๊ณตํ†ต ๋กœ์ง์„ ๊ตฌํ˜„์ด ์ด๋ฏธ ๊ตฌํ˜„๋˜์–ด์žˆ์–ด ์ด๋ฏธ ๊ตฌํ˜„๋œ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ๋งŒ์œผ๋กœ CRUD ๋“ฑ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ์™€ ๊ด€๋ จ๋œ ์—ฌ๋Ÿฌ ๊ธฐ๋Šฅ๋“ค์ด ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.

CRUD ์ž๋™ ์ƒ์„ฑ ์„ค์ •

MyBatis Plus ๊ฐ€ CRUD ๋“ฑ์˜ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ๋กœ์ง์„ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•˜๋ ค๋ฉด, ์•„๋ž˜์™€ ๊ฐ™์ด ์„ค์ •์„ ํ•ด์ฃผ์–ด์•ผํ•ฉ๋‹ˆ๋‹ค.
1.
ํ…Œ์ด๋ธ” ์ด๋ฆ„ ์„ค์ •
2.
ํ…Œ์ด๋ธ”์˜ ๊ธฐ๋ณธํ‚ค(PK) ์„ค์ •
3.
ServiceImpl ํด๋ž˜์Šค ์ƒ์†

์˜ˆ์‹œ ํ…Œ์ด๋ธ”

DROP TABLE IF EXISTS `board`; CREATE TABLE `board` ( `no` int NOT NULL AUTO_INCREMENT PRIMARY KEY, -- โšก PK (๊ธฐ๋ณธํ‚ค) `id` varchar(100) NOT NULL, `title` varchar(100) NOT NULL, `writer` varchar(100) NOT NULL, `content` text, `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `views` int NOT NULL DEFAULT '0' ) COMMENT='๊ฒŒ์‹œํŒ';
SQL
๋ณต์‚ฌ
์˜ˆ์‹œ ํ…Œ์ด๋ธ” board์—์„œ, no ์ปฌ๋Ÿผ์ด PK (๊ธฐ๋ณธํ‚ค)๋กœ ์ง€์ •๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

ํ…Œ์ด๋ธ” ์ด๋ฆ„ ์„ค์ •

ํ…Œ์ด๋ธ” ์ด๋ฆ„ ์„ค์ •์€ ๋„๋ฉ”์ธ(์—”ํ„ฐํ‹ฐ) ๊ฐ์ฒด์— @TableName("ํ…Œ์ด๋ธ”๋ช…") ์–ด๋…ธํ…Œ์ด์…˜์„ ์ž‘์„ฑํ•˜์—ฌ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
@Data @TableName("board") // ํ…Œ์ด๋ธ”๋ช… (๐Ÿฆ๏ธŽ๐Ÿ’™ Mybatis plus) @Alias("Board") // typealias ๋ณ„์นญ (๐Ÿฆ๏ธŽ๐Ÿ’— Mybatis) public class Board { ... }
Java
๋ณต์‚ฌ
@TableName ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ํ…Œ์ด๋ธ” ๋ช… board ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

ํ…Œ์ด๋ธ”์˜ ๊ธฐ๋ณธํ‚ค(PK) ์„ค์ •

๋“ฑ๋ก, ์กฐํšŒ, ์ˆ˜์ •, ์‚ญ์ œ (C.R.U.D) ๊ธฐ๋Šฅ์ด ์ ์šฉ๋˜๋ ค๋ฉด, ์–ด๋–ค ํ…Œ์ด๋ธ”์—์„œ ์–ด๋–ค ์ปฌ๋Ÿผ์„ ๊ธฐ์ค€์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ง€ ๊ธฐ๋ณธํ‚ค ์ปฌ๋Ÿผ(PK)๋ฅผ ์•Œ์•„์•ผํ•ฉ๋‹ˆ๋‹ค.
MyBatis Plus ์—๊ฒŒ ๋„๋ฉ”์ธ(์—”ํ„ฐํ‹ฐ) ๊ฐ์ฒด์˜ ์–ด๋–ค ๋ณ€์ˆ˜๊ฐ€ ๊ธฐ๋ณธํ‚ค์™€ ๋งคํ•‘๋˜๋Š” ๋ณ€์ˆ˜์ธ์ง€ ์ง€์ •ํ•ด์ฃผ๋ฉด PK ๋ฅผ ์ธ์‹ํ•˜์—ฌ ์ž๋™์œผ๋กœ C.R.U.D ๊ฐ€ ๋™์ž‘๋˜๋„๋ก ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
@Data @TableName("board") // ํ…Œ์ด๋ธ”๋ช… (๐Ÿฆ๏ธŽ๐Ÿ’™ Mybatis plus) @Alias("Board") // typealias ๋ณ„์นญ (๐Ÿฆ๏ธŽ๐Ÿ’— Mybatis) public class Board { @TableId(type = IdType.AUTO) // PK ์„ค์ • ๋ฐ ์ž๋™์ฆ๊ฐ€ (๐Ÿฆ๏ธŽ๐Ÿ’™ Mybatis plus - CRUD ์— ์‚ฌ์šฉ) private int no; private String id; private String title; private String writer; private String content; private Date createdAt; private Date updatedAt; private int views; public Board() { this.id = UUID.randomUUID().toString(); } }
Java
๋ณต์‚ฌ
@TableId(type = IdType.AUTO) // PK ์„ค์ • ๋ฐ ์ž๋™์ฆ๊ฐ€ (๐Ÿฆ๏ธŽ๐Ÿ’™ Mybatis plus - CRUD ์— ์‚ฌ์šฉ) private int no;
Java
๋ณต์‚ฌ
@TableId ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ no ๋ณ€์ˆ˜(์ปฌ๋Ÿผ)์„ PK(๊ธฐ๋ณธํ‚ค)๋กœ ์ง€์ •ํ•˜๊ณ , type = IdType.AUTO ์†์„ฑ์„ ํ†ตํ•ด์„œ AUTO_INCREMENT ์˜ต์…˜์„ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค.

ServiceImpl ํด๋ž˜์Šค ์ƒ์†

MyBatis Plus ์˜ ๊ธฐ๋ณธ CRUD ๋ฐ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ๊ธฐ๋Šฅ์€ ServiceImpl ํด๋ž˜์Šค์— ์ •์˜๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์€ ํด๋ž˜์Šค์—์„œ CRUD ๋ฐ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
@Service public class BoardServiceImpl extends ServiceImpl<BoardMapper, Board> implements BoardService { @Autowired private BoardMapper boardMapper; /* โญ ์ด๋ฏธ ๊ธฐ๋ณธ CRUD ๊ฐ€ ๋˜์–ด ์žˆ์œผ๋‹ˆ, ๊ธฐ๋ณธ์ ์ธ CRUD ๋Š” ์ฝ”๋”ฉํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค๐Ÿ‘ */ }
Java
๋ณต์‚ฌ

ServiceImpl

MyBatis Plus์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋ณธ ์„œ๋น„์Šค ๊ตฌํ˜„ ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด CRUD์™€ ๊ด€๋ จ๋œ ๊ณตํ†ต ๋กœ์ง์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ฐœ๋ฐœ์ž๋Š” ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์—๋งŒ ์ง‘์ค‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ServiceImpl์˜ ์ฃผ์š” ๋ฉ”์„œ๋“œ

์•„๋ž˜๋Š” MyBatis Plus์˜ ServiceImpl์—์„œ ์ œ๊ณตํ•˜๋Š” ์ฃผ์š” ๋ฉ”์„œ๋“œ๋ฅผ ํ‘œ๋กœ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.
๋ฉ”์„œ๋“œ
์„ค๋ช…
getById(Serializable id)
ID๋กœ ์—”ํ‹ฐํ‹ฐ ์กฐํšŒ (id PK ์ปฌ๋Ÿผ)
list()
์กฐ๊ฑด ์—†์ด ์ „์ฒด ์—”ํ‹ฐํ‹ฐ ์กฐํšŒ
save(T entity)
๋‹จ์ผ ์—”ํ‹ฐํ‹ฐ ์ €์žฅ
saveBatch(Collection<T> entityList)
์—”ํ‹ฐํ‹ฐ ๋ฐฐ์น˜ ์ €์žฅ
updateById(T entity)
ID๋กœ ์—”ํ‹ฐํ‹ฐ ์—…๋ฐ์ดํŠธ (id PK ์ปฌ๋Ÿผ)
removeById(Serializable id)
ID๋กœ ์—”ํ‹ฐํ‹ฐ ์‚ญ์ œ (id PK ์ปฌ๋Ÿผ)
count()
์ „์ฒด ๋ฐ์ดํ„ฐ ๊ฐœ์ˆ˜ ์กฐํšŒ

์˜ˆ์‹œ์ฝ”๋“œ

1.
???.sql
2.
???.java
a.
@TableName("???")
b.
@TableId(type = IdType.AUTO)
3.
???Mapper.xml
4.
???Mapper.java
a.
@Mapper
5.
???Service.java
6.
???SeviceImpl.java
a.
extends ServiceImpl<???Mapper, ???>
7.
???Controller.java
8.
views
a.
???
i.
list.html
ii.
insert.html
iii.
read.html
iv.
update.html
b.
index.html

???.sql

DROP TABLE IF EXISTS `board`; CREATE TABLE `board` ( `no` int NOT NULL AUTO_INCREMENT PRIMARY KEY, `id` varchar(100) NOT NULL, `title` varchar(100) NOT NULL, `writer` varchar(100) NOT NULL, `content` text, `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `views` int NOT NULL DEFAULT '0' ) COMMENT='๊ฒŒ์‹œํŒ';
SQL
๋ณต์‚ฌ

???.java

1.
@TableName("???")
2.
@TableId(type = IdType.AUTO)
/** * Board * - ๊ฒŒ์‹œ๊ธ€ ์ •๋ณด */ @Data @TableName("board") // ํ…Œ์ด๋ธ”๋ช… (๐Ÿฆ๏ธŽ๐Ÿ’™ Mybatis plus) @Alias("Board") // typealias ๋ณ„์นญ (๐Ÿฆ๏ธŽ๐Ÿ’— Mybatis) public class Board { @TableId(type = IdType.AUTO) // PK ์„ค์ • ๋ฐ ์ž๋™์ฆ๊ฐ€ (๐Ÿฆ๏ธŽ๐Ÿ’™ Mybatis plus - CRUD ์— ์‚ฌ์šฉ) private int no; private String id; private String title; private String writer; private String content; private Date createdAt; private Date updatedAt; private int views; public Board() { this.id = UUID.randomUUID().toString(); } }
Java
๋ณต์‚ฌ

???Mapper.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"> <!-- namespace="๋งคํผ ์ธํ„ฐํŽ˜์ด์Šค ๊ฒฝ๋กœ" --> <mapper namespace="com.aloha.mybatis_plus.mapper.BoardMapper"> <!-- โญ ๊ธฐ๋ณธ CRUD ์ด๋ฏธ ๊ตฌํ˜„๋˜์–ด ์žˆ์Œ --> <!-- โžก ServiceImpl<BoardMapper, Board> --> </mapper>
XML
๋ณต์‚ฌ

???Mapper.java

1.
@Mapper
@Mapper public interface BoardMapper extends BaseMapper<Board> { }
Java
๋ณต์‚ฌ

???Service.java

public interface BoardService { }
Java
๋ณต์‚ฌ

???SeviceImpl.java

1.
extends ServiceImpl<???Mapper, ???>
@Service public class BoardServiceImpl extends ServiceImpl<BoardMapper, Board> implements BoardService { @Autowired private BoardMapper boardMapper; }
Java
๋ณต์‚ฌ

???Controller.java

/** * /board ๊ฒฝ๋กœ๋กœ ์š”์ฒญ ์™”์„ ๋•Œ ์ฒ˜๋ฆฌ * [GET] - /board/list : ๊ฒŒ์‹œ๊ธ€ ๋ชฉ๋ก ํ™”๋ฉด * [GET] - /board/read : ๊ฒŒ์‹œ๊ธ€ ์กฐํšŒ ํ™”๋ฉด * [GET] - /board/insert : ๊ฒŒ์‹œ๊ธ€ ๋“ฑ๋ก ํ™”๋ฉด * [POST] - /board/insert : ๊ฒŒ์‹œ๊ธ€ ๋“ฑ๋ก ์ฒ˜๋ฆฌ * [GET] - /board/update : ๊ฒŒ์‹œ๊ธ€ ์ˆ˜์ • ํ™”๋ฉด * [POST] - /board/update : ๊ฒŒ์‹œ๊ธ€ ์ˆ˜์ • ์ฒ˜๋ฆฌ * [POST] - /board/delete : ๊ฒŒ์‹œ๊ธ€ ์‚ญ์ œ ์ฒ˜๋ฆฌ */ @Slf4j // ๋กœ๊ทธ ์–ด๋…ธํ…Œ์ด์…˜ @Controller // ์ปจํŠธ๋กค๋Ÿฌ ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ๋“ฑ๋ก @RequestMapping("/board") // ํด๋ ˆ์Šค ๋ ˆ๋ฒจ ์š”์ฒญ ๊ฒฝ๋กœ ๋งคํ•‘ // - /board/~ ๊ฒฝ๋กœ์˜ ์š”์ฒญ์€ ์ด ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ์ฒ˜๋ฆฌ public class BoardController { // โญ๋ฐ์ดํ„ฐ ์š”์ฒญ๊ณผ ํ™”๋ฉด ์ถœ๋ ฅ // Controller --> Service (๋ฐ์ดํ„ฐ ์š”์ฒญ) // Controller <-- Service (๋ฐ์ดํ„ฐ ์ „๋‹ฌ) // Controller --> Model (๋ชจ๋ธ ๋“ฑ๋ก) // View <-- Model (๋ฐ์ดํ„ฐ ์ถœ๋ ฅ) @Autowired // ์˜์กด์„ฑ ์ž๋™ ์ฃผ์ž… // private BoardService boardService; // @Service๋ฅผ --Impl ์— ๋“ฑ๋ก private BoardServiceImpl boardService; // @Service๋ฅผ --Impl ์— ๋“ฑ๋ก /** * ๊ฒŒ์‹œ๊ธ€ ๋ชฉ๋ก ์กฐํšŒ ํ™”๋ฉด * @return * @throws Exception */ @GetMapping("/list") public String list(Model model) throws Exception { // ๋ฐ์ดํ„ฐ ์š”์ฒญ List<Board> boardList = boardService.list(); // ๋ชจ๋ธ ๋“ฑ๋ก model.addAttribute("boardList", boardList); // ๋ทฐ ํŽ˜์ด์ง€ ์ง€์ • return "/board/list"; // resources/templates/board/list.html } /** * ๊ฒŒ์‹œ๊ธ€ ์กฐํšŒ ํ™”๋ฉด * - /board/read?no=๐Ÿ’Ž * @param no * @return * @throws Exception */ // @RequestParam("ํŒŒ๋ผ๋ฏธํ„ฐ๋ช…") // - ์Šคํ”„๋ง ๋ถ€ํŠธ 3.2๋ฒ„์ „ ์ดํ•˜, ์ƒ๋žตํ•ด๋„ ์ž๋™ ๋งคํ•‘๋œ๋‹ค. // - ์Šคํ”„๋ง ๋ถ€ํŠธ 3.2๋ฒ„์ „ ์ด์ƒ, ํ•„์ˆ˜๋กœ ๋ช…์‹œํ•ด์•ผ ๋งคํ•‘๋œ๋‹ค. @GetMapping("/read") public String read(@RequestParam("no") int no, Model model) throws Exception { // ๋ฐ์ดํ„ฐ ์š”์ฒญ // Board board = boardService.select(no); Board board = boardService.getById(no); // ๋ชจ๋ธ ๋“ฑ๋ก model.addAttribute("board", board); // ๋ทฐํŽ˜์ด์ง€ ์ง€์ • return "/board/read"; } /** * ๊ฒŒ์‹œ๊ธ€ ๋“ฑ๋ก ํ™”๋ฉด * @return */ @GetMapping("/insert") public String insert() { return "/board/insert"; } /** * ๊ฒŒ์‹œ๊ธ€ ๋“ฑ๋ก ์ฒ˜๋ฆฌ * @param board * @return * @throws Exception */ @PostMapping("/insert") public String insertPro(Board board) throws Exception { // ๋ฐ์ดํ„ฐ ์š”์ฒญ // int result = boardService.insert(board); boolean result = boardService.save(board); // ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ // โญ• ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ์„ฑ๊ณต // if( result > 0 ) { if( result ) { return "redirect:/board/list"; } // โŒ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ์‹คํŒจ return "redirect:/board/insert?error"; } /** * ๊ฒŒ์‹œ๊ธ€ ์ˆ˜์ • ํ™”๋ฉด * @param no * @param model * @return * @throws Exception */ @GetMapping("/update") public String update(@RequestParam("no") int no, Model model) throws Exception { // Board board = boardService.select(no); Board board = boardService.getById(no); model.addAttribute("board", board); return "/board/update"; } /** * ๊ฒŒ์‹œ๊ธ€ ์ˆ˜์ • ์ฒ˜๋ฆฌ * @param board * @return * @throws Exception */ @PostMapping("/update") public String updatePro(Board board) throws Exception { // int result = boardService.update(board); boolean result = boardService.updateById(board); // if( result > 0 ) { if( result ) { return "redirect:/board/list"; } int no = board.getNo(); return "redirect:/board/update?no="+ no + "&error"; } /** * ๊ฒŒ์‹œ๊ธ€ ์‚ญ์ œ ์ฒ˜๋ฆฌ * @param no * @return * @throws Exception */ @PostMapping("/delete") public String delete(@RequestParam("no") int no) throws Exception { // int result = boardService.delete(no); boolean result = boardService.removeById(no); // if( result > 0 ) { if( result ) { return "redirect:/board/list"; } return "redirect:/board/update?no=" + no + "&error"; } }
Java
๋ณต์‚ฌ

views

1.
???
a.
list.html
b.
insert.html
c.
read.html
d.
update.html
2.
index.html

???

1.
list.html
2.
insert.html
3.
read.html
4.
update.html

list.html

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>๊ฒŒ์‹œ๊ธ€ ๋ชฉ๋ก</title> </head> <body> <h1>๊ฒŒ์‹œ๊ธ€ ๋ชฉ๋ก</h1> <a href="/board/insert">๊ธ€์“ฐ๊ธฐ</a> <table border="1"> <tr> <th width="50">๋ฒˆํ˜ธ</th> <th width="300">์ œ๋ชฉ</th> <th width="100">์ž‘์„ฑ์ž</th> <th width="200">๋“ฑ๋ก์ผ์ž</th> <th width="200">์ˆ˜์ •์ผ์ž</th> <th widht="100">์กฐํšŒ์ˆ˜</th> </tr> <th:block th:if="${boardList == null or boardList.isEmpty()}"> <tr> <td colspan="6" align="center">์กฐํšŒ๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.</td> </tr> </th:block> <th:block th:each="board : ${boardList}"> <tr> <td align="center" th:text="${board.no}"></td> <td> <!-- th:???="|๋ฌธ์ž์—ด+${ํ‘œํ˜„์‹}|" --> <a th:href="|/board/read?no=${board.no}|" th:text="${board.title}"></a> </td> <td align="center" th:text="${board.writer}"></td> <!-- ์œ ํ‹ธ๋ฆฌํ‹ฐ๊ฐ์ฒด.๋ฉ”์†Œ๋“œ() #dates.format( ๋‚ ์งœ๊ฐ์ฒด๋ช…, '๋‚ ์งœํฌ๋งท' ) - #dates.format( board.regDate, 'yyyy-MM-dd HH:mm:ss' ) --> <td align="center"> <span th:text="${ #dates.format( board.createdAt, 'yyyy-MM-dd HH:mm:ss' ) }"></span> </td> <td align="center"> <span th:text="${ #dates.format( board.updatedAt, 'yyyy-MM-dd HH:mm:ss' ) }"></span> </td> <td align="center" th:text="${board.views}"></td> </tr> </th:block> </table> </body> </html>
HTML
๋ณต์‚ฌ

insert.html

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>๊ฒŒ์‹œ๊ธ€ ๋“ฑ๋ก</title> </head> <body> <h1>๊ฒŒ์‹œ๊ธ€ ๋“ฑ๋ก</h1> <form action="/board/insert" method="post"> <input type="text" name="title" id="title"> <input type="text" name="writer" id="writer"> <input type="text" name="content" id="content"> <input type="submit" value="๋“ฑ๋ก"> </form> </body> </html>
HTML
๋ณต์‚ฌ

read.html

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>๊ฒŒ์‹œ๊ธ€ ์กฐํšŒ</title> </head> <body> <h1>๊ฒŒ์‹œ๊ธ€ ์กฐํšŒ</h1> <form action="/board/delete" method="post"> <input type="hidden" name="no" th:value="${board.no}"> <table> <tr> <td>์ œ๋ชฉ</td> <td> <input type="text" name="title" th:value="${board.title}"> </td> </tr> <tr> <td>์ž‘์„ฑ์ž</td> <td> <input type="text" name="writer" th:value="${board.writer}"> </td> </tr> <tr> <td colspan="2"> <textarea name="content" id="content" cols="40" rows="5" th:text="${board.content}"></textarea> </td> </tr> </table> <div> <button type="button" onclick="moveUpdate()">์ˆ˜์ •</button> <button type="button" onclick="moveList()">๋ชฉ๋ก</button> </div> </form> <script> // ๐Ÿ‘ฉโ€๐Ÿ’ป ๋ชจ๋ธ ๊ฐ์ฒด๋ฅผ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ• let no = "[[${board.no}]]" // ์ˆ˜์ • ํ™”๋ฉด ์ด๋™ function moveUpdate() { location.href = '/board/update?no=' + no } // ๋ชฉ๋ก ํ™”๋ฉด ์ด๋™ function moveList() { location.href = '/board/list' } </script> </body> </html>
HTML
๋ณต์‚ฌ

update.html

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>๊ฒŒ์‹œ๊ธ€ ์ˆ˜์ •</title> </head> <body> <h1>๊ฒŒ์‹œ๊ธ€ ์ˆ˜์ •</h1> <form id="form" action="/board/update" method="post"> <input type="hidden" name="no" th:value="${board.no}"> <table> <tr> <td>์ œ๋ชฉ</td> <td> <input type="text" name="title" th:value="${board.title}"> </td> </tr> <tr> <td>์ž‘์„ฑ์ž</td> <td> <input type="text" name="writer" th:value="${board.writer}"> </td> </tr> <tr> <td colspan="2"> <textarea name="content" id="content" cols="40" rows="5" th:text="${board.content}"></textarea> </td> </tr> </table> <div> <button type="submit">์ˆ˜์ •</button> <button type="button" onclick="actionDelete()">์‚ญ์ œ</button> <button type="button" onclick="moveList()">๋ชฉ๋ก</button> </div> </form> <script> let form = document.getElementById('form') // ๐Ÿ‘ฉโ€๐Ÿ’ป ๋ชจ๋ธ ๊ฐ์ฒด๋ฅผ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ• let no = "[[${board.no}]]" // ์ˆ˜์ • ํ™”๋ฉด ์ด๋™ function moveUpdate() { location.href = '/board/update?no=' + no } // ์‚ญ์ œ ์š”์ฒญ function actionDelete() { let check = confirm('์ •๋ง๋กœ ์‚ญ์ œํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?') if( check ) { form.action = '/board/delete' form.submit() } } // ๋ชฉ๋ก ํ™”๋ฉด ์ด๋™ function moveList() { location.href = '/board/list' } </script> </body> </html>
HTML
๋ณต์‚ฌ

index.html

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>๋ฉ”์ธ ํ™”๋ฉด</title> </head> <body> <h1>SpringBoot x MyBatis</h1> <h3>๊ฒŒ์‹œํŒ ํ”„๋กœ์ ํŠธ</h3> <ul> <li>Spring Boot 3.x</li> <li>Thymeleaf</li> <li>MySQL 8.x</li> <li>MyBatis 3.0</li> </ul> <div> <a href="/board/list">๊ฒŒ์‹œํŒ</a> </div> </body> </html>
HTML
๋ณต์‚ฌ

ํŽ˜์ด์ง• ์ฒ˜๋ฆฌ

์กฐ๊ฑด ๋นŒ๋”