SpringBoot 搭建多模块的后台项目-SSM(二)将Druid连接池集成进项目

1.在父工程中引入所需启动jar包的版本信息

2.在web子模块中导入所需jar包



    4.0.0

    com.powerlong
    xproject-web
    ${xproject.version}
    war

    xproject-web
    xproject-web project for Spring Boot

    
        com.powerlong
        xpoject
        0.0.1-SNAPSHOT
         
    

    

        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            org.springframework.boot
            spring-boot-configuration-processor
        
        
            com.powerlong
            xproject-service
            ${xproject.version}
        

        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            ${mybatis.starter.version}
        

        
            tk.mybatis
            mapper-spring-boot-starter
            ${mapper.starter.version}
        

        
            com.github.pagehelper
            pagehelper-spring-boot-starter
            ${pagehelper.starter.version}
        

        
            mysql
            mysql-connector-java
            ${mysql.connector.version}
        

        
            com.alibaba
            druid-spring-boot-starter
            ${druid.starter.version}
        

    


3.代码的开发

     3.1) 在xproject-web模块,配置数据源信息


#******************************
#**********数据源配置开始*********
#******************************
spring.datasource.name=druidDataSource
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.url=jdbc:mysql://172.20.10.2:3306/ssm?useUnicode=true&characterEncoding=UTF-8&useSSL=false
spring.datasource.druid.username=root
spring.datasource.druid.password=MaryF6650
spring.datasource.druid.filters=stat

#最大连接数
spring.datasource.druid.max-active=100
#初始化连接池的时候,连接数
spring.datasource.druid.initial-size=5
#空闲时最小连接数
spring.datasource.druid.min-idle=5
# 获取最大连接时最大等待时间,单位时毫秒
# 配置了maxWait之后,缺省启用公平锁,并发效率会有所下降
# 如果需要可以通过配置useUnfairLock属性为true使用非公平锁
spring.datasource.druid.max-wait=6000
# 是否缓存preparedStatement,也就是PSCache
# PSCache对支持游标的数据库性能提升巨大,比如说oracle.
# 在mysql下建议关闭
spring.datasource.druid.pool-prepared-statements=false
# 当poolPreparedStatements配置为true时,才可以使用
# 要启用PSCache,必须配置大于0,当大于0时,poolPreparedStatements自动触发修改为true。
# 在Druid中,不会存在Oracle下PSCache占用内存过多的问题
# 可以把这个数值配置大一些,比如说500
spring.datasource.druid.max-open-prepared-statements=500
#用来检测连接是否有效的sql,要求是一个查询语句
# 如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会其作用。
# MySql : SELECT 'x'
# Oracle:SELECT 'x' FROM dual
spring.datasource.druid.validation-query= SELECT 'x'
# 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
# 开发时使用,生产环境建议关闭
# 默认值为true
spring.datasource.druid.test-on-borrow=true
# 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
# 默认值为false
spring.datasource.druid.test-on-return=false
# 间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.druid.time-between-eviction-runs-millis=60000
#建议配置为true,不影响性能,并且保证安全性.申请连接的时候检测
# 如果空闲时间大于timeBetweenEvictionRunsMillis
# 执行validationQuery检测连接是否有效
# 默认值为false
spring.datasource.druid.test-while-idle=true
#一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.druid.min-evictable-idle-time-millis=300000


#******************************
#**********数据源配置结束*********
#******************************

     3.2)在xproject-web模块自定义druid配置,并配置相关的Servlet,Filter

package com.powerlong.xproject.core.config.datasource.properties;

import lombok.Data;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;

@Data
@ToString
@ConfigurationProperties(prefix = "spring.datasource.druid")
public class DruidProperties {

    private String driverClassName;
    private String url;
    private String username;
    private String password;

    private int initialSize;
    private int minIdle;
    private int maxActive = 100;
    private long maxWait;
    private long timeBetweenEvictionRunsMillis;
    private long minEvictableIdleTimeMillis;
    private String validationQuery;
    private boolean testWhileIdle;
    private boolean testOnBorrow;
    private boolean testOnReturn;
    private boolean poolPreparedStatements;
    private int maxOpenPreparedStatements;
    private String filters;
}
package com.powerlong.xproject.core.config.datasource;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.powerlong.xproject.core.config.datasource.properties.DruidProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.SQLException;

@Configuration
@EnableConfigurationProperties({DruidProperties.class})
public class DruidDataSourceConfig {

    @Autowired
    private DruidProperties properties;

    @Bean
    @ConditionalOnMissingBean
    public DataSource druidDataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();

        druidDataSource.setDriverClassName(properties.getDriverClassName());
        druidDataSource.setUrl(properties.getUrl());
        druidDataSource.setUsername(properties.getUsername());
        druidDataSource.setPassword(properties.getPassword());
        druidDataSource.setInitialSize(properties.getInitialSize());
        druidDataSource.setMinIdle(properties.getMinIdle());
        druidDataSource.setMaxActive(properties.getMaxActive());
        druidDataSource.setMaxWait(properties.getMaxWait());
        druidDataSource.setTimeBetweenEvictionRunsMillis(properties.getTimeBetweenEvictionRunsMillis());
        druidDataSource.setMinEvictableIdleTimeMillis(properties.getMinEvictableIdleTimeMillis());
        druidDataSource.setValidationQuery(properties.getValidationQuery());
        druidDataSource.setTestWhileIdle(properties.isTestWhileIdle());
        druidDataSource.setTestOnBorrow(properties.isTestOnBorrow());
        druidDataSource.setTestOnReturn(properties.isTestOnReturn());
        druidDataSource.setPoolPreparedStatements(properties.isPoolPreparedStatements());
        druidDataSource.setMaxOpenPreparedStatements(properties.getMaxOpenPreparedStatements());
        try {
            druidDataSource.setFilters(properties.getFilters());
            druidDataSource.init();
        } catch (SQLException e) {
            //TODO 等待集成日志的时候
        }

        return druidDataSource;
    }

    /**
     * 注册Servlet信息, 配置监控视图,相当于WebServlet配置
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public ServletRegistrationBean druidServlet() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");

        //白名单:
        servletRegistrationBean.addInitParameter("allow","172.20.10.2");
        //IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page.
        servletRegistrationBean.addInitParameter("deny","192.168.6.73");
        //登录查看信息的账号密码, 用于登录Druid监控后台
        servletRegistrationBean.addInitParameter("loginUsername", "admin");
        servletRegistrationBean.addInitParameter("loginPassword", "admin");
        //是否能够重置数据.
        servletRegistrationBean.addInitParameter("resetEnable", "true");
        return servletRegistrationBean;

    }

    /**
     * 注册Filter信息, 监控拦截器 相当于Web Filter配置
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }
}

  3.3 启动

SpringBoot 搭建多模块的后台项目-SSM(二)将Druid连接池集成进项目_第1张图片

3.4) 访问http://localhost:8080/druid/index.html

SpringBoot 搭建多模块的后台项目-SSM(二)将Druid连接池集成进项目_第2张图片

你可能感兴趣的:(ssm)