錯誤:java
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.spring
解決方案:sql
在運行類中加入以下代碼,DateSource這塊看本身的數據源,我這邊用的是阿里的druid數據庫
- package com.gwd;
- import javax.sql.DataSource;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.context.annotation.Bean;
- import org.springframework.core.env.Environment;
- import com.alibaba.druid.pool.DruidDataSource;
- @SpringBootApplication
- public class SpringBootTestApplication {
- public static void main(String[] args) {
- SpringApplication.run(SpringBootTestApplication.class, args);
- }
- @Autowired
- private Environment env;
- //destroy-method="close"的做用是當數據庫鏈接不使用的時候,就把該鏈接從新放到數據池中,方便下次使用調用.
- @Bean(destroyMethod = "close")
- public DataSource dataSource() {
- DruidDataSource dataSource = new DruidDataSource();
- dataSource.setUrl(env.getProperty("spring.datasource.url"));
- dataSource.setUsername(env.getProperty("spring.datasource.username"));//用戶名
- dataSource.setPassword(env.getProperty("spring.datasource.password"));//密碼
- dataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
- dataSource.setInitialSize(2);//初始化時創建物理鏈接的個數
- dataSource.setMaxActive(20);//最大鏈接池數量
- dataSource.setMinIdle(0);//最小鏈接池數量
- dataSource.setMaxWait(60000);//獲取鏈接時最大等待時間,單位毫秒。
- dataSource.setValidationQuery("SELECT 1");//用來檢測鏈接是否有效的sql
- dataSource.setTestOnBorrow(false);//申請鏈接時執行validationQuery檢測鏈接是否有效
- dataSource.setTestWhileIdle(true);//建議配置爲true,不影響性能,而且保證安全性。
- dataSource.setPoolPreparedStatements(false);//是否緩存preparedStatement,也就是PSCache
- return dataSource;
- }
- }