ParansaikStudy / LDK-SpringBase

스프링 부트 기초 실습소
0 stars 0 forks source link

26-u-d-implementation #28

Closed dbzoseh2rl closed 9 months ago

dbzoseh2rl commented 9 months ago

개요

코드 부분

}

- BoardServiece를 아래와 같이 작성하였습니다.

@Service @RequiredArgsConstructor public class BoardService {

private final BoardMapper boardMapper;

public List<BoardEntity> getAllBoards() {
    return boardMapper.getAllBoards();
}

public BoardEntity getBoardById(int id) {
    return boardMapper.getBoardById(id);
}

public BoardEntity createBoard(BoardEntity board) {
    try {
        boardMapper.createBoard(board);
        return board;
    } catch (Exception e) {
        // 실패 시 예외 처리
        throw new RuntimeException("보드 생성에 실패하였습니다.", e);
    }
}
public boolean updateBoard(int id, BoardEntity board) {
    try {
        BoardEntity existingBoard = boardMapper.getBoardById(id);
        if (existingBoard != null) {
            board.setId(id);
            boardMapper.updateBoard(board);
            return true;
        } else {
            // Board not found, update failed
            return false;
        }
    } catch (Exception e) {
        // 실패 시 예외 처리
        throw new RuntimeException("보드 업데이트에 실패하였습니다.", e);
    }
}
public boolean deleteBoard(int id) {
    try {
        BoardEntity existingBoard = boardMapper.getBoardById(id);
        if (existingBoard != null) {
            boardMapper.deleteBoard(id);
            return true;
        } else {
            // Board not found, delete failed
            return false;
        }
    } catch (Exception e) {
        // 실패 시 예외 처리
        throw new RuntimeException("보드 삭제에 실패하였습니다.", e);
    }
}

}

- 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">

INSERT INTO boards(id, user_Name, title, content, nowDay, view_Count) VALUES (#{id}, #{userName}, #{title}, #{content}, #{nowDay}, #{viewCount}); UPDATE Boards SET user_Name = #{userName}, title = #{title}, content = #{content}, nowDay = #{nowDay}, view_Count = #{viewCount} WHERE id = #{id} DELETE FROM Boards WHERE id = #{id}
- application.yml을 아래와 같이 작성하였습니다.

server: port: 8084

spring: datasource: url: jdbc:mysql://127.0.0.1:3306/Board?allowPublicKeyRetrieval=true&useSSL=false username: root password: dbzoseh2rl driver-class-name: com.mysql.cj.jdbc.Driver

mybatis: type-aliases-package: com.ldkspringbase.entity mapper-locations: classpath:mapper/*.xml configuration: map-underscore-to-camel-case: 'true'



## u 구현 사진
![image](https://github.com/ParansaikStudy/LDK-SpringBase/assets/91397068/121faefe-9e30-4356-b2eb-390f267e6c41)
![image](https://github.com/ParansaikStudy/LDK-SpringBase/assets/91397068/af711ffb-e7f9-41ee-9124-327ba0cc6075)

## d 구현 사진
![image](https://github.com/ParansaikStudy/LDK-SpringBase/assets/91397068/3fadd9b4-4b10-4f51-9f2d-9b998263ba26)
![image](https://github.com/ParansaikStudy/LDK-SpringBase/assets/91397068/7c4d6207-0b0e-4a9b-bc96-390c32e351f0)