- implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
create table person (
id integer not null,
name varchar(255) not null,
location varchar(255),
birth_date timestamp,
primary key (id)
);
insert into person (id, name, location, birth_date) values (1, 'John', 'Seoul', sysdate());
insert into person (id, name, location, birth_date) values (2, 'Jane', 'Daegu', sysdate());
insert into person (id, name, location, birth_date) values (3, 'Jack', 'Incheon', sysdate());
public class Person {
private Integer id;
private String name;
private String location;
private Date birthDate;
public Person() {
}
public Person(Integer id, String name, String location, Date birthDate) {
this.id = id;
this.name = name;
this.location = location;
this.birthDate = birthDate;
}
....
@Repository
public class PersonJdbcDao {
@Autowired
JdbcTemplate jdbcTemplate;
public List<Person> findAll() {
return jdbcTemplate.query("select * from person",
new BeanPropertyRowMapper(Person.class));
}
public Person findById(Integer id) {
return jdbcTemplate.queryForObject("select * from person where id=?",
new BeanPropertyRowMapper<Person>(Person.class), id);
}
public int deleteById(Integer id) {
return jdbcTemplate.update("delete from person where id=?", id);
}
public int insert(Person person) {
return jdbcTemplate.update("insert into person (id, name, location, birth_date) " +
"values (?, ?, ?, ?)",
person.getId(),
person.getName(),
person.getLocation(),
new Timestamp(person.getBirthDate().getTime()));
}
public int update(Person person) {
return jdbcTemplate.update("update person set name=?, location=?, birth_date=? where id=?",
person.getName(),
person.getLocation(),
new Timestamp(person.getBirthDate().getTime()),
person.getId());
}
}