- 引入jdbcTemplate依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
- 编写一个类,并将jdbcTemplate直接注入到该类中
@Repository
public class PersonDaoImpl {
@Resource
private JdbcTemplate jdbcTemplate;
public Person findPersonById(long id){
String sql = "select * from person where id = ?";
RowMapper<Person> rowMapper = new BeanPropertyRowMapper<Person>(Person.class);
return jdbcTemplate.queryForObject(sql,rowMapper,id);
}
}
- 将该类注入至service并调用即可