BoardDAO
package com.kim.app;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
public class BoardDAO{
@Autowired
private JdbcTemplate jdbcTemplate;
private final String SELECTALL="SELECT * FROM BOARD";
private final String SELECTONE="SELECT * FROM BOARD WHERE BID=?";
private final String INSERT="INSERT INTO BOARD (BID,CONTENT,MID) VALUES(?,?,?)";
private final String UPDATE="UPDATE BOARD SET CONTENT=? WHERE BID=?";
private final String DELETE="DELETE FROM BOARD WHERE BID=?";
public List<BoardDTO> selectAll(BoardDTO bDTO) {
System.out.println("Board selectAll 로그");
return jdbcTemplate.query(SELECTALL, new BeanPropertyRowMapper<BoardDTO>(BoardDTO.class));
}
public BoardDTO selectOne(BoardDTO bDTO) {
System.out.println("Board selectOne 로그");
Object[] args = { bDTO.getBid() };
try {
return jdbcTemplate.queryForObject(SELECTONE,args ,new BeanPropertyRowMapper<BoardDTO>(BoardDTO.class));
}
catch(Exception e) {
return null;
}
}
public boolean insert(BoardDTO bDTO) {
System.out.println("Board insert 로그");
int rs = jdbcTemplate.update(INSERT,bDTO.getBid(),bDTO.getContent(),bDTO.getMid());
if(rs<=0) {
return false;
}
return true;
}
public boolean update(BoardDTO bDTO) {
System.out.println("Board update 로그");
int rs = jdbcTemplate.update(UPDATE,bDTO.getContent(),bDTO.getBid());
if(rs<=0) {
return false;
}
return true;
}
public boolean delete(BoardDTO bDTO) {
System.out.println("Board delete 로그");
int rs = jdbcTemplate.update(DELETE,bDTO.getBid());
if(rs<=0) {
return false;
}
return true;
}
}
BoardDTO
package com.kim.app;
import lombok.Data;
@Data
public class BoardDTO {
private int bid;
private String mid;
private String content;
}
BoardService
package com.kim.app;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class BoardService implements InterfaceBoardService {
@Autowired
private InterfaceBoardDAO bDAO;
@Override
public List<BoardDTO> selectAll(BoardDTO bDTO) {
return bDAO.selectAll(bDTO);
}
@Override
public BoardDTO selectOne(BoardDTO bDTO) {
return bDAO.selectOne(bDTO.getBid());
}
@Override
public boolean insert(BoardDTO bDTO) {
Map<String, String> map = new HashMap<String, String>();
map.put("BID", Integer.toString(bDTO.getBid()));
map.put("CONTENT", bDTO.getContent());
map.put("MID", bDTO.getMid());
return bDAO.insert(map);
}
@Override
public boolean update(BoardDTO bDTO) {
Map<String, String> map = new HashMap<String, String>();
map.put("CONTENT", bDTO.getContent());
map.put("BID", Integer.toString(bDTO.getBid()));
return bDAO.update(map);
}
@Override
public boolean delete(BoardDTO bDTO) {
return bDAO.delete(bDTO.getBid());
}
}
InterfaceBoardDAO
package com.kim.app;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface InterfaceBoardDAO {
public List<BoardDTO> selectAll(BoardDTO bDTO);
public BoardDTO selectOne(int bid);
public boolean insert(Map<String, String> map);
public boolean update(Map<String, String> map);
public boolean delete(@Param("BID")int bid);
}
InterfaceBoardService
package com.kim.app;
import java.util.List;
public interface InterfaceBoardService {
List<BoardDTO> selectAll(BoardDTO bDTO);
BoardDTO selectOne(BoardDTO bDTO);
boolean insert(BoardDTO bDTO);
boolean update(BoardDTO bDTO);
boolean delete(BoardDTO bDTO);
}
MemberDAO
package com.kim.app;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
public class MemberDAO {
@Autowired
private JdbcTemplate jdbcTemplate;
private final String SELECTALL="SELECT * FROM MEMBER";
private final String SELECTONE="SELECT * FROM MEMBER WHERE MID=? AND MPW=?";
private final String SELECTONE_DUPLICATE = "SELECT * FROM MEMBER WHERE MID = ?";
private final String INSERT="INSERT INTO MEMBER (MID, MPW) VALUES (?, ?)";
private final String UPDATE="UPDATE MEMBER SET MPW=? WHERE MID=?";
private final String DELETE="DELETE FROM MEMBER WHERE MID=?";
public List<MemberDTO> selectAll(MemberDTO mDTO) {
System.out.println("Member selectAll 로그");
return jdbcTemplate.query(SELECTALL, new BeanPropertyRowMapper<MemberDTO>(MemberDTO.class));
}
public MemberDTO selectOne(MemberDTO mDTO) {
System.out.println("Member selectOne 로그");
try {
if(mDTO.getSc()==null) {
Object[] args = { mDTO.getMid(), mDTO.getMpw() };
return jdbcTemplate.queryForObject(SELECTONE, new BeanPropertyRowMapper<MemberDTO>(MemberDTO.class), args);
}
else if(mDTO.getSc().equals("duplicate")) {
Object[] args = { mDTO.getMid() };
return jdbcTemplate.queryForObject(SELECTONE_DUPLICATE, new BeanPropertyRowMapper<MemberDTO>(MemberDTO.class), args);
}
} catch(EmptyResultDataAccessException e) {
return null;
}
return null;
}
public boolean insert(MemberDTO mDTO) {
System.out.println("Member insert 로그");
int rs = jdbcTemplate.update(INSERT, mDTO.getMid(), mDTO.getMpw());
if(rs<=0) {
return false;
}
return true;
}
public boolean update(MemberDTO mDTO) {
System.out.println("Member update 로그");
int rs = jdbcTemplate.update(UPDATE, mDTO.getMpw(), mDTO.getMid());
if(rs<=0) {
return false;
}
return true;
}
public boolean delete(MemberDTO mDTO) {
System.out.println("Member delete 로그");
int rs = jdbcTemplate.update(DELETE, mDTO.getMid());
if(rs<=0) {
return false;
}
return true;
}
}
MemberDTO
package com.kim.app;
import lombok.Data;
@Data
public class MemberDTO {
private String mid;
private String mpw;
private String sc;
}
MemberService
package com.kim.app;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class MemberService implements InterfaceMemberService {
@Autowired
private InterfaceMemberDAO mDAO;
@Override
public List<MemberDTO> selectAll(MemberDTO mDTO) {
return mDAO.selectAll(mDTO);
}
@Override
public MemberDTO selectOne(MemberDTO mDTO) {
Map<String, String> map = new HashMap<String, String>();
map.put("MID", mDTO.getMid());
map.put("MPW", mDTO.getMpw());
map.put("sc", mDTO.getSc());
return mDAO.selectOne(map);
}
@Override
public boolean insert(MemberDTO mDTO) {
Map<String, String> map = new HashMap<String, String>();
map.put("MID", mDTO.getMid());
map.put("MPW", mDTO.getMpw());
return mDAO.insert(map);
}
@Override
public boolean update(MemberDTO mDTO) {
Map<String, String> map = new HashMap<String, String>();
map.put("MID", mDTO.getMid());
map.put("MPW", mDTO.getMpw());
return mDAO.update(map);
}
@Override
public boolean delete(MemberDTO mDTO) {
return mDAO.delete(mDTO.getMid());
}
}
InterfaceMemberDAO
package com.kim.app;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface InterfaceMemberDAO {
public List<MemberDTO> selectAll(MemberDTO mDTO);
public MemberDTO selectOne(Map<String, String> map);
public boolean insert(Map<String, String> map);
public boolean update(Map<String, String> map);
public boolean delete(@Param("MID")String mid);
}
InterfaceMemberService
package com.kim.app;
import java.util.List;
public interface InterfaceMemberService {
public List<MemberDTO> selectAll(MemberDTO mDTO);
public MemberDTO selectOne(MemberDTO mDTO);
public boolean insert(MemberDTO mDTO);
public boolean update(MemberDTO mDTO);
public boolean delete(MemberDTO mDTO);
}
Controller
package com.kim.app;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import jakarta.servlet.http.HttpSession;
@Controller
public class CTRL {
@Autowired
private InterfaceBoardService boardService;
@Autowired
private InterfaceMemberService memberService;
@RequestMapping("/")
public String root() {
return "redirect:/main";
}
@RequestMapping("/main")
public String main(BoardDTO bDTO, Model model) {
System.out.println("main로그");
model.addAttribute("datas", boardService.selectAll(bDTO));
return "main";
}
@RequestMapping("/login")
public String login(MemberDTO mDTO, Model model, HttpSession session) {
System.out.println("login로그");
System.out.println("mDTO로그 : "+ mDTO);
mDTO = memberService.selectOne(mDTO);
if(mDTO != null) {
session.setAttribute("member", mDTO.getMid());
}
else {
model.addAttribute("message", "incorrect");
return "error";
}
return "redirect:/main";
}
@RequestMapping("/logout")
public String logout(HttpSession session) {
System.out.println("logout로그");
session.removeAttribute("member");
return "redirect:/main";
}
@RequestMapping(value = "/signupPage", method = RequestMethod.GET)
public String signupPage() {
System.out.println("signupPage로그");
return "join";
}
@RequestMapping(value = "/signup", method = RequestMethod.POST)
public String signup(MemberDTO mDTO, Model model) {
System.out.println("signup로그");
System.out.println("mDTO로그 : " + mDTO);
mDTO.setSc("duplicate");
MemberDTO mdata = memberService.selectOne(mDTO);
if (mdata == null) {
memberService.insert(mDTO);
System.out.println("singup로그 : 회원가입성공");
model.addAttribute("message", "joined");
return "success";
} else {
System.out.println("singup로그 : 중복된아이디");
model.addAttribute("message", "duplicate");
return "error";
}
}
@RequestMapping("/check")
public String checkPage() {
System.out.println("check로그");
return "check";
}
@RequestMapping("/mypage")
public String mypage( MemberDTO mDTO, Model model, HttpSession session) {
System.out.println("mypage로그");
System.out.println("mDTO로그 : "+ mDTO);
mDTO.setMid((String)session.getAttribute("member"));
mDTO = memberService.selectOne(mDTO);
if(mDTO == null) {
model.addAttribute("message", "incorrectPw");
return "error";
}
model.addAttribute("data", mDTO);
return "mypage";
}
@RequestMapping("/updateMember")
public String updateMember( MemberDTO mDTO, HttpSession session, Model model) {
System.out.println("updateMember로그");
mDTO.setMid((String)session.getAttribute("member"));
System.out.println("mDTO로그 : "+ mDTO);
if(memberService.update(mDTO)) {
System.out.println("updateMember로그 : 회원정보변경 성공");
session.removeAttribute("member");
model.addAttribute("message","changedMember");
return "success";
}
else {
System.out.println("updateMember로그 : 회원정보변경 실패");
model.addAttribute("message", "memberChangeFail");
return "error";
}
}
@RequestMapping("/deleteMember")
public String deleteMember( MemberDTO mDTO, HttpSession session, Model model) {
System.out.println("deleteMember로그");
mDTO.setMid((String)session.getAttribute("member"));
if(memberService.delete(mDTO)) {
System.out.println("deleteMember로그 : 회원탈퇴성공");
session.removeAttribute("member");
return "redirect:/main";
}
else {
System.out.println("deleteMember로그 : 회원탈퇴실패");
model.addAttribute("message","deleteMemberFail");
return "error";
}
}
@RequestMapping("/board")
public String selectBoard(BoardDTO bDTO, Model model) {
System.out.println("board로그");
model.addAttribute("data",boardService.selectOne(bDTO));
return "board";
}
@RequestMapping(value = "/insertBoard")
public String insertBoard(BoardDTO bDTO,HttpSession session, Model model) {
System.out.println("bDTO 로그 : " +bDTO);
bDTO.setMid((String)session.getAttribute("member"));
if(boardService.insert(bDTO)) {
System.out.println("insertBoard로그: 게시글 작성 성공");
return "redirect:/main";
}
else {
System.out.println("insertBoard로그: 게시글 작성 실패");
model.addAttribute("message","postBoardFail");
return "error";
}
}
@RequestMapping("/updateBoard")
public String updateBoard(BoardDTO bDTO, Model model) {
if (boardService.update(bDTO)) {
System.out.println("updateBoard로그: 게시글 변경 성공");
model.addAttribute("message","changedBoard");
return "success";
}
else {
System.out.println("updateBoard로그: 게시글 변경 실패");
model.addAttribute("message","boardChangeFail");
return "error";
}
}
@RequestMapping("/deleteBoard")
public String deleteBoard(BoardDTO bDTO, Model model) {
System.out.println("deleteBoard로그");
if(boardService.delete(bDTO)) {
System.out.println("deleteBoard로그 : 게시글 삭제 성공");
return "redirect:/main";
}
else {
System.out.println("deleteBoard로그 : 게시글 삭제 실패");
model.addAttribute("message","deleteBoardFail");
return "error";
}
}
}