insert, update 구현
1) RoleDao 클래스 수정
- SimpleJdbcInsert 객체 추가
- RoleDao 생성자에 넣을 테이블명과 함께 SimpleJdbcInsert 객체 초기화
- insert, update 메소드 작성
org.springframework.jdbc.core.simple.SimpleJdbcInsert;
public class RoleDao {
private SimpleJdbcInsert insertAction;
public RoleDao(DataSource dataSource) {
this.jdbc = new NamedParameterJdbcTemplate(dataSource);
this.insertAction=new SimpleJdbcInsert(dataSource).withTableName("role");
}
public int insert(Role role) {
SqlParameterSource params = new BeanPropertySqlParameterSource(role);
return insertAction.execute(params);
}
public int update(Role role) {
SqlParameterSource params = new BeanPropertySqlParameterSource(role);
return jdbc.update(UPDATE, params);
}
}
2) RoleDaoSqls 클래스 수정
- UPDATE 변수에 UPDATE SQL문 추가
public static final String UPDATE= "UPDATE role SET description = :description WHERE ROLE_ID = :roleId";
3) main 패키지에 JDBCTest 클래스 생성
package kr.or.connect.daoexam.main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import kr.or.connect.daoexam.config.ApplicationConfig;
import kr.or.connect.daoexam.dao.RoleDao;
import kr.or.connect.daoexam.dto.Role;
public class JDBCTest {
public static void main(String[] args) {
ApplicationContext ac = new AnnotationConfigApplicationContext(ApplicationConfig.class);
RoleDao roleDao = ac.getBean(RoleDao.class);
Role role = new Role();
role.setRoleId(201);
role.setDescription("CEO");
int count = roleDao.insert(role);
System.out.println(count+"건 입력 완료");
role.setRoleId(201);
role.setDescription("PROGRAMMER");
int count = roleDao.update(role);
System.out.println(count+"건 수정 완료");
}
}