기록해! 정리해!

비즈니스 컴포넌트 실습 I (복습) - Board 본문

Spring

비즈니스 컴포넌트 실습 I (복습) - Board

zsuling 2022. 9. 21. 10:29

BoardService

package com.springbook.biz.board;

import java.util.List;

public interface BoardService {
    void  insert(BoardVo vo);
    void  update(BoardVo vo);
    void  delete(int seq);
    BoardVo  edit(int seq);
    List<BoardVo>  selectAll();
}

BoardDao

package com.springbook.biz.board;

import java.util.List;

public interface BoardDao {
    void  insert(BoardVo vo);
    void  update(BoardVo vo);
    void  delete(int seq);
    BoardVo  edit(int seq);
    List<BoardVo>  selectAll();
}

 

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
public class BoardDaoImpl implements BoardDao {
	
	BoardDaoImpl(){
		dbConn = JDBCUtil.getInstance();
	}
	
	private JDBCUtil dbConn =null;
	private Connection conn =null;
	private PreparedStatement pstmt =null;
	private ResultSet rs=null;
	
	String BOARD_INSERT="insert into board(seq, title, writer, content)"
			+ " values((select nvl(max(seq),0)+1 from board), ?,?,?)";

	String BOARD_SELECT=" select seq, title, writer, content from board " ;

	@Override
	public void insert(BoardVo vo) {
		try {
		conn =dbConn.getConnection();
		pstmt=conn.prepareStatement(BOARD_INSERT);
		pstmt.setString(1, vo.getTitle());
		pstmt.setString(2, vo.getWriter());
		pstmt.setString(3, vo.getContent());		
		pstmt.executeUpdate();
		
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			dbConn.close(pstmt, conn);
		}
	}

	@Override
	public void update(BoardVo vo) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void delete(int seq) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public BoardVo edit(int seq) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<BoardVo> selectAll() {
		List<BoardVo> li = new ArrayList<BoardVo>();
		try {
		conn =dbConn.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"));
			li.add(vo);
		}		
		
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			dbConn.close(rs, pstmt, conn);
		}
		return li;
	}

}

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 {

	@Autowired
	private BoardDao boardDao;
	
	@Override
	public void insert(BoardVo vo) {
		boardDao.insert(vo);		
	}

	@Override
	public void update(BoardVo vo) {
		boardDao.update(vo);		
	}

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

	@Override
	public BoardVo edit(int seq) {		
		return boardDao.edit(seq);
	}

	@Override
	public List<BoardVo> selectAll() {		
		return boardDao.selectAll();
	}

}

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");
		BoardVo vo =new BoardVo();
		vo.setTitle("임시 제목1");
		vo.setWriter("홍길동1");
		vo.setContent("임시내용1 ....");		
		service.insert(vo);
		
		List<BoardVo> li =service.selectAll();
		for(BoardVo m :li) {
		  System.out.println("===> " +m.toString());	
		}
		container.close();
	}

}
Comments