MVC_Spring - JDBC 템플릿 사용하기
프로그래밍 정리/Spring
2020. 1. 2. 16:48
반응형
MVC_Spring - JDBC 템플릿 사용하기
- Spring-JDBC 템플릿을 사용하여 코드줄을 줄일수 있다.
- 사용되는 Dependency
- spring-jdbc release 4.1.4
- 필요한 설정
-pom.xml
- servlet-context.xml
1 2 3 4 5 6 7 8 9 10 | <beans:bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" > //커넥션풀을 통한 데이터소스 초기화 <beans:property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> //드라이버 잡아줌 <beans:property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" /> //DB명 <beans:property name="username" value="scott" /> //접속 아이디 <beans:property name="password" value="tiger" /> //접속 비번 </beans:bean> <beans:bean name="template" class="org.springframework.jdbc.core.JdbcTemplate">//템플릿 사용하기 위해 정의 <beans:property name="dataSource" ref="dataSource"/> //커넥션풀을 사용하기 위해 초기화한 dataSource 집어넣음 </beans:bean> | cs |
- Import
1 2 | //JdbcTemplate를 import 해야한다. import org.springframework.jdbc.core.JdbcTemplate; | cs |
- 실습 예제
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | public class BDao { JdbcTemplate template; public BDao() { this.template = Constant.template; //생성자를 통해 템플릿 초기화 } public void insert_method(String bName, String bTitle, String bContent){ this.template.update(new PreparedStatementCreator() { //template update 메소드 사용하여 insert into 쿼리 실행 @Override public PreparedStatement createPreparedStatement(Connection con) throws SQLException { String query = "insert into mvc_board (bId, bName, bTitle, bContent, bHit, bGroup, bStep, bIndent) values (mvc_board_seq.nextval, ?, ?, ?, 0, mvc_board_seq.currval, 0, 0 )"; PreparedStatement pstmt = con.prepareStatement(query); pstmt.setString(1, bName); pstmt.setString(2, bTitle); pstmt.setString(3, bContent); return pstmt; } }); } public ArrayList<BDto> select_method() { String query = "select bId, bName, bTitle, bContent, bDate, bHit, bGroup, bStep, bIndent from mvc_board order by bGroup desc, bStep asc"; return (ArrayList<BDto>) template.query(query, new BeanPropertyRowMapper<BDto>(BDto.class)); //template query 메소드 사용 } public void delete_method(final String bId) { // TODO Auto-generated method stub String query = "delete from mvc_board where bId = ?"; this.template.update(query, new PreparedStatementSetter() { //template update 메소드 사용하여 delete from 쿼리 실행 @Override public void setValues(PreparedStatement ps) throws SQLException { ps.setString(1, bId); } }); } private void update_method(final String bId) { String query = "update mvc_board set bHit = bHit + 1 where bId = ?"; this.template.update(query, new PreparedStatementSetter() { //template update 메소드를 사용하여 update set @Override public void setValues(PreparedStatement ps) throws SQLException { // TODO Auto-generated method stub ps.setInt(1, Integer.parseInt(bId)); } }); } } | cs |
반응형
'프로그래밍 정리 > Spring' 카테고리의 다른 글
MVC_Spring - 트랜잭션 기본 (0) | 2020.01.03 |
---|---|
MVC_Spring - @Autowired 사용하기 (0) | 2020.01.03 |
MVC_Spring - 폼 데이터 값 검증 (0) | 2020.01.02 |
MVC_Spring - ID 또는 Password 체크 하기 (0) | 2020.01.02 |
MVC_Spring - Redirect 사용하기 (0) | 2019.12.31 |