• springboot+Druid+mybatis整合


    一、添加Druid、MySQL连接池、mybatis依赖

     <!--整合Druid-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.11</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.46</version>
            </dependency>
    
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.3.2</version>
            </dependency>
    View Code

    二、配置DruidDataSourceConfig

    @Configuration
    public class DruidDataSourceConfigurer {
    
        @Value("${spring.datasource.url}")
        private String dbUrl;
        @Value("${spring.datasource.username}")
        private String username;
        @Value("${spring.datasource.password}")
        private String password;
        @Value("${spring.datasource.driverClassName}")
        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;
        @Value("${spring.datasource.poolPreparedStatements}")
        private boolean poolPreparedStatements;
        @Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
        private int maxPoolPreparedStatementPerConnectionSize;
        @Value("${spring.datasource.filters}")
        private String filters;
        @Value("${spring.datasource.connectionProperties}")
        private String connectionProperties;
        @Value("${spring.datasource.useGlobalDataSourceStat}")
        private boolean useGlobalDataSourceStat;
    
        /**
         * @Bean 创建一个实例
         * @Primary  在同样的DataSource中,首先使用被标注的DataSource
         * @return
         */
        @Bean
        @Primary
        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.setTestWhileIdle(testWhileIdle);
            datasource.setTestOnBorrow(testOnBorrow);
            datasource.setTestOnReturn(testOnReturn);
            datasource.setPoolPreparedStatements(poolPreparedStatements);
            datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
            datasource.setUseGlobalDataSourceStat(useGlobalDataSourceStat);
            try {
                datasource.setFilters(filters);
            } catch (SQLException e) {
                System.err.println("druid configuration initialization filter: "+ e);
            }
            datasource.setConnectionProperties(connectionProperties);
            return datasource;
        }
    
    
    
    }
    View Code

    三、配置DruidStatFilter

    @WebFilter(filterName = "druidWebStatFilter",
                urlPatterns = "/*",
                initParams = {
                    //定义可以忽略的资源文件
                    @WebInitParam(name="exclusion",value = "*.js,*gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")
                })
    public class DruidStatFilter extends WebStatFilter {
    }
    View Code

    四、application.porperties

    spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
    #连接池配置
    spring.datasource.url=jdbc:mysql://localhost:3306/cloud01??useUnicode=true&characterEncoding=utf8&useSSL=false
    spring.datasource.username=root
    spring.datasource.password=123456
    spring.datasource.driverClassName=com.mysql.jdbc.Driver
    #初始化大小
    spring.datasource.initialSize=5
    #最小连接数
    spring.datasource.minIdle=5
    #最大连接数
    spring.datasource.maxActive=20
    #连接等待超时时间
    spring.datasource.maxWait=60000
    #配置隔多久进行一次检测(检测可以关闭的空闲连接),检测需要关闭的空闲连接,单位是毫秒
    spring.datasource.timeBetweenEvictionRunsMillis=60000
    #配置连接在池中的最小生存时间
    spring.datasource.minEvictableIdleTimeMillis=300000
    #校验sql、oracle配置,如果不配置validationQuery,则下面三项无效
    spring.datasource.validationQuery=SELECT 1 FROM DUAL
    spring.datasource.testWhileIdle=true
    spring.datasource.testOnBorrow=false
    spring.datasource.testOnReturn=false
    # 打开PSCache,并且指定每个连接上PSCache的大小
    spring.datasource.poolPreparedStatements=true
    spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
    # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
    spring.datasource.filters=stat,wall,slf4j
    # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
    spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
    #合并多个DruidDataSource的监控数据
    spring.datasource.useGlobalDataSourceStat=true
    
    #mybatis的配置文件
    mybatis.config-location=classpath:/mybatis-config.xml
    mybatis.mapper-locations=classpath:/mapper/*.xml
    View Code

    五、mybatis-config.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        <!--<settings>-->
            <!--<setting name="cacheEnabled" value="true"/>-->
        <!--</settings>-->
        <typeAliases>
            <typeAlias type="com.itwx.druid.model.Dept" alias="Dept"/>
        </typeAliases>
    
    </configuration>
    View Code

    六、主启动类

    @SpringBootApplication
    //该注解使Servlet生效
    @ServletComponentScan
    public class SpringbootDruidApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringbootDruidApplication.class, args);
        }
    
        /**使用以下注解方式可以忽略DruidDataSourceConfigurer配置*/
    //    @Bean("druidDataSource")
    //    @ConfigurationProperties(prefix = "spring.datasource")
    //    public DataSource druidDataSource(){
    //        return new DruidDataSource();
    //    }
    
    }
    View Code

    git源码地址:https://gitee.com/wx-git/springboot

  • 相关阅读:
    TDH-hbase shell 常用命令
    WebService之基于REST机制的实现实例(Java版)
    大牛地址
    Solr的SolrCloud与Master-slave主从模式对比
    solr亿万级索引优化实践-自动生成UUID
    Solr查询中涉及到的Cache使用及相关的实现【转】
    05-spectral 图机器学习之谱分解
    03-motifs 图机器学习之motif和结构角色
    04-communities 图机器学习之社区
    02-gnp-smallworld 图机器学习之最小世界
  • 原文地址:https://www.cnblogs.com/wx_blog/p/10371840.html
Copyright © 2020-2023  润新知