一:引入jdbcTemplate依赖
如果在JPA已经加入的话,则可以不用引入以上的配置。 org.springframework.boot spring-boot-starter-jdbc org.springframework.boot spring-boot-starter-data-jpa
编写DAO
直接使用就可以了
@Resource
private JdbcTemplate jdbcTemplate;
package com.mt.dog.dao;import java.util.List;import javax.annotation.Resource;import org.springframework.jdbc.core.BeanPropertyRowMapper;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.core.RowMapper;import org.springframework.stereotype.Repository;import com.mt.dog.entity.Dog;@Repositorypublic class DogDao { @Resource private JdbcTemplate jdbcTemplate; public Dog getDogById(Long id) { String sql = "select * from dog where id=? "; RowMapperrowMapper = new BeanPropertyRowMapper<>(Dog.class); List dogs = jdbcTemplate.query(sql, new Object[] { id }, rowMapper); return dogs.get(0); }}