content: /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package com.alipay.dao.config;
  
  import com.alibaba.druid.pool.DruidDataSource;
  import org.apache.commons.lang3.StringUtils;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.jdbc.core.JdbcTemplate;
  
  import javax.sql.DataSource;
  import java.util.Properties;
  
  /*
   *@title DataConfig
   *@description
   *@author jietian
   *@version 1.0
   *@create 2024/10/30 16:35
   */
  @Configuration
  public class DataConfig {
  
      @Value("${spring.datasource.driver-class-name}")
      private String driver;
      @Value("${spring.datasource.url}")
      private String url;
      @Value("${spring.datasource.username}")
      private String username;
  
      @Value("${spring.datasource.password}")
      private String password;
  
      @Bean
      public DataSource dataSource() {
          DruidDataSource dataSource = new DruidDataSource();
          dataSource.setDriverClassName(driver);
          dataSource.setUrl(url);
          dataSource.setUsername(username);
          if (StringUtils.isEmpty(password)) {
              String password = System.getenv("DB_PASSWORD");
              dataSource.setPassword(password);
          } else {
              dataSource.setPassword(password);
          }
          dataSource.setValidationQuery("SELECT 1");
          dataSource.setTestOnBorrow(true);
  
          Properties properties = new Properties();
          properties.setProperty("connectionTimeout", "60");
          properties.setProperty("maxWait", "20000"); // 设置查询等待时间为5秒
          dataSource.setConnectProperties(properties);
          return dataSource;
      }
  
      @Bean
      public JdbcTemplate jdbcTemplate(DataSource dataSource) {
          return new JdbcTemplate(dataSource);
      }
  }
  