菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
380
0

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

原创
05/13 14:22
阅读数 80211

错误:

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

 

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

发表评论

0/200
380 点赞
0 评论
收藏
为你推荐 换一批