기록해! 정리해!

비즈니스 컴포넌트 실습 1 - Board 코드 본문

Spring

비즈니스 컴포넌트 실습 1 - Board 코드

zsuling 2022. 9. 20. 10:00

BoardWeb.zip
0.05MB

 

BoardDao

package com.springbook.biz.board;

import java.util.List;

public interface BoardDao {
	void  insert(BoardVO vo);
	List<BoardVO> select();
	void  delete(int seq);
	BoardVO edit(int seq);
}

 

BoardService

package com.springbook.biz.board;

import java.util.List;

public interface BoardService {
   void  insert(BoardVO vo);
   List<BoardVO> select();
   void  delete(int seq);
   BoardVO edit(int seq);
}

 

BoardInsertClient

package com.springbook.biz.board;

import java.util.List;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

public class BoardInsertClient {

	public static void main(String[] args) {
		  AbstractApplicationContext container =
				  new GenericXmlApplicationContext("applicationContext.xml");
		  BoardService service=(BoardService)container.getBean("boardService"); // SamSungTV , LgTV
		  
		  BoardVO vo = new BoardVO();
		  vo.setTitle("임시제목2");
		  vo.setWriter("홍길동2");
		  vo.setContent("임시내용2 ......");		  
		  service.insert(vo);
		  
		  List<BoardVO> li = service.select();
		  for(int i=0 ; i < li.size() ; i++ ) {
			  BoardVO  m = li.get(i);
			  System.out.println(m.toString());
			  
		  }

		  container.close();
	}

}

 

BoardServiceImpl

package com.springbook.biz.board.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.springbook.biz.board.BoardDao;
import com.springbook.biz.board.BoardService;
import com.springbook.biz.board.BoardVO;

@Service("boardService")
public class BoardServiceImpl implements BoardService {
	BoardServiceImpl(){
		System.out.println("===> BoardServiceImpl 생성자");
	}
	
	@Autowired	
	private BoardDao dao  ;
	
	
	@Override
	public void insert(BoardVO vo) {
		dao.insert(vo);		
	}


	@Override
	public List<BoardVO> select() {
	
		return dao.select();
	}


	@Override
	public void delete(int seq) {
		dao.delete(seq);		
	}


	@Override
	public BoardVO edit(int seq) {
		return dao.edit(seq);
	}

}

 

BoardDaoImpl

package com.springbook.biz.board.impl;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.springbook.biz.board.BoardDao;
import com.springbook.biz.board.BoardVO;
import com.springbook.biz.common.JDBCUtil;

@Repository("boardDao")
public class BoardDaoImpl implements BoardDao {
	
	private JDBCUtil jDBCUtil =null;
	private Connection conn = null;
	private PreparedStatement pstmt=null;
	private ResultSet  rs =null;
	
	private final String  BOARD_INSERT="insert  into board(seq, title, writer, content, REGDATE)"
			+ " values( (select  nvl(max(seq)+1,0) from board) ,?,?,?,'2022-08-25')";
	
	private final String  BOARD_SELECT="select seq, title, writer, content, REGDATE, cnt from board ";
	private final String  BOARD_DELETE="delete from board where seq=? ";
	private final String  BOARD_EDIT="select seq, title, writer, content, REGDATE, cnt from board where seq=? ";


	
	BoardDaoImpl(){
		System.out.println("===>BoardDaoImpl 생성자 ");
		jDBCUtil = JDBCUtil.getInstance();
	}
	
	@Override
	public void insert(BoardVO vo) {		
		
		try {
			conn = jDBCUtil.getConnection();
			pstmt = conn.prepareStatement(BOARD_INSERT);
			pstmt.setString(1, vo.getTitle());
			pstmt.setString(2, vo.getWriter());
			pstmt.setString(3, vo.getContent());
			pstmt.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			jDBCUtil.close(pstmt, conn);
		}
	}

	@Override
	public List<BoardVO> select() {
		List<BoardVO> li = new ArrayList<BoardVO>();
		try {
			conn = jDBCUtil.getConnection();
			pstmt = conn.prepareStatement(BOARD_SELECT);
			rs=pstmt.executeQuery();
			while(rs.next()) {
				BoardVO	 vo = new BoardVO();
				vo.setSeq(rs.getInt("seq"));
				vo.setTitle(rs.getString("title"));
				vo.setWriter(rs.getString("writer"));
				vo.setContent(rs.getString("content"));
				vo.setRegDate(rs.getDate("regDate"));
				vo.setCnt(rs.getInt("cnt"));
				li.add(vo);
			}
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			jDBCUtil.close(rs, pstmt, conn);
		}
		
		return li;
	}

	@Override
	public void delete(int seq) {
		try {
			conn = jDBCUtil.getConnection();
			pstmt = conn.prepareStatement(BOARD_DELETE);
			pstmt.setInt(1, seq);
			pstmt.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			jDBCUtil.close(pstmt, conn);
		}	
		
	}

	@Override
	public BoardVO edit(int seq) {
		BoardVO	 vo = new BoardVO();
		try {
			conn = jDBCUtil.getConnection();
			pstmt = conn.prepareStatement(BOARD_EDIT);
			pstmt.setInt(1, seq);
			rs=pstmt.executeQuery();
			rs.next();
			
				vo.setSeq(rs.getInt("seq"));
				vo.setTitle(rs.getString("title"));
				vo.setWriter(rs.getString("writer"));
				vo.setContent(rs.getString("content"));
				vo.setRegDate(rs.getDate("regDate"));
				vo.setCnt(rs.getInt("cnt"));
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			jDBCUtil.close(rs, pstmt, conn);
		}
		return vo;
	}

}
Comments