Search

κ²Œμ‹œνŒ - CRUD

κ²Œμ‹œνŒ - 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
볡사