Spring/Spring framework

Command 사용 Board

원2 2021. 8. 3. 16:23
728x90
반응형

프로젝트 구성


라이브러리에 mybatis추가 안해도됨

Controller

package com.bit.springBoard.controller;

import javax.servlet.http.HttpServletRequest;

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 com.bit.springBoard.command.BoardCommand;
import com.bit.springBoard.command.BoardContentViewCommand;
import com.bit.springBoard.command.BoardDeleteCommand;
import com.bit.springBoard.command.BoardListCommand;
import com.bit.springBoard.command.BoardModifyCommand;
import com.bit.springBoard.command.BoardWriteCommand;

@Controller
public class BoardController {
	
	BoardCommand command;

	@RequestMapping(value = "/writeView")
	public String writeView() {
		
		return "writeView";
	}
	
	@RequestMapping(value = "/write", method = RequestMethod.POST)
	public String write(HttpServletRequest req, Model model) {
		model.addAttribute("request", req);
		command = new BoardWriteCommand();
		command.execute(model);
		return "redirect:list";
	}

//	list
	@RequestMapping(value = "/list")
	public String list (Model model) {
		command = new BoardListCommand();
		command.execute(model);
		return "BoardList";
	}
	
//	delete
	@RequestMapping(value = "/delete")
	public String delete (HttpServletRequest req, Model model) {
		model.addAttribute("request", req);
		command = new BoardDeleteCommand();
		command.execute(model);
		return "redirect:list";
	}
	
//	contentView
	@RequestMapping(value = "/contentView")
	public String contentView (HttpServletRequest req, Model model) {
		model.addAttribute("request", req);
		command = new BoardContentViewCommand();
		command.execute(model);
		return "contentView";
	}
	
//	modify
	@RequestMapping(value = "/modify", method = RequestMethod.POST)
	public String modify (HttpServletRequest req, Model model) {
		model.addAttribute("request", req);
		command = new BoardModifyCommand();
		command.execute(model);
		return "redirect:list";
	}
}

Command

package com.bit.springBoard.command;

import org.springframework.ui.Model;

public interface BoardCommand {

	public void execute(Model model);
}

package com.bit.springBoard.command;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.ui.Model;

import com.bit.springBoard.dao.BoardDao;
import com.bit.springBoard.dto.BoardDto;

public class BoardContentViewCommand implements BoardCommand {

	@Override
	public void execute (Model model) {
		Map<String, Object> map = model.asMap();
		HttpServletRequest req = (HttpServletRequest)map.get("request");
		int id = Integer.parseInt(req.getParameter("id"));
		BoardDao dao = new BoardDao();
		BoardDto dto = dao.contentView(id);
		model.addAttribute("contentView", dto);
	}
}

package com.bit.springBoard.command;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.ui.Model;

import com.bit.springBoard.dao.BoardDao;

public class BoardDeleteCommand implements BoardCommand{

	@Override
	public void execute (Model model) {
		Map<String, Object> map = model.asMap();
		HttpServletRequest req = (HttpServletRequest)map.get("request");
		int id = Integer.parseInt(req.getParameter("id"));
		BoardDao dao = new BoardDao();
		dao.delete(id);
	}
}

package com.bit.springBoard.command;

import java.util.ArrayList;

import org.springframework.ui.Model;

import com.bit.springBoard.dao.BoardDao;
import com.bit.springBoard.dto.BoardDto;

public class BoardListCommand implements BoardCommand {
	
	@Override
	public void execute (Model model) {
		BoardDao dao = new BoardDao();
		ArrayList<BoardDto> btos = dao.list();
		model.addAttribute("list",btos);
	}
}

package com.bit.springBoard.command;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.ui.Model;

import com.bit.springBoard.dao.BoardDao;

public class BoardModifyCommand  implements BoardCommand{

	public void execute (Model model) {
		Map<String, Object> map = model.asMap();
		HttpServletRequest req = (HttpServletRequest)map.get("request");
		int id = Integer.parseInt(req.getParameter("id"));
		String name = req.getParameter("name");
		String title = req.getParameter("title");
		String content = req.getParameter("content");
		BoardDao dao = new BoardDao();
		dao.modify(id, name, title, content);
	}
}

package com.bit.springBoard.command;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.ui.Model;

import com.bit.springBoard.dao.BoardDao;

public class BoardWriteCommand implements BoardCommand {
	
