Consider defining a bean of type 'org.springframework.jdbc.core.JdbcTemplate' in your configuration

報錯信息:

Description:

Field customerRepo in com.chnvas.tour.auth.controller.AuthController required a bean of type 'org.springframework.jdbc.core.JdbcTemplate' that could not be found.


Action:

Consider defining a bean of type 'org.springframework.jdbc.core.JdbcTemplate' in your configuration.

解決方案:

在配置文件裏面加上

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

在運行類中加入如下代碼,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;
    }
}


---------------------

原文:https://blog.csdn.net/gwd1154978352/article/details/78383180

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章