springboot+druid 旧城等待, 2022-03-15 02:54 232阅读 0赞 1 springboots的项目创建 创建一个maven项目,引入springboot的相关依赖包 2 引入springboot-druid的jar 到druid的git中[https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter][https_github.com_alibaba_druid_tree_master_druid-spring-boot-starter] 进行引入 <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> 3 配置yml文件 spring: datasource: druid: url: jdbc:oracle:thin:@172.16.2.172:1521:SIEBELDB username: SIEBEL password: SIEBEL driver-class-name: oracle.jdbc.driver.OracleDriver initial-size: 10 max-active: 10000 min-idle: 2 max-wait: 100000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 # 是否缓存preparedStatement,也就是PSCache。PSCache对支持游标的数据库性能提升巨大,比如说oracle。在mysql下建议关闭。 pool-prepared-statements: true # 要启用PSCache,必须配置大于0,当大于0时,poolPreparedStatements自动触发修改为true。在Druid中,不会存在Oracle下PSCache占用内存过多的问题,可以把这个数值配置大一些,比如说100 max-open-prepared-statements: 100 validation-query: select 1 from dual validation-query-timeout: 100000 # 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。 建议false test-on-borrow: false # 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能 ,建议false test-on-return: false test-while-idle: true max-pool-prepared-statement-per-connection-size: 20 filters: stat 4 配置druid数据源连接池 @Configuration public class DBDruidConfig { private static Logger logger = LogManager.getLogger(DBDruidConfig.class); @Value("${spring.datasource.druid.url}") private String dbUrl; @Value("${spring.datasource.druid.username}") private String username; @Value("${spring.datasource.druid.password}") private String password; @Value("${spring.datasource.druid.driver-class-name}") private String driverClassName; @Value("${spring.datasource.druid.initial-size}") private int initialSize; @Value("${spring.datasource.druid.min-idle}") private int minIdle; @Value("${spring.datasource.druid.max-active}") private int maxActive; @Value("${spring.datasource.druid.max-wait}") private long maxWait; @Value("${spring.datasource.druid.timeBetweenEvictionRunsMillis}") private long timeBetweenEvictionRunsMillis; @Value("${spring.datasource.druid.minEvictableIdleTimeMillis}") private long minEvictableIdleTimeMillis; @Value("${spring.datasource.druid.validation-query}") private String validationQuery; @Value("${spring.datasource.druid.validation-query-timeout}") private int validationQueryTimeout; @Value("${spring.datasource.druid.test-while-idle}") private boolean testWhileIdle; @Value("${spring.datasource.druid.test-on-borrow}") private boolean testOnBorrow; @Value("${spring.datasource.druid.test-on-return}") private boolean testOnReturn; @Value("${spring.datasource.druid.pool-prepared-statements}") private boolean poolPreparedStatements; @Value("${spring.datasource.druid.max-pool-prepared-statement-per-connection-size}") private int maxPoolPreparedStatementPerConnectionSize; @Value("${spring.datasource.druid.filters}") private String filters; /* meger操作 @Value("{spring.datasource.connectionProperties}") private String connectionProperties;*/ @Bean(name = "druidDataSource") //声明其为Bean实例 @Primary //在同样的DataSource中,首先使用被标注的DataSource public DruidDataSource dataSource(){ DruidDataSource datasource = new DruidDataSource(); datasource.setUrl(this.dbUrl); datasource.setUsername(username); datasource.setPassword(password); datasource.setDriverClassName(driverClassName); //configuration datasource.setInitialSize(initialSize); datasource.setMinIdle(minIdle); datasource.setMaxActive(maxActive); datasource.setMaxWait(maxWait); datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); datasource.setValidationQuery(validationQuery); datasource.setValidationQueryTimeout(validationQueryTimeout); datasource.setTestWhileIdle(testWhileIdle); datasource.setTestOnBorrow(testOnBorrow); datasource.setTestOnReturn(testOnReturn); datasource.setPoolPreparedStatements(poolPreparedStatements); datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize); try { datasource.setFilters(filters); } catch (SQLException e) { logger.error("druid configuration initialization filter", e); } //datasource.setConnectionProperties(connectionProperties); return datasource; } } 5 使用中 @Resource(name = "linkDruidDataSource") private DruidDataSource druidDataSource; 6 操作 DruidPooledConnection conn = druidDataSource.getConnection(); [https_github.com_alibaba_druid_tree_master_druid-spring-boot-starter]: https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter
还没有评论,来说两句吧...