	@Override
	public void execute(Model model) {
		Map<String, Object> map = model.asMap();
		HttpServletRequest req = (HttpServletRequest)map.get("request");
		String name = req.getParameter("name");
		String title = req.getParameter("title");
		String content = req.getParameter("content");
		BoardDao dao = new BoardDao();
		dao.write(name, title, content);
	}
}

BoardDao

package com.bit.springBoard.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import com.bit.springBoard.dto.BoardDto;

public class BoardDao {
	
	DataSource ds;
	int id;
	
	public BoardDao() {

		try {
			Context context = new InitialContext();
			ds = (DataSource) context.lookup("java:comp/env/jdbc/Oracle");
			System.out.println("연결성공");
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("응 연결실패ㅋ");
		}
	}
//	insert
	public void write(String name, String title, String content) {
		Connection con = null;
		PreparedStatement pstmt = null;
		String sql = null;
		try {
			con = ds.getConnection();
			sql = "insert into tblBoard (id, name, title, content, hit) values (tblBoardSeq.nextval, ?, ?, ?, 0)";
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, name);
			pstmt.setString(2, title);
			pstmt.setString(3, content);
			pstmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (pstmt != null)
					pstmt.close();
				if (con != null)
					con.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}
	
//	list
	public ArrayList<BoardDto> list() {
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = null;
		ArrayList<BoardDto> btos = new ArrayList<BoardDto>();

		try {
			con = ds.getConnection();
			sql = "select * from tblboard order by id desc";
			pstmt = con.prepareStatement(sql);
			rs = pstmt.executeQuery();
			
			while (rs.next()) {
				id = rs.getInt(1);
				String name = rs.getString(2);
				String title = rs.getString(3);
				String content = rs.getString(4);
				String regdate = rs.getString(5);
				int hit = rs.getInt(6);
				btos.add(new BoardDto(id, name, title, content, regdate, hit));
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (pstmt != null)
					pstmt.close();
				if (con != null)
					con.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return btos;
	}
	
//	contentView (개별적 게시물 리턴) 어레이리스트를 사용해도 되지만 의미가없다?
	public BoardDto contentView (int id) {
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = null;
		BoardDto dto = null;
		upHit(id);

		try {
			con = ds.getConnection();
			sql = "select * from tblboard where id = ?";
			pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, id);
			rs = pstmt.executeQuery();

//			while 을 써도 무방하지만, 정확성으로는 if가 맞다. 개별목록이니까.
			if (rs.next()) {
				String name = rs.getString("name");
				String title = rs.getString("title");
				String content = rs.getString("content");
				String regdate = rs.getString("regdate");
				int hit = rs.getInt("hit");
				dto = new BoardDto(id, name, title, content, regdate, hit);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (pstmt != null)
					pstmt.close();
				if (con != null)
					con.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return dto;
	}
	
	
//	modify (수정) write 와 유사하다
	public void modify(int id, String name, String title, String content) {
		Connection con = null;
		PreparedStatement pstmt = null;
		String sql = null;
		try {
			con = ds.getConnection();
			sql = "update tblboard set name =?, title = ?, content = ? where id = ?";
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, name);
			pstmt.setString(2, title);
			pstmt.setString(3, content);
			pstmt.setInt(4, id);
			pstmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (pstmt != null)
					pstmt.close();
				if (con != null)
					con.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}
	
//	delete
	public void delete (int id) {
		Connection con = null;
		PreparedStatement pstmt = null;
		String sql = null;
		try {
			con = ds.getConnection();
			sql = "delete from tblboard where id = ?";
			pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, id);
			pstmt.executeUpdate();
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (pstmt != null)
					pstmt.close();
				if (con != null)
					con.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}
	
//	upHit
	public void upHit (int id) {
		Connection con = null;
		PreparedStatement pstmt = null;
		String sql = null;
		try {
			con = ds.getConnection();
			sql = "update tblboard set hit = hit +1 where id = ?";
			pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, id);
			pstmt.executeUpdate();
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (pstmt != null)
					pstmt.close();
				if (con != null)
					con.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}
}

BoardDto

package com.bit.springBoard.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
public class BoardDto {
	
	private int Id;
	private String Name;
	private String Title;
	private String Content;
	private String regdate;
	private int hit;
	
}

 

728x90
반응형

'Spring > Spring framework' 카테고리의 다른 글

JPA : Supported keywords inside method names  (0) 2021.08.05
Spring Boot  (0) 2021.08.04
1  (0) 2021.08.04
Mybatis이용한 Board2  (0) 2021.08.03
Board  (0) 2021.07.30
Mybatis  (2) 2021.07.29