1.在pom.xml加入jdbcTemplate的依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
如果在JPA(下面这个)已经加入的话,则可以不用引入以上的配置。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
2.操作数据库在dao层,在类中加入@Resource private JdbcTemplate jdbcTemplate;
@Repository //替代继承Repository接口,猜测
public class CatDao {
@Resource
private JdbcTemplate jdbcTemplate;
public Cat selectByCatName(String catName){
/** 1、定义一个Sql语句;2、定义一个RowMapper.3、执行查询方法.*/
String sql = "select *from cat where cat_name=?";
RowMapper<Cat> rowMapper = new BeanPropertyRowMapper<>(Cat.class);
Cat cat = jdbcTemplate.queryForObject(sql, new Object[]{catName}, rowMapper);
return cat;
}
}