티스토리 뷰
RowMapper
DB의 컬럼명과 클래스의 필드명이 다른 경우 생성 (보통 JOIN TABLE)
cf) DB의 컬럼명과 클래스의 필드명이 같은 경우: #BeanPropertyRowMapper
1
2
3
4
5
6
7
8
9
10
11
12
|
public class RoleMapper implements RowMapper<Role> {
@Override
public Role mapRow(ResultSet rs, int rowNum) throws SQLException {
Role role = new role();
role.setRoleId(rs.getInt(1));
role.setDescription(rs.getString(2));
return role;
}
}
|
cs |
line 4 mapRow: 행의 수만큼 호출된다. (한 번만 호출되는 것이 아님)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@Repository
public class RoleDao {
private NamedParameterJdbcTemplate jdbc;
/* private RowMapper<Role> rowMapper = BeanPropertyRowMapper.newInstance(Role.class); */
public RoleDao(DataSource dataSource) {
this.jdbc = new NamedParameterJdbcTemplate(dataSource);
}
public List<Role> selectRoleAll(){
String SELECT_ALL = "SELECT role_id, description FROM role ORDER BY role_id";
return jdbc.query(SELECT_ALL, Collections.emptyMap(), /*rowMapper*/ new RoleMapper());
}
}
|
cs |
line 4, 12: BeanPropertyRowMapper 사용할 때와 차이
'JAVA > Spring' 카테고리의 다른 글
ApplicationConfig @ComponentScan 범위 오류 (0) | 2019.12.22 |
---|---|
[Spring] Service / Transaction (0) | 2019.10.14 |
[Spring] Connection Pool / Spring JDBC (0) | 2019.10.10 |
[Spring] Controller(Handler) (0) | 2019.10.09 |
[Spring] [Config] WebMvcContextConfiguration (0) | 2019.10.08 |