기록해! 정리해!

Spring JDBC 방법 본문

Spring

Spring JDBC 방법

zsuling 2022. 9. 22. 10:01

1. 

	<!-- Spring -->
		
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>	
		
		<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>1.4</version>
		</dependency>
		
=======================================================================

​

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

<context:component-scan base-package="com.springbook.biz" />

<!--  
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
-->

<bean  id="dataSource" 
          class="org.apache.commons.dbcp.BasicDataSource" 
          destroy-method="close" >

 <property name="driverClassName"  value="oracle.jdbc.OracleDriver" />
 <property name="url"  value="jdbc:oracle:thin:@//localhost:1521/xe" />
 <property name="username"  value="system" />
 <property name="password"  value="1234" />
</bean>
 
</beans>
==========================================================================

​

package com.springbook.biz.board.impl;

import java.sql.*;
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.biz.board.BoardDao;
import com.springbook.biz.board.BoardVo;

@Repository
public class BoardDaoSpring 
             extends JdbcDaoSupport implements BoardDao {
	
	@Autowired
	public  void setSuperDataSource(DataSource dataSource ) {
		super.setDataSource(dataSource);
	}


	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, regdate, cnt from board " ;

	String BOARD_EDIT="select seq, title, writer, content"
			+ " from board where seq = ? " ;

	String BOARD_DELETE=" delete from board where seq = ? " ;
	
	String BOARD_UPDATE="update board set title=?, writer=?, content=? where seq=?";

	
	@Override
	public void insert(BoardVo vo) {
		getJdbcTemplate().update(BOARD_INSERT, vo.getTitle(), vo.getWriter(), vo.getContent());
	}

	@Override
	public void update(BoardVo vo) {
		getJdbcTemplate().update(BOARD_UPDATE, vo.getTitle(), vo.getWriter(), vo.getContent(),vo.getSeq());
	}

	@Override
	public void delete(int seq) {
		getJdbcTemplate().update(BOARD_DELETE, seq);
		
	}

	@SuppressWarnings("deprecation")
	@Override
	public BoardVo edit(int seq) {
		Object[] args = {seq}; 
		return getJdbcTemplate().queryForObject(BOARD_EDIT, args, new BoardRowMapper());
	}

	@Override
	public List<BoardVo> selectAll() {
		
		return  getJdbcTemplate().query(BOARD_SELECT, 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;
	}
	
}  
=====================================================================

​

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();
	}

}

'Spring' 카테고리의 다른 글

트랜잭션 처리  (0) 2022.09.22
Spring JDBC 방법 2  (0) 2022.09.22
어노데이션 기본 AOP - Before 어드바이스  (0) 2022.09.22
JoinPoint 메소드 사용하기  (0) 2022.09.22
AOP 사용하기  (0) 2022.09.22
Comments