기록해! 정리해!

Spring JDBC 방법 2 본문

Spring

Spring JDBC 방법 2

zsuling 2022. 9. 22. 10:02
<?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" />

<context:property-placeholder  location="classpath:config/database.properties" />
<bean id="dataSource"  class="org.apache.commons.dbcp.BasicDataSource"  destroy-method="close" >
 <property name="driverClassName"  value="${jdbc.driver}" />
 <property name="url"  value="${jdbc.url}" />
 <property name="username"  value="${jdbc.username}" />
 <property name="password"  value="${jdbc.password}" />
</bean>
 
<!-- Spring JDBC 설정 --> 
<bean id="jdbcTemplate"  class="org.springframework.jdbc.core.JdbcTemplate" >
  <property name="dataSource"  ref="dataSource" ></property>
</bean>

</beans>
=======================================================================

​

package com.springbook.biz.board.impl;


import java.util.List;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
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 BoardDaoSpring2  implements BoardDao {
	
	private final String BOARD_INSERT="insert into board(seq, title, writer, content)"
			+ " values((select nvl(max(seq),0)+1 from board), ?,?,?)";
	private final String BOARD_SELECT=" select * from board " ;
	private final String BOARD_EDIT="select seq, title, writer, content, regdate, cnt"
			+ " from board where seq = ? " ;
	private final String BOARD_DELETE=" delete from board where seq = ? " ;
	private final String BOARD_UPDATE="update board set title=?, writer=?, content=? where seq=?";
	
	// applicationContext.xml 에서  JdbcTemplate 형 받아오기
	@Autowired
    private  JdbcTemplate  jdbcTemplate;
	
	@Override
	public void insert(BoardVo vo) {
		Object[] args = {vo.getTitle(), vo.getWriter(), vo.getContent()};
		jdbcTemplate.update(BOARD_INSERT, args );
	}

	@Override
	public void update(BoardVo vo) {
		Object[] args = {vo.getTitle(), vo.getWriter(), vo.getContent(),vo.getSeq() };
		jdbcTemplate.update(BOARD_UPDATE, args);
	}

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

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

	@Override
	public List<BoardVo> selectAll() {		
		return  jdbcTemplate.query(BOARD_SELECT, new BoardRowMapper());
	}
}
​

​

====================================================================

​

package com.springbook.biz.user.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.springbook.biz.user.UserDao;
import com.springbook.biz.user.UserVo;

@Repository
public class UserDaoImpl implements UserDao {
	
    String SQL="select *  from  users where id=? and password=?";

	@Autowired
    private  JdbcTemplate  jdbcTemplate;
	
	@SuppressWarnings("deprecation")
	@Override
	public UserVo getUser(UserVo vo) {
		 System.out.println(" ===>  getUser(UserVo vo) 기능처리 " );
		 Object [] args = {vo.getId(), vo.getPassword()};
		return jdbcTemplate.queryForObject(SQL, args, new UserRowMapper());				 
	}
}
====================================================================

​

package com.springbook.biz.user.impl;

import java.sql.*;
import org.springframework.jdbc.core.RowMapper;
import com.springbook.biz.user.UserVo;

public class UserRowMapper implements RowMapper<UserVo> {

	@Override
	public UserVo mapRow(ResultSet rs, int rowNum) throws SQLException {
		UserVo  user=new UserVo();
		 user.setId(rs.getString("id"));
		 user.setName(rs.getString("name"));
		 user.setPassword(rs.getString("password"));
		 user.setRole(rs.getString("role"));
		return user;
	}

}

'Spring' 카테고리의 다른 글

Model 1 아키텍처로 게시판 개발  (0) 2022.09.23
트랜잭션 처리  (0) 2022.09.22
Spring JDBC 방법  (0) 2022.09.22
어노데이션 기본 AOP - Before 어드바이스  (0) 2022.09.22
JoinPoint 메소드 사용하기  (0) 2022.09.22
Comments