为了账号安全,请及时绑定邮箱和手机立即绑定

没有类型为“org.springframework.jdbc.core.JdbcTemplate”

没有类型为“org.springframework.jdbc.core.JdbcTemplate”

哆啦的时光机 2023-07-13 14:14:28
我正在https://developer.ibm.com/tutorials/spring-with-db2-via-jdbc/上运行tutorial.example, 但无法让它工作,我不断收到以下错误,并且不确定如何修复。没有“org.springframework.jdbc.core.JdbcTemplate”类型的合格 bean 可用:预计至少有 1 个有资格作为自动装配候选者的 bean。依赖注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)}'教程中没有提到与设置 bean 相关的内容,所以我不确定我是否应该中断它来修复它,或者我只是犯了一个错误。我的应用程序类 -@SpringBootApplicationpublic class SBApplication {    public static void main(String[] args) {        SpringApplication.run(SBApplication.class, args);    } }休息控制器示例 -package application.rest.v1;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.http.HttpStatus;import org.springframework.http.ResponseEntity;import org.springframework.web.bind.annotation.ResponseBody;import java.util.ArrayList;import java.util.List;import main.java.application.jdbc.*;@RestControllerpublic class Example {    @Autowired    JdbcTemplate jdbcTemplate;    @RequestMapping("test")    public @ResponseBody ResponseEntity<String> example() {        List<String> list = new ArrayList<>();        list.add("Table data...");        jdbcTemplate.query(                "SELECT * FROM things", new Object[]{},                (rs,rowNum) -> new Things(rs.getLong("id"), rs.getString("name")))                .forEach(thing -> list.add(thing.toString()));        return new ResponseEntity<String>(list.toString(), HttpStatus.OK);    }}application.properties -spring.datasource.url=jdbc:imdb://xxxx.xxx.xxxx/xxxxspring.datasource.username=xxxxxxxspring.datasource.password=xxxxspring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration另外,我没有连接到教程中建议的 DB2 实例,而是连接到我自己的实例。
查看完整描述

1 回答

?
一只甜甜圈

TA贡献1836条经验 获得超5个赞

我相信您缺少应该JdbcTemplate在配置中配置的部分。当您使用 时,您可以通过类上的注释spring boot来实现它。@Configuration您的典型配置如下所示


@Configuration

public class WebAppConfig {


    @Bean(name = "appDataSource")

    @Primary

    @ConfigurationProperties(prefix = "spring.datasource")

    public DataSource dataSource() {

        return DataSourceBuilder.create().build();

    }


    @Bean(name = "applicationJdbcTemplate")

    public JdbcTemplate applicationDataConnection(){

        return new JdbcTemplate(dataSource());

    }

}


查看完整回答
反对 回复 2023-07-13
  • 1 回答
  • 0 关注
  • 112 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信