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.

在这里插入图片描述
在网上找了很多资料,最终发现了错误所在之处,原来是在做测试的时候因为使用了@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})自动化配置,所以我们取消了自动化配置,于是程序顺利运行。
在这里插入图片描述
测试代码如下
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配置

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.21</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

最终结果
在这里插入图片描述

在这里插入图片描述

  • 11
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
在Spring Boot 3中出现 "Could not autowire. No beans of 'JdbcTemplate' type found" 错误通常是因为你没有正确配置JdbcTemplate bean。要解决这个问题,你可以按照以下步骤进行操作: 1. 确保你在项目的依赖中添加了正确的JdbcTemplate依赖。你可以在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> ``` 2. 确保在配置类中使用了`@EnableJdbc`注解,这将启用Spring Boot的JDBC支持。例如: ```java import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.core.JdbcTemplate; @Configuration @EnableJdbc public class AppConfig { // 注入JdbcTemplate bean @Bean public JdbcTemplate jdbcTemplate(DataSource dataSource) { return new JdbcTemplate(dataSource); } } ``` 注意:上述示例假设你已经正确配置了数据源(DataSource)。 3. 确保你在需要使用JdbcTemplate的类中使用了`@Autowired`注解进行自动注入。例如: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; @Repository public class MyRepository { private final JdbcTemplate jdbcTemplate; @Autowired public MyRepository(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } // 其他方法... } ``` 完成以上步骤后,重新运行应用程序,应该不再出现 "Could not autowire. No beans of 'JdbcTemplate' type found" 错误。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值