解决:No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate] found for dependency

错误:

Description:

Field jdbcTemplate in com.gwd.dao.impl.IUserDaoImpl required a bean of type 'org.springframework.jdbc.core.JdbcTemplate' that could not be found.
- Bean method 'jdbcTemplate' not loaded because @ConditionalOnSingleCandidate (types: javax.sql.DataSource; SearchStrategy: all) did not find any beans

Action:

Consider revisiting the conditions above or defining a bean of type 'org.springframework.jdbc.core.JdbcTemplate' in your configuration.

解决方案:

在运行类中加入如下代码,DateSource这块看自己的数据源,我这边用的是阿里的druid

    1. package com.gwd;
    2. import javax.sql.DataSource;
    3. import org.springframework.beans.factory.annotation.Autowired;
    4. import org.springframework.boot.SpringApplication;
    5. import org.springframework.boot.autoconfigure.SpringBootApplication;
    6. import org.springframework.context.annotation.Bean;
    7. import org.springframework.core.env.Environment;
    8. import com.alibaba.druid.pool.DruidDataSource;
    9. @SpringBootApplication
    10. public class SpringBootTestApplication {
    11. public static void main(String[] args) {
    12. SpringApplication.run(SpringBootTestApplication.class, args);
    13. }
    14. @Autowired
    15. private Environment env;
    16. //destroy-method="close"的作用是当数据库连接不使用的时候,就把该连接重新放到数据池中,方便下次使用调用.
    17. @Bean(destroyMethod =  "close")
    18. public DataSource dataSource() {
    19. DruidDataSource dataSource = new DruidDataSource();
    20. dataSource.setUrl(env.getProperty("spring.datasource.url"));
    21. dataSource.setUsername(env.getProperty("spring.datasource.username"));//用户名
    22. dataSource.setPassword(env.getProperty("spring.datasource.password"));//密码
    23. dataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
    24. dataSource.setInitialSize(2);//初始化时建立物理连接的个数
    25. dataSource.setMaxActive(20);//最大连接池数量
    26. dataSource.setMinIdle(0);//最小连接池数量
    27. dataSource.setMaxWait(60000);//获取连接时最大等待时间,单位毫秒。
    28. dataSource.setValidationQuery("SELECT 1");//用来检测连接是否有效的sql
    29. dataSource.setTestOnBorrow(false);//申请连接时执行validationQuery检测连接是否有效
    30. dataSource.setTestWhileIdle(true);//建议配置为true,不影响性能,并且保证安全性。
    31. dataSource.setPoolPreparedStatements(false);//是否缓存preparedStatement,也就是PSCache
    32. return dataSource;
    33. }
    34. }
上一篇:Spring 整合Mybatis 出现了Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create Poola


下一篇:codeforces528D Fuzzy Search