引入依赖:
<!--如果已经引入了jpa,则不需要引用jdbc,即下面的依赖-->
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>-->
<!--添加spring-data-jpa依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
dao:
package com.huawei.dao;
import com.huawei.bean.Cat;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
@Repository
public class CatDao
{
@Resource
private JdbcTemplate jdbcTemplate;
public Cat selectByCatName(String catName) {
RowMapper<Cat> rowMapper = new BeanPropertyRowMapper<>(Cat.class);
String sql = "select * from cat where cat_name=?";
return jdbcTemplate.queryForObject(sql, new Object[]{catName}, rowMapper);
}
}
service:
public Cat selectByCatName(String catName) {
System.out.println("catName:"+catName);
return catDao.selectByCatName(catName);
}
controller:
@RequestMapping("/selectByCatName")
public Cat selectByCatName(String catName){
return catService.selectByCatName(catName);
}