springboot2.0.4搭建ssm开发环境,driud连接池

基于springboot2.0.4搭建的ssm框架,使用的druid连接池

springboot2.0.4搭建ssm开发环境,driud连接池_第1张图片

 

springboot文档中说明:

springboot2.0.4搭建ssm开发环境,driud连接池_第2张图片

springboot2.0.4搭建ssm开发环境,driud连接池_第3张图片

pom文件添加一些其他依赖:



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




    org.springframework.boot
    spring-boot-starter-test
    test




    org.projectlombok
    lombok
    1.18.0
    provided





    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    1.3.2




    mysql
    mysql-connector-java




    com.alibaba
    druid
    1.1.10

对mybatis的配置:


    
        
        
    
    
        
         
        
    

application.properties配置:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&useUnicode=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

# 下面为连接池的补充设置,应用到上面所有数据源中
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
# 配置获取连接等待超时的时间
spring.datasource.maxWait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false


mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
mybatis.config-location=classpath:mybatis/mybatis-config.xml

#嵌入tomcat配置
spring.server.port=8080
#和CPU数
spring.server.acceptorThreadCount=4
spring.server.minSpareThreads=50
spring.server.maxSpareThreads=50
spring.server.maxThreads=1000
spring.server.maxConnections=10000
#10秒超时
spring.server.connectionTimeout=10000
spring.server.protocol=org.apache.coyote.http11.Http11Nio2Protocol
spring.server.redirectPort=443
spring.server.compression=on
#文件请求大小
spring.server.MaxFileSize=300MB
spring.server.MaxRequestSize=500MB

其中指明要使用的连接池类型;

自定义配置连接池:

/**
 * @author lishuo
 * @describe :
 * @datetime 2018/8/6 14:14
 */
@Configuration
public class DruidConfig {
    
    @Value("${spring.datasource.url}")
    private String dbUrl;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    @Value("${spring.datasource.driver-class-name}")
    private String driverClassName;

    @Value("${spring.datasource.initialSize}")
    private int initialSize;

    @Value("${spring.datasource.minIdle}")
    private int minIdle;

    @Value("${spring.datasource.maxActive}")
    private int maxActive;

    @Value("${spring.datasource.maxWait}")
    private int maxWait;

    @Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
    private int timeBetweenEvictionRunsMillis;

    @Value("${spring.datasource.minEvictableIdleTimeMillis}")
    private int minEvictableIdleTimeMillis;

    @Value("${spring.datasource.validationQuery}")
    private String validationQuery;

    @Value("${spring.datasource.testWhileIdle}")
    private boolean testWhileIdle;

    @Value("${spring.datasource.testOnBorrow}")
    private boolean testOnBorrow;

    @Value("${spring.datasource.testOnReturn}")
    private boolean testOnReturn;



    @Bean
    public DataSource druidDataSource() {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(dbUrl);
        datasource.setUsername(username);
        datasource.setPassword(password);
        datasource.setDriverClassName(driverClassName);
        datasource.setInitialSize(initialSize);
        datasource.setMinIdle(minIdle);
        datasource.setMaxActive(maxActive);
        datasource.setMaxWait(maxWait);
        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        datasource.setValidationQuery(validationQuery);
        datasource.setTestWhileIdle(testWhileIdle);
        datasource.setTestOnBorrow(testOnBorrow);
        datasource.setTestOnReturn(testOnReturn);
        return datasource;
    }
}

自定义的tomcat服务器:

/**
 * @author lishuo
 * @describe :
 * @datetime 2018/7/27 10:49
 */
@Configuration
public class TomcatConfig {

    @Value("${spring.server.port}")
    private String port;
    @Value("${spring.server.acceptorThreadCount}")
    private String acceptorThreadCount;
    @Value("${spring.server.minSpareThreads}")
    private String minSpareThreads;
    @Value("${spring.server.maxSpareThreads}")
    private String maxSpareThreads;
    @Value("${spring.server.maxThreads}")
    private String maxThreads;
    @Value("${spring.server.maxConnections}")
    private String maxConnections;
    @Value("${spring.server.protocol}")
    private String protocol;
    @Value("${spring.server.redirectPort}")
    private String redirectPort;
    @Value("${spring.server.compression}")
    private String compression;
    @Value("${spring.server.connectionTimeout}")
    private String connectionTimeout;

    @Value("${spring.server.MaxFileSize}")
    private String MaxFileSize;
    @Value("${spring.server.MaxRequestSize}")
    private String MaxRequestSize;

    /*
    * web服务器工厂,,用来创建tomcat服务器
    * */
    @Bean
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        tomcat.addConnectorCustomizers(new MyTomcatConnectionCustomizer());
        return tomcat;
    }

    /**
     * 文件上传大小设置
     * @return
     */
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        //  单个文件大小
        factory.setMaxFileSize(MaxFileSize);
        /// 总文件大小
        factory.setMaxRequestSize(MaxRequestSize);
        return factory.createMultipartConfig();
    }

    /**
     * 默认http连接
     */
    public class MyTomcatConnectionCustomizer implements TomcatConnectorCustomizer {

        public MyTomcatConnectionCustomizer() {
        }

        @Override
        public void customize(Connector connector) {
            connector.setPort(Integer.valueOf(port));
            connector.setAttribute("connectionTimeout", connectionTimeout);
            connector.setAttribute("acceptorThreadCount", acceptorThreadCount);
            connector.setAttribute("minSpareThreads", minSpareThreads);
            connector.setAttribute("maxSpareThreads", maxSpareThreads);
            connector.setAttribute("maxThreads", maxThreads);
            connector.setAttribute("maxConnections", maxConnections);
            connector.setAttribute("protocol", protocol);
            connector.setAttribute("redirectPort", "redirectPort");
            connector.setAttribute("compression", "compression");
        }
    }
}

说明一下 ServletWebServerFactory:

springboot2.0.4搭建ssm开发环境,driud连接池_第4张图片

因为要使用tomcat服务器,所以要使用TomcatServletWebServerFactory,它是对ServletWebServerFactory的一种实现,

springboot2.0.4搭建ssm开发环境,driud连接池_第5张图片

springboot2.0.4搭建ssm开发环境,driud连接池_第6张图片

 springboot2.0.4搭建ssm开发环境,driud连接池_第7张图片

springboot2.0.4搭建ssm开发环境,driud连接池_第8张图片

connector.setAttribute()方法会把属性放入hashmap中,已经默认有些配置了

日志管理:







    
        
            %d{yyyy-MM-dd HH:mm:ss.SSS} ----> [%thread] ---> %-5level %logger{50} - %msg%n
        

        
             %d{yyyy-MM-dd HH:mm:ss.SSS} ==== [%thread] ==== %-5level %logger{50} - %msg%n
        
    



    ${LOG_HOME}/${appName}.log
    
    ${LOG_HOME}/${appName}-%d{yyyy-MM-dd}-%i.log
    365
    
    100MB
    
    
    
    %d{yyyy-MM-dd HH:mm:ss.SSS} [ %thread ] - [ %-5level ] [ %logger{50} : %line ] - %msg%n
    





    
    



 

官网对logback的扩展,可以指定不同环境使用,暴漏一些属性等等,官方文档介绍:

springboot2.0.4搭建ssm开发环境,driud连接池_第9张图片

再之后启动主启动类就可以了 ,其余的controller,service,transaction注解什么的我就省略了,没什么区别。

别忘了配置mapper包的扫描,或者使用@mapper注解

springboot2.0.4搭建ssm开发环境,driud连接池_第10张图片

运行成功!!

 

 

 

 

 

 

 

你可能感兴趣的:(springboot2.x)