zoukankan      html  css  js  c++  java
  • spring javaconfig druidsource

    package dataConfig;

    import java.sql.SQLException;

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.jdbc.core.JdbcTemplate;

    import com.alibaba.druid.pool.DruidDataSource;

    @Configuration
    public class DataSourceConfig{
    @Bean
    public DruidDataSource dataSourceConfig() throws SQLException{
    DruidDataSource ds = new DruidDataSource();
    /*
    * 基本属性
    */
    ds.setDriverClassName("com.mysql.jdbc.Driver");
    ds.setUrl("jdbc:mysql://127.0.0.1:3306/my");
    ds.setUsername("root");
    ds.setPassword("root");
    /*
    * 配置初始化大小、最小、最
    */
    ds.setInitialSize(1);
    ds.setMinIdle(1);
    ds.setMaxActive(10);
    /*
    * 配置获取连接等待超时的时间
    */
    ds.setMaxWait(60000);
    /*
    * 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
    */
    ds.setTimeBetweenEvictionRunsMillis(60000);
    /*
    * 配置一个连接在池中最小生存的时间,单位是毫秒
    */
    ds.setMinEvictableIdleTimeMillis(300000);

    ds.setValidationQuery("SELECT 'X'");
    ds.setTestWhileIdle(true);
    ds.setTestOnBorrow(false);
    ds.setTestOnReturn(false);

    /*
    * 打开PSCache,并且指定每个连接上PSCache的大小
    */
    ds.setPoolPreparedStatements(false);;
    ds.setMaxPoolPreparedStatementPerConnectionSize(20);
    /*
    * 配置监控统计拦截的filters
    */
    ds.setFilters("stat");

    return ds;

    }

    /*
    * JDBC模板
    */
    @Bean
    public JdbcTemplate jdbcTemplate(DruidDataSource druidDataSource) throws SQLException{
    return new JdbcTemplate(druidDataSource);
    }

    }

  • 相关阅读:
    解决Cannot delete or update a parent row: a foreign key constraint fails的mysql报错
    zabbix4.2绘制网络拓扑图-添加链路速率
    zabbix 添加宏变量
    238_Product of Array Except Self
    122_Best Time to Buy and Sell Stock II
    260_Single Number III
    C# 比较时间问题
    226_Invert Binary Tree
    100_Same Tree
    283_Move Zeroes
  • 原文地址:https://www.cnblogs.com/shenjichenai/p/6568990.html
Copyright © 2011-2022 走看看