@Component
public class PersonDao {
private final JdbcTemplate jdbcTemplate;
@Autowired
public PersonDao(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public List<Person> index() {
return jdbcTemplate.query("SELECT * FROM person", new BeanPropertyRowMapper<>(Person.class));
}
public Person show(int id){
return jdbcTemplate.query("SELECT * FROM person WHERE id=?", new Object[]{id}, new BeanPropertyRowMapper<>(Person.class))
.stream().findAny().orElse(null);
}
public void save(Person person){
jdbcTemplate.update("INSERT INTO person (name, age, email) VALUES (?, ?, ?)", person.getName(), person.getAge(), person.getEmail());
}
public void edit(Integer id, Person person) {
jdbcTemplate.update("UPDATE person SET name = ?, age = ?, email = ? WHERE id = ?", person.getName(), person.getAge(), person.getEmail(), id);
}
public void delete(Integer id) {
jdbcTemplate.update("DELETE FROM person WHERE id = ?", id);
}
}