티스토리 뷰
728x90
.... 생략 ....
private SimpleJdbcTemplate template = new SimpleJdbcTemplate(dataSource);
또는..
이외에도 충분히 여러가지 형태로 오버로딩이 되어 있으므로 상황에 맞게 골라 쓰면 된다
아래는 SimpleJdbcTemplate 클래스의 API이다
젠장 짤린다....
private SimpleJdbcTemplate template = new SimpleJdbcTemplate(dataSource);
ㅇ 데이터 갯수 확인 - int queryForInt(String)
int count = template.queryForInt(
"SELECT count(*) FROM article");
ㅇ 데이터를 입력해서 문자열 반환
String password = template.queryForObject(
"SELECT password FROM article WHERE article_number=?",
String.class,
articleNumber);
ㅇ 리스트 형태 반환
List<BoardBean> list = null;
list = template.query(
"SELECT * FROM article WHERE article_number BETWEEN ? AND ?",
new BoardBeanRowMapper();
new Object[] { startRow, endRow });
[BoardBeanRowMapper.java]
import java.sql.*;
import org.springframework.jdbc.core.RowMapper;
public class BoardBeanRowMapper implements RowMapper<BoardBean> {
@Override
public BoardBean mapRow(ResultSet rs, int rowNum) throws SQLException {
BoardBean boardBean = new BoardBean();
boardBean.setArticleNumber(rs.getInt("article_number"));
boardBean.setId(rs.getString("id"));
boardBean.setTitle(rs.getString("title"));
boardBean.setContent(rs.getString("content"));
boardBean.setPassword(rs.getString("password"));
boardBean.setHitNumber(rs.getInt("hit_number"));
boardBean.setDepth(rs.getInt("depth"));
String writeDateTemp = String.valueOf(rs.getTimestamp("write_date"));
String date = writeDateTemp.substring(0, 4) + "년 " + writeDateTemp.substring(5, 7) + "월" +
writeDateTemp.substring(8, 10) + "일 " + writeDateTemp.substring(11, 19);
boardBean.setWriteDate(date);
boardBean.setFileName(rs.getString("file_name"));
return boardBean;
}
}
[BoardBean.java]
public class BoardBean {
int articleNumber;
String id;
String title;
String content;
String password;
int hitNumber;
int depth;
String writeDate;
String fileName;
.... Getter / Setter 생략 ....
.... Getter / Setter 생략 ....
}
ㅇ INSERT
template.update(
"INSERT INTO article_model2 VALUES (article_model2_sequence.nextval, ?, ?, ?, ?, ?, ?, sysdate, ?)",
upload.getId(),
upload.getTitle(),
upload.getContent(),
upload.getPassword(),
upload.getTitle(),
upload.getContent(),
upload.getPassword(),
upload.getHitNumber(),
upload.getDepth(),
upload.getFile().getOriginalFilename()
);
upload.getDepth(),
upload.getFile().getOriginalFilename()
);
또는..
BeanPropertySqlParameterSource bps = new BeanPropertySqlParameterSource(boardBean);
template.update(
"INSERT INTO
article
VALUES
(article_seq.NEXTVAL, :id, :title, :content, :password, :hit_number, :depth, SYSDATE, :file_name)",
bps);
"INSERT INTO
article
VALUES
(article_seq.NEXTVAL, :id, :title, :content, :password, :hit_number, :depth, SYSDATE, :file_name)",
bps);
ㅇ UPDATE
template.update(
"UPDATE article_model2 SET hit_number=hit_number+1 WHERE article_number=?",
articleNumber
);
ㅇ DELETE
template.update(
"DELETE FROM article_model2 WHERE article_number=?",
articleNumber
);
);
아래는 SimpleJdbcTemplate 클래스의 API이다
Method Summary | ||
---|---|---|
int[] |
batchUpdate(String sql, List<Object[]> batchArgs) Execute a batch using the supplied SQL statement with the batch of supplied arguments. |
|
int[] |
batchUpdate(String sql, List<Object[]> batchArgs, int[] argTypes) Execute a batch using the supplied SQL statement with the batch of supplied arguments. |
|
int[] |
batchUpdate(String sql, Map<String,?>[] batchValues) Executes a batch using the supplied SQL statement with the batch of supplied arguments. |
|
int[] |
batchUpdate(String sql, SqlParameterSource[] batchArgs) Execute a batch using the supplied SQL statement with the batch of supplied arguments. |
|
JdbcOperations |
getJdbcOperations() Expose the classic Spring JdbcTemplate to allow invocation of less commonly used methods. |
|
NamedParameterJdbcOperations |
getNamedParameterJdbcOperations() Expose the Spring NamedParameterJdbcTemplate to allow invocation of less commonly used methods. |
|
|
query(String sql, ParameterizedRowMapper<T> rm, Map<String,?> args) Deprecated. |
|
|
query(String sql, ParameterizedRowMapper<T> rm, Object... args) Deprecated. |
|
|
query(String sql, ParameterizedRowMapper<T> rm, SqlParameterSource args) Deprecated. |
|
|
query(String sql, RowMapper<T> rm, Map<String,?> args) Query for a List of Objects of type T using the supplied RowMapper to the query results to the object. |
|
|
query(String sql, RowMapper<T> rm, Object... args) Query for a List of Objects of type T using the supplied RowMapper to the query results to the object. |
|
|
query(String sql, RowMapper<T> rm, SqlParameterSource args) Query for a List of Objects of type T using the supplied RowMapper to the query results to the object. |
|
int |
queryForInt(String sql, Map<String,?> args) Query for an int passing in a SQL query using the named parameter support provided by the NamedParameterJdbcTemplate and a map containing the arguments. |
|
int |
queryForInt(String sql, Object... args) Query for an int passing in a SQL query using the standard '?' placeholders for parameters and a variable number of arguments. |
|
int |
queryForInt(String sql, SqlParameterSource args) Query for an int passing in a SQL query using the named parameter support provided by the NamedParameterJdbcTemplate and a SqlParameterSource containing the arguments. |
|
List<Map<String,Object>> |
queryForList(String sql, Map<String,?> args) Execute the supplied query with the supplied arguments. |
|
List<Map<String,Object>> |
queryForList(String sql, Object... args) Execute the supplied query with the (optional) supplied arguments. |
|
List<Map<String,Object>> |
queryForList(String sql, SqlParameterSource args) Execute the supplied query with the supplied arguments. |
|
long |
queryForLong(String sql, Map<String,?> args) Query for an long passing in a SQL query using the named parameter support provided by theNamedParameterJdbcTemplate and a map containing the arguments. |
|
long |
queryForLong(String sql, Object... args) Query for an long passing in a SQL query using the standard '?' placeholders for parameters and a variable number of arguments. |
|
long |
queryForLong(String sql, SqlParameterSource args) Query for an long passing in a SQL query using the named parameter support provided by theNamedParameterJdbcTemplate and a SqlParameterSource containing the arguments. |
|
Map<String,Object> |
queryForMap(String sql, Map<String,?> args) Execute the supplied query with the supplied arguments. |
|
Map<String,Object> |
queryForMap(String sql, Object... args) Execute the supplied query with the (optional) supplied arguments. |
|
Map<String,Object> |
queryForMap(String sql, SqlParameterSource args) Execute the supplied query with the supplied arguments. |
|
|
queryForObject(String sql, Class<T> requiredType, Map<String,?> args) Query for an object of type T identified by the supplied @Class . |
|
|
queryForObject(String sql, Class<T> requiredType, Object... args) Query for an object of type T identified by the supplied @Class . |
|
|
queryForObject(String sql, Class<T> requiredType, SqlParameterSource args) Query for an object of type T identified by the supplied @Class . |
|
|
queryForObject(String sql, ParameterizedRowMapper<T> rm, Map<String,?> args) Deprecated. |
|
|
queryForObject(String sql, ParameterizedRowMapper<T> rm, Object... args) Deprecated. |
|
|
queryForObject(String sql, ParameterizedRowMapper<T> rm, SqlParameterSource args) Deprecated. |
|
|
queryForObject(String sql, RowMapper<T> rm, Map<String,?> args) Query for an object of type T using the supplied RowMapper to the query results to the object. |
|
|
queryForObject(String sql, RowMapper<T> rm, Object... args) Query for an object of type T using the supplied RowMapper to the query results to the object. |
|
|
queryForObject(String sql, RowMapper<T> rm, SqlParameterSource args) Query for an object of type T using the supplied RowMapper to the query results to the object. |
|
int |
update(String sql, Map<String,?> args) Execute the supplied SQL statement with (optional) supplied arguments. |
|
int |
update(String sql, Object... args) Execute the supplied SQL statement with supplied arguments. |
|
int |
update(String sql, SqlParameterSource args) Execute the supplied SQL statement with supplied arguments. |
젠장 짤린다....
'프로그래밍 > iBatis, MyBatis' 카테고리의 다른 글
[iBatis / MyBatis] Java Bean 클래스와 DB 컬럼명이 다를 경우 데이터 가져오는 방법 (0) | 2011.08.26 |
---|---|
[iBatis / MyBatis] DB 데이터 불러오기 (0) | 2011.08.24 |
TAG
- Android
- 안드로이드
- 스프링
- 여성가족부
- 서양인
- struts
- JSP
- 동양인
- 함수
- window
- 오류
- mvc
- 특수문자
- ibatis
- 페이지 이동
- 주피터 노트북
- 파이썬
- JavaScript
- 데이터베이스
- jstl
- 랜덤
- MacOS
- 자바스크립트
- 기본
- EL
- 시각 차이
- 코멧
- 테이블
- 구매 가이드
- 스트럿츠
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함
- Total
- Today
- Yesterday