SpringBoot引入jdbcTemplate时报错Field jdbcTemplate in com.x required a bean of type...could not be found

在学习SpringBoot的时候,引入jdbcTemplate时出了一点错误

Field jdbcTemplate in com.hsy.service.UserServiceImpl required a bean of type 'org.springframework.jdbc.core.JdbcTemplate' that could not be found.

SpringBoot引入jdbcTemplate时报错Field jdbcTemplate in com.x required a bean of type...could not be found_第1张图片
在网上找了很多资料,最终发现了错误所在之处,原来是在做测试的时候因为使用了@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})自动化配置,所以我们取消了自动化配置,于是程序顺利运行。
SpringBoot引入jdbcTemplate时报错Field jdbcTemplate in com.x required a bean of type...could not be found_第2张图片
测试代码如下
controller层

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/createUser")
    public String createUser(int id,int age){
        userService.create(id, age);
        return "success";
    }

}

service层

public interface UserService {

    void create(int id,int age);

}

@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void create(int id, int age) {
        jdbcTemplate.update("insert into testuser(id,age) value (?,?)",id,age);
    }
}

application.properties配置

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/你的数据库名
spring.datasource.username=root
spring.datasource.password=111111

pom.xml配置


            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.0.0
        

        
            mysql
            mysql-connector-java
            5.1.21
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

最终结果
SpringBoot引入jdbcTemplate时报错Field jdbcTemplate in com.x required a bean of type...could not be found_第3张图片

SpringBoot引入jdbcTemplate时报错Field jdbcTemplate in com.x required a bean of type...could not be found_第4张图片

你可能感兴趣的:(SpringBoot)