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

编程知识 更新时间:2023-05-02 05:32:35

首先遇到这个问题很好解决,不要慌

控制台显示没有找到jdbcTemplate就是因为没有配置连接池,给它安排上就OK了

package com.uncle.seciruty.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import com.alibaba.druid.pool.DruidDataSource;


/**
 * @program: spring-boot-security
 * @description:
 * @author: 步尔斯特
 * @create: 2021-07-23 19:35
 */
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class SecuritySpringBootApp {
    public static void main(String[] args) {
        SpringApplication.run(SecuritySpringBootApp.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;
    }

}

当然,别忘了加入依赖哦,阿里的德鲁伊。over。

更多推荐

Consider defining a bean of type ‘org.springframework.jdbc.core.JdbcTemplate‘ in

本文发布于:2023-04-26 07:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/600f90e6fd8116d3bde6449bdf8ce7a8.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:type   org   defining   bean   springframework

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 104617文章数
  • 26216阅读数
  • 0评论数