我是靠谱客的博主 优美凉面,最近开发中收集的这篇文章主要介绍springboot使用Druid来连接数据库,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

springboot使用Druid来连接数据库
(一):引入Druid依赖

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.23</version>
        </dependency>

(二):application.yml

spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/jdbc?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
	initialization-mode: always//别忘记
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    #   配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
    filters: stat,wall,slf4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

(三):编写DruidConfig:

@Configuration
public class DruidConfig {

    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druid(){
        return new DruidDataSource();

    }
    @Bean
    public ServletRegistrationBean servletRegistrationBean(){

       ServletRegistrationBean<StatViewServlet> bean= new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
        Map<String,String> init=new HashMap<>();
        init.put("loginUsername","admin");
        init.put("loginPassword","123456");
        init.put("allow","localhost");
        bean.setInitParameters(init);
       return bean;
   }

   @Bean
    public FilterRegistrationBean filterRegistrationBean(){
       FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<>();
      bean.setFilter(new WebStatFilter());
       Map<String, String> init = new HashMap<>();
       init.put("exclusions","*.js,*.css,/druid/*");
       bean.setInitParameters(init);
       bean.setUrlPatterns(Arrays.asList("/*"));
       return bean;

   }


}

修改对应的WebStatFilter.class:

public static final String PARAM_NAME_PROFILE_ENABLE = "profileEnable";
    public static final String PARAM_NAME_SESSION_STAT_ENABLE = "sessionStatEnable";
    public static final String PARAM_NAME_SESSION_STAT_MAX_COUNT = "sessionStatMaxCount";
    public static final String PARAM_NAME_EXCLUSIONS = "exclusions";
    public static final String PARAM_NAME_PRINCIPAL_SESSION_NAME = "principalSessionName";
    public static final String PARAM_NAME_PRINCIPAL_COOKIE_NAME = "principalCookieName";
    public static final String PARAM_NAME_REAL_IP_HEADER = "realIpHeader";

和ResourceServlet.class:

private static final Log LOG = LogFactory.getLog(ResourceServlet.class);
    public static final String SESSION_USER_KEY = "druid-user";
    public static final String PARAM_NAME_USERNAME = "loginUsername";
    public static final String PARAM_NAME_PASSWORD = "loginPassword";
    public static final String PARAM_NAME_ALLOW = "allow";
    public static final String PARAM_NAME_DENY = "deny";
    public static final String PARAM_REMOTE_ADDR = "remoteAddress";

最后是test:

@SpringBootTest
class Springboot06DataJdbcApplicationTests {

    @Autowired
    DataSource dataSource;
    @Test
    public void contextLoads()  throws SQLException{
        System.out.println(dataSource.getClass());
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();
    }

}

最后

以上就是优美凉面为你收集整理的springboot使用Druid来连接数据库的全部内容,希望文章能够帮你解决springboot使用Druid来连接数据库所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(44)

评论列表共有 0 条评论

立即
投稿
返回
顶部