Jam's story
[Spring] JDBC 본문
Spring DI
Spring AOP
Spring MVC - 공지사항, 회원가입 CRUD + 파일업로드
- 템플릿 클래스 지원
- 쿼리 sql - 다른부분
- jdbc driver 로딩
- Connection conn 객체
- PreparedStatement pstmt 객체 - 다른부분
- pstmt.executeQuery() - 다른부분
- List<Notice> list
- .conn, pstmt, rs 닫기
- 스프링의 의미있는 Exception 타입 존재한다.
- DataAccessException 등등
- 트랜잭션 처리
(2) 스프링에서 DB 연동해서 작업 (처리) - DataSource 객체
- 스프링 빈으로 등록
- mybatis 연동할 때도 DataSource 객체가 필요하다.
ㄱ. DataSourrce 설정하는 3가지 방법
- 커넥션풀을 이용한 DataSource 설정
- JNDI 를 이용한 방법 - X 웹로직, JBoss WAS 서버에서 사용한다.
- 로컬 테스트 할 목적으로 DriverManager 를 이용한 DataSource 설정 - X
ㄴ. 커넥션풀을 이용한 DataSource 설정
커넥션풀을 이용한 DataSource 설정
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"></property>
<property name="username" value="scott"></property>
<property name="password" value="tiger"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
ㄴ. 주요 메서드
- query() 조회하는 메서드
- queryForList() 조회하는 메서드. 읽어온 컬럼 개수가 한 개
- SELECT count(*) FROM emp
- queryForObject() 조회하는 메서드. 행이 한 개.
- SELECT * FROM emp WHERE empno = 7655
- update() 삽입, 수정, 삭제하는 메서드
NoticeDAO.java
// @Component
@Repository // noticeDao
public class NoticeDao {
@Autowired
private JdbcTemplate jdbcTemplate;
package newlecture.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.stereotype.Repository;
import newlecture.vo.Notice;
// @Component
@Repository // noticeDao
public class NoticeDao {
@Autowired
private JdbcTemplate jdbcTemplate;
// 검색한 결과의 총레코드 수 를 반환하는 메서드
public int getCount(String field, String query) throws ClassNotFoundException, SQLException
{
String sql = "SELECT COUNT(*) CNT "
+ " FROM NOTICES "
+ " WHERE "+field+" LIKE ?"; //
//return this.jdbcTemplate.queryForInt(sql, "%"+query+"%");
return this.jdbcTemplate.queryForInt(
sql
, new Object[] { "%"+query+"%" }
);
}
// 페이징 처리 + 공지사항 목록
public List<Notice> getNotices(int page, String field, String query) throws ClassNotFoundException, SQLException
{
int srow = 1 + (page-1)*15;
int erow = 15 + (page-1)*15;
String sql = " SELECT * "
+ " FROM ( "
+ " SELECT ROWNUM NUM, N.* "
+ " FROM ("
+ " SELECT * "
+ " FROM NOTICES "
+ " WHERE "+field+" LIKE ? "
+ " ORDER BY REGDATE DESC"
+ " ) N"
+ " ) "
+ " WHERE NUM BETWEEN ? AND ? ";
// 테이블 컬러명 == Notice DTO의 필드명 반드시 일치해야 된다.
List<Notice> list = this.jdbcTemplate.query(sql
, new Object[] { "%"+query+"%", srow, erow } // ? , ? , ?
, new BeanPropertyRowMapper<Notice>(Notice.class)
);
return list;
} // getNotices
// 공지사항 삭제
public int delete(String seq) throws ClassNotFoundException, SQLException
{
String sql = " DELETE FROM NOTICES "
+ " WHERE SEQ=?";
return this.jdbcTemplate.update(sql, seq); // mybatis
}
// 공지사항 수정
public int update( Notice notice ) throws ClassNotFoundException, SQLException{
String sql = "UPDATE NOTICES "
+ " SET TITLE=?, CONTENT=?, FILESRC=? "
+ " WHERE SEQ=?";
return this.jdbcTemplate.update(sql, notice.getTitle(), notice.getContent(), notice.getFilesrc(), notice.getSeq());
}
// 해당 공지사항 상세보기.
public Notice getNotice(String seq) throws ClassNotFoundException, SQLException
{
String sql = " SELECT * "
+ " FROM NOTICES "
+ " WHERE SEQ= ? ";
Notice notice = this.jdbcTemplate.queryForObject(
sql
, new Object[] { seq }
, ParameterizedBeanPropertyRowMapper.newInstance(Notice.class)
);
return notice;
}
// 공지사항 글쓰기
public int insert(Notice notice) throws ClassNotFoundException, SQLException {
String sql = "INSERT INTO NOTICES(SEQ, TITLE, CONTENT, WRITER, REGDATE, HIT, FILESRC) "
+ " VALUES( "
+ " (SELECT MAX(TO_NUMBER(SEQ))+1 FROM NOTICES), ?, ?, 'tae', SYSDATE, 0, ?) ";
return jdbcTemplate.update(sql, notice.getTitle(), notice.getContent(), notice.getFilesrc());
}
} // class
'Spring' 카테고리의 다른 글
[Spring] 트랜잭션 (0) | 2022.07.18 |
---|---|
[Spring] NamedParameterJdbcTemplate (0) | 2022.07.18 |
[Spring] 파일업로드 (0) | 2022.07.15 |
[Spring] 스프링 파일 업로드 (0) | 2022.07.14 |
[Spring] MVC2 모델을 이용한 게시판 구현 (0) | 2022.07.14 |
Comments