간단한 springframework JdbcTemplate 예제

2018. 10. 5. 22:57Web/Spring


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
@Repository
public class LearnRequestDAOImpl implements LearnRequestDAO{
    private static final String INSERT_SQL="INSERT INTO AIBOT_USER_LEARNREQUEST VALUES(LEARNREQUEST_SEQ.NEXTVAL,?,?,?,?,?)";
    private static final String SELECT_BY_QUESTION="SELECT LEARNREQUEST_ID,LEARN_QUESTION,LEARN_ANSWER,DEPT_NAME,EMP_CODE,USERNAME\n"
                                                    + "FROM USER_LEARNREQUEST\n"
                                                    + "WHERE LEARN_QUESTION =?";
    private static final String SELECT_INT_BY_QUESTION="SELECT COUNT(*) FROM USER_LEARNREQUEST WHERE LEARN_QUESTION=?";
    private static final String SELECT_LIST="SELECT * FROM USER_LEARNREQUEST";
    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    
    @Override
    public int insert(LearnRequestDTO dto) {
        // TODO Auto-generated method stub
        return jdbcTemplate.update(INSERT_SQL
                ,dto.getLearn_question()
                ,dto.getLearn_answer()
                ,dto.getDept_name()
                ,dto.getEmp_code()
                ,dto.getUsername());
    }
    @Override
    public List<LearnRequestDTO> selectList() {
        // TODO Auto-generated method stub
        return jdbcTemplate.query(SELECT_LIST, new RowMapper<LearnRequestDTO>() {
                    @Override
                    public LearnRequestDTO mapRow(ResultSet rs, int rowNum) throws SQLException {
                        // TODO Auto-generated method stub
                        LearnRequestDTO learnRequestDto=new LearnRequestDTO();
                        learnRequestDto.setLearnRequest_id(rs.getInt("LEARNREQUEST_ID"));
                        learnRequestDto.setLearn_question(rs.getString("LEARN_QUESTION"));
                        learnRequestDto.setLearn_answer(rs.getString("LEARN_ANSWER"));
                        learnRequestDto.setDept_name("DEPT_NAME");
                        learnRequestDto.setEmp_code("EMP_CODE");
                        learnRequestDto.setUsername("USERNAME");
                        return learnRequestDto;
                    }
        });
    }
    @Override
    public LearnRequestDTO selectByQuestion(String question) {
        // TODO Auto-generated method stub
        Object[] args= {question};
        return (LearnRequestDTO) jdbcTemplate.queryForObject(SELECT_BY_QUESTION,args,
                new RowMapper<LearnRequestDTO>() {
                    @Override
                    public LearnRequestDTO mapRow(ResultSet rs, int rowNum) throws SQLException {
                        // TODO Auto-generated method stub
                        LearnRequestDTO learnRequestDto=new LearnRequestDTO();
                        learnRequestDto.setLearnRequest_id(rs.getInt("LEARNREQUEST_ID"));
                        learnRequestDto.setLearn_question(rs.getString("LEARN_QUESTION"));
                        learnRequestDto.setLearn_answer(rs.getString("LEARN_ANSWER"));
                        learnRequestDto.setDept_name("DEPT_NAME");
                        learnRequestDto.setEmp_code("EMP_CODE");
                        learnRequestDto.setUsername("USERNAME");
                        return learnRequestDto;
                    }
        });
    }
    
}
 
cs


JdbcTemplate 사용전의 준비과정들은 모두 생략하였습니다.(DataSource....)