PropertiesDB 是一个读取配置文件的类,也可以不用,每个参数直接用String代替。
public DataSource dataSource(PropertiesDB propertiesDB) { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(propertiesDB.getUrl()); dataSource.setDriverClassName(propertiesDB.getDriverClassName()); dataSource.setUsername(propertiesDB.getUsername()); dataSource.setPassword(propertiesDB.getPassword()); if (StringUtils.isNotBlank(propertiesDB.getConnectionProperties())) { dataSource.setConnectionProperties(propertiesDB.getConnectionProperties()); } dataSource.setInitialSize(propertiesDB.getInitialSize()); dataSource.setMinIdle(propertiesDB.getMinIdle()); dataSource.setMaxWait(propertiesDB.getMaxActive()); dataSource.setMaxActive(propertiesDB.getMaxActive()); dataSource.setTimeBetweenEvictionRunsMillis(propertiesDB.getTimeBetweenEvictionRunsMillis()); dataSource.setMinEvictableIdleTimeMillis(propertiesDB.getMinEvictableIdleTimeMillis()); dataSource.setTestWhileIdle(propertiesDB.getTestWhileIdle()); if (StringUtils.isNotBlank(propertiesDB.getValidationQuery())) { dataSource.setValidationQuery(propertiesDB.getValidationQuery()); } dataSource.setTestOnBorrow(propertiesDB.getTestOnBorrow()); dataSource.setTestOnReturn(propertiesDB.getTestOnReturn()); dataSource.setPoolPreparedStatements(propertiesDB.getPoolPreparedStatements()); dataSource.setMaxPoolPreparedStatementPerConnectionSize( propertiesDB.getMaxPoolPreparedStatementPerConnectionSize()); if (StringUtils.isNotBlank(propertiesDB.getFilters())) { try { dataSource.setFilters(propertiesDB.getFilters()); } catch (SQLException e) { logger.error("初始化数据库连接池发生异常:{}", e.toString()); } } return dataSource; }