κ²μν - CRUD
Preview
β’
BackEnd
β¦
Spring Boot
β¦
CRUD μμ
βͺ
board ν
μ΄λΈ μμ±
βͺ
Board.java DTO μμ±
βͺ
BoardMapper.xml
βͺ
BoardMapper.java
βͺ
BoardService.java
βͺ
BoardServiceImpl.java
βͺ
BoardController.java
β’
FrontEnd
β¦
React
β¦
apis
βͺ
boards.js
β¦
components
βͺ
board
β’
List
β’
Read
β’
InsertForm
β’
UpdateForm
β¦
containers
βͺ
κ²μκΈ λͺ©λ‘ - ListContainer
βͺ
κ²μκΈ λ±λ‘ - InsertContainer
βͺ
κ²μκΈ μ‘°ν - ReadContainer
βͺ
κ²μκΈ μμ - UpdateContainer
β¦
pages
βͺ
board
β’
List
β’
Read
β’
Insert
β’
Update
βͺ
Home
axios λΌμ΄λΈλ¬λ¦¬
axios λΌμ΄λΈλ¬λ¦¬ μ€μΉ
npm install axios
Bash
볡μ¬
axios μ¬μ© λ°©λ²
β¦
import
β¦
GET
β¦
POST
β¦
PUT
β¦
DELETE
import
import axios from 'axios';
JavaScript
볡μ¬
GET
axios.get("κ²½λ‘")
JavaScript
볡μ¬
POST
axios.post("κ²½λ‘", {λ°μ΄ν°})
JavaScript
볡μ¬
PUT
axios.put("κ²½λ‘", {λ°μ΄ν°})
JavaScript
볡μ¬
DELETE
axios.delete("κ²½λ‘")
JavaScript
볡μ¬
β’
BackEnd
β¦
Spring Boot
β¦
CRUD μμ
βͺ
board ν
μ΄λΈ μμ±
βͺ
Board.java DTO μμ±
βͺ
BoardMapper.xml
βͺ
BoardMapper.java
βͺ
BoardService.java
βͺ
BoardServiceImpl.java
βͺ
BoardController.java
β’
FrontEnd
β¦
React
β¦
apis
βͺ
boards.js
β¦
components
βͺ
board
β’
List
β’
Read
β’
InsertForm
β’
UpdateForm
β¦
containers
βͺ
κ²μκΈ λͺ©λ‘ - ListContainer
βͺ
κ²μκΈ λ±λ‘ - InsertContainer
βͺ
κ²μκΈ μ‘°ν - ReadContainer
βͺ
κ²μκΈ μμ - UpdateContainer
β¦
pages
βͺ
board
β’
List
β’
Read
β’
Insert
β’
Update
βͺ
Home
apis
boards.js
// π» npm install axios
import axios from 'axios';
// λͺ©λ‘
export const list = () => axios.get("/boards")
// μ‘°ν
export const select = (no) => axios.get(`/boards/${no}`)
// λ±λ‘
export const insert = (title, writer, content) => axios.post("/boards", {title, writer, content})
// μμ
export const update = (no, title, writer, content) => axios.put("/boards", {no,title,writer,content})
// μμ
export const remove = (no) => axios.delete(`/boards/${no}`)
JavaScript
볡μ¬
components
β’
board
β¦
List
β¦
Read
β¦
InsertForm
β¦
UpdateForm
List
import React from 'react'
import {Link} from 'react-router-dom'
const List = ({ boardList }) => {
console.log(boardList);
return (
<div className='container'>
<h1>κ²μκΈ λͺ©λ‘</h1>
<Link to="/boards/insert">κΈμ°κΈ°</Link>
<table border={1}>
<thead>
<tr>
<th>λ²νΈ</th>
<th>μ λͺ©</th>
<th>μμ±μ</th>
<th>λ±λ‘μΌμ</th>
</tr>
</thead>
<tbody>
{boardList.map((board) =>
(
<tr key={board.no}>
<td>{board.no}</td>
<td>
<Link to={`/boards/${board.no}`}>
{board.title}
</Link>
</td>
<td>{board.writer}</td>
<td>{board.regDate}</td>
</tr>
)
)}
</tbody>
</table>
</div>
)
}
export default List
JavaScript
볡μ¬
Read
import React from 'react'
import { Link } from 'react-router-dom'
const Read = ({ no, board }) => {
return (
<div className='container'>
<h1 className="title">κ²μκΈ μ‘°ν</h1>
<h3>λ²νΈ : {no}</h3>
<hr />
<table>
<tbody>
<tr>
<td>λ²νΈ</td>
<td>
<input type="text" value={no} readOnly />
</td>
</tr>
<tr>
<td>λ±λ‘μΌμ</td>
<td>
<input type="text" value={board.regDate} readOnly />
</td>
</tr>
<tr>
<td>μ λͺ©</td>
<td>
<input type="text" value={board.title} readOnly />
</td>
</tr>
<tr>
<td>μμ±μ</td>
<td>
<input type="text" value={board.writer} readOnly />
</td>
</tr>
<tr>
<td colSpan={2}>λ΄μ©</td>
</tr>
<tr>
<td colSpan={2}>
<textarea cols="40" rows="10" value={board.content} readOnly></textarea>
</td>
</tr>
</tbody>
</table>
<hr />
<div className="btn-box">
<Link to="/boards" className='btn'>λͺ©λ‘</Link>
<Link to={`/boards/update/${no}`} className='btn'>μμ </Link>
</div>
</div>
)
}
export default Read
JavaScript
볡μ¬
InsertForm
import React, { useState } from 'react'
import { Link } from 'react-router-dom'
const InsertForm = ({ onInsert }) => {
// π§ state
const [title, setTitle] = useState('')
const [writer, setWriter] = useState('')
const [content, setContent] = useState('')
// π ν¨μ
const handleChangeTitle = (e) => {
setTitle(e.target.value)
}
const handleChangeWriter = (e) => {
setWriter(e.target.value)
}
const handleChangeContent = (e) => {
setContent(e.target.value)
}
const onSubmit = () => {
// μ ν¨μ± κ²μ¬ β
// ...
onInsert(title, writer, content)
}
return (
<div className='container'>
<h1 className='title'>κ²μκΈ λ±λ‘</h1>
<table>
<tbody>
<tr>
<td>μ λͺ©</td>
<td>
<input type="text"
value={title}
onChange={handleChangeTitle} />
</td>
</tr>
<tr>
<td>μμ±μ</td>
<td>
<input type="text"
value={writer}
onChange={handleChangeWriter} />
</td>
</tr>
<tr>
<td colSpan={2}>λ΄μ©</td>
</tr>
<tr>
<td colSpan={2}>
<textarea cols="40" rows="10"
value={content}
onChange={handleChangeContent}></textarea>
</td>
</tr>
</tbody>
</table>
<div className="btn-box">
<Link to="/boards" className='btn'>λͺ©λ‘</Link>
<button className='btn' onClick={ onSubmit }>λ±λ‘</button>
</div>
</div>
)
}
export default InsertForm
JavaScript
볡μ¬
UpdateForm
import React, { useEffect } from 'react'
import { useState } from 'react'
import { Link } from 'react-router-dom'
const UpdateForm = ({ no, board, onUpdate, onDelete }) => {
// π§ state
const [title, setTitle] = useState('')
const [writer, setWriter] = useState('')
const [content, setContent] = useState('')
// π ν¨μ
const handleChangeTitle = (e) => {
setTitle(e.target.value)
}
const handleChangeWriter = (e) => {
setWriter(e.target.value)
}
const handleChangeContent = (e) => {
setContent(e.target.value)
}
const onSubmit = () => {
onUpdate(no, title, writer, content)
}
const handleDelete = () => {
const check = window.confirm("μ λ§λ‘ μμ νμκ² μ΅λκΉ?")
if( check ) {
onDelete(no)
}
}
useEffect( () => {
if( board ) {
setTitle(board.title)
setWriter(board.writer)
setContent(board.content)
}
}, [board])
// [μμ‘΄νλ κ°μ²΄] (βμμ‘΄μ± λ°°μ΄)
// : μ§μ ν κ°μ²΄κ° λ³ννμλ, λ€μ useEffect λ₯Ό μ€ννλ€.
return (
<div className='container'>
<h1 className='title'>κ²μκΈ μμ </h1>
<table>
<tbody>
<tr>
<td>λ²νΈ</td>
<td>
<input type="text" value={no} readOnly />
</td>
</tr>
<tr>
<td>μ λͺ©</td>
<td>
<input type="text"
value={title}
onChange={handleChangeTitle} />
</td>
</tr>
<tr>
<td>μμ±μ</td>
<td>
<input type="text"
value={writer}
onChange={handleChangeWriter} />
</td>
</tr>
<tr>
<td colSpan={2}>λ΄μ©</td>
</tr>
<tr>
<td colSpan={2}>
<textarea cols="40" rows="10"
value={content}
onChange={handleChangeContent}></textarea>
</td>
</tr>
</tbody>
</table>
<div className="btn-box">
<Link to="/boards" className='btn'>λͺ©λ‘</Link>
<button className='btn' onClick={handleDelete}>μμ </button>
<button className='btn' onClick={onSubmit}>μμ </button>
</div>
</div>
)
}
export default UpdateForm
JavaScript
볡μ¬
containers
β’
κ²μκΈ λͺ©λ‘ - ListContainer
β’
κ²μκΈ λ±λ‘ - InsertContainer
β’
κ²μκΈ μ‘°ν - ReadContainer
β’
κ²μκΈ μμ - UpdateContainer
κ²μκΈ λͺ©λ‘ - ListContainer
import React, { useEffect, useState } from 'react'
import List from '../components/board/List'
import * as boards from '../apis/boards'
const ListContainer = () => {
// π§ state
const [boardList, setBoardList] = useState([])
// π ν¨μ
const getBoardList = async () => {
const response = await boards.list()
const data = await response.data // βboardList
setBoardList(data)
}
// β hook
useEffect( ()=> {
getBoardList()
}, [])
return (
<>
{/* κ²μκΈ λͺ©λ‘ */}
<List boardList={boardList} />
</>
)
}
export default ListContainer
JavaScript
볡μ¬
κ²μκΈ λ±λ‘ - InsertContainer
import React from 'react'
import InsertForm from '../components/board/InsertForm'
import * as boards from '../apis/boards'
import { useNavigate } from 'react-router-dom'
const InsertContainer = () => {
const navigate = useNavigate()
// π§ state
// π ν¨μ
const onInsert = async (title, writer, content) => {
try {
const response = await boards.insert(title, writer, content)
const status = await response.status
console.log(`κ²μκΈ λ±λ‘ μμ² κ²°κ³Ό : ${status}`);
alert("κ²μκΈ λ±λ‘ μλ£!")
// β‘ κ²μκΈ λͺ©λ‘μΌλ‘ μ΄λ
navigate("/boards")
} catch (error) {
console.log(error);
}
}
return (
<>
<InsertForm onInsert={onInsert} />
</>
)
}
export default InsertContainer
JavaScript
볡μ¬
κ²μκΈ μ‘°ν - ReadContainer
import React, { useEffect } from 'react'
import Read from '../components/board/Read'
import { useState } from 'react'
import * as boards from '../apis/boards'
const ReadContainer = ({ no }) => {
// π§ state
const [board, setBoard] = useState({})
// π ν¨μ
const getBoard = async () => {
const response = await boards.select(no)
const data = await response.data // β board
console.log(data);
setBoard(data)
}
// β hook
useEffect( () => {
getBoard()
}, [])
return (
<>
<Read no={no} board={board} />
</>
)
}
export default ReadContainer
JavaScript
볡μ¬
κ²μκΈ μμ - UpdateContainer
import React, { useEffect, useState } from 'react'
import UpdateForm from '../components/board/UpdateForm'
import * as boards from '../apis/boards'
import { useNavigate } from 'react-router-dom'
const UpdateContainer = ({ no }) => {
// π§ state
const [board, setBoard] = useState({})
// π ν¨μ
const navigate = useNavigate()
const getBoard = async () => {
const response = await boards.select(no)
const data = await response.data // β board
console.log(data);
setBoard(data)
}
const onUpdate = async (no, title, writer, content) => {
try {
const response = await boards.update(no, title, writer, content)
const status = await response.status
console.log(`κ²μκΈ μμ μμ² κ²°κ³Ό : ${status}`);
alert("κ²μκΈ μμ μλ£!")
// β‘ κ²μκΈ λͺ©λ‘μΌλ‘ μ΄λ
navigate("/boards")
} catch (error) {
console.log(error);
}
}
const onDelete = async (no) => {
const response = await boards.remove(no)
const status = await response.status
console.log(`κ²μκΈ μμ μμ² κ²°κ³Ό : ${status}`);
alert("μμ μλ£!")
// β‘ κ²μκΈ λͺ©λ‘μΌλ‘ μ΄λ
navigate("/boards")
}
// β hook
useEffect( () => {
getBoard()
}, [])
return (
<>
<UpdateForm no={no}
board={board}
onUpdate={onUpdate}
onDelete={onDelete} />
</>
)
}
export default UpdateContainer
JavaScript
볡μ¬
pages
β’
board
β¦
List
β¦
Read
β¦
Insert
β¦
Update
β’
Home
board
List
import React from 'react'
import ListContainer from '../../containers/ListContainer'
const List = () => {
return (
<>
{/* Header */}
<ListContainer />
{/* Footer */}
</>
)
}
export default List
JavaScript
볡μ¬
Read
import React from 'react'
import ReadContainer from '../../containers/ReadContainer'
import { useParams } from 'react-router-dom'
const Read = () => {
// πβ νλΌλ―Έν° κ°μ Έμ€κΈ°
const { no } = useParams()
console.log(`no : ${no}`);
return (
<>
{/* Header */}
<ReadContainer no={no} />
{/* Footer */}
</>
)
}
export default Read
JavaScript
볡μ¬
Insert
import React from 'react'
import InsertContainer from '../../containers/InsertContainer'
const Insert = () => {
return (
<>
{/* Header */}
<InsertContainer/>
{/* Footer */}
</>
)
}
export default Insert
JavaScript
볡μ¬
Update
import React from 'react'
import UpdateContainer from '../../containers/UpdateContainer'
import { useParams } from 'react-router-dom'
const Update = () => {
const { no } = useParams()
return (
<>
{/* Header */}
<UpdateContainer no={no} />
{/* Footer */}
</>
)
}
export default Update
JavaScript
볡μ¬
Home
import React from 'react'
import {Link} from 'react-router-dom'
const Home = () => {
return (
<div className='container'>
<h1 className='title'>λ©μΈ νλ©΄</h1>
<h3>κ²μν νλ‘μ νΈ</h3>
<Link to="/boards">κ²μν</Link>
</div>
)
}
export default Home
JavaScript
볡μ¬