<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- DBCP -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
전체소스
package com.springbook.k1.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;
import com.springbook.k1.common.BoardVO;
@Repository
public class BoardDAOSpring extends JdbcDaoSupport implements BoardDAO{
@Autowired
public void setSuperDataSource(DataSource dataSource) {
super.setDataSource(dataSource);
}
private final String sql_insert = "insert into board (seq, title, writer, content) values ((select max(seq)+1 from board), ?,?,? )";
private final String sql_delete = "delete from board where seq=?";
private final String sql_update = "update board set title=?, writer=?, content=? where seq = ?";
private final String sql_select = "select * from board where seq = ?";
private final String sql_selectList = "select * from board order by seq asc";
@Override
public void insertBoard(BoardVO vo) {
System.out.println("===> Spring JDBC로 insertBoard()기능 처리하기");
getJdbcTemplate().update(sql_insert, vo.getTitle(),vo.getWriter(),vo.getContent());
}
@Override
public void updateBoard(BoardVO vo) {
System.out.println("===> Spring JDBC로 updateBoard()기능 처리하기");
getJdbcTemplate().update(sql_update,vo.getTitle(),vo.getWriter(),vo.getContent(), vo.getSeq());
}
@Override
public void deleteBoard(int s) {
System.out.println("===> Spring JDBC로 deleteBoard()기능 처리하기");
getJdbcTemplate().update(sql_delete,s);
}
@Override
public BoardVO getBoard(BoardVO vo) {
Object[] args= {vo.getSeq()};
return getJdbcTemplate().queryForObject(sql_select, args, new BoardRowMapper());
}
@Override
public List<BoardVO> getBoardList(BoardVO vo) {
return getJdbcTemplate().query(sql_selectList, new BoardRowMapper());
}
class BoardRowMapper implements RowMapper<BoardVO>{
@Override
public BoardVO mapRow(ResultSet rs, int rowNum) throws SQLException {
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"));
return vo;
}
}
}