Spring JDBC에서 DB 접근하는 방법 – SqlUpdate(자바학원/스프링학원/JAVA학원)
http://ojc.asia/bbs/board.php?bo_table=LecSpring&wr_id=898
ojc.asia
https://www.youtube.com/watch?v=Pn4k4PSewxw&list=PLxU-iZCqT52B7oCYJltUT2-kBzckdr5vp&index=16

https://www.youtube.com/watch?v=2MlbXhykkwU&list=PLxU-iZCqT52B7oCYJltUT2-kBzckdr5vp&index=17

- 재사용 가능한 SQL Update 구문을 생성하며, 이를 위해 SqlUpdate 클래스를 상속받아서 구현해야 한다.
- SqlUpdate를 상속받은 하위 클래스의 생성자에서 SQL 쿼리 구문(DML : insert, update, delete), 파라미터를 생성하고 compile() 메소드를 호출하여 컴파일 한다. 마지막으로 update() 메소드를 호출하여 해당 DML 쿼리를 실행한다.
public class PersonUpdate extends SqlUpdate {
public PersonUpdate(final DataSource ds) {
setDataSource(ds);//SqlUpdate의 메소드 호출
setSql("update Person set firstname = ?, lastname = ? where id = ?"); //SqlUpdate 메소드호출
declareParameter(new SqlParameter(Types.VARCHAR)); //SqlUpdate 메소드호출
declareParameter(new SqlParameter(Types.VARCHAR));
declareParameter(new SqlParameter(Types.INTRGER));
compile(); //SqlUpdate의 메소드 호출
}
//update 구문 실행
public int run(final Person person) {
Object[] params =
new Object[] {
person.getFirstName(),
person.getLastName(),
person.getId()};
return update(params);
}
}
public class PersonInsert extends SqlUpdate {
public PersonInsert(final DataSource ds) {
setDataSource(ds);
setSql("insert into Person(id, firstname, lastname) values(?, ?, ?)");
declareParameter(new SqlParameter(Types.INTEGER));
declareParameter(new SqlParameter(Types.VARCHAR));
declareParameter(new SqlParameter(Types.VARCHAR));
compile();
}
// insert 구문 실행
public int run(final Person person) {
Object[] params = new Object[] { pserson.getId(),
person.getFirstName(),
person.getLastName()};
return update(params);
}
}
#SqlUpdate, #JPA, #JPA교육, #스프링부트, #스프링CRUD예제, #마리아DB, #스프링교육, #스프링동영상, #자바교육, #자바학원, #스프링학원, #스프링JDBC, SqlUpdate, JPA, JPA교육, 스프링부트, 스프링CRUD예제, 마리아DB, 스프링교육, 스프링동영상, 자바교육, 자바학원, 스프링학원, 스프링JDBC
댓글 없음:
댓글 쓰기