zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    ES正常停止步骤
    有效的域名后缀列表
    sc.textFile("file:///home/spark/data.txt") Input path does not exist解决方法——submit 加参数 --master local 即可解决
    Spark技术在京东智能供应链预测的应用——按照业务进行划分,然后利用scikit learn进行单机训练并预测
    SaltStack介绍——SaltStack是一种新的基础设施管理方法开发软件,简单易部署,可伸缩的足以管理成千上万的服务器,和足够快的速度控制,与他们交流
    英特尔深度学习框架BigDL——a distributed deep learning library for Apache Spark
    宠物乘机的三种模式【转】
    机器学习特征表达——日期与时间特征做离散处理(数字到分类的映射),稀疏类分组(相似特征归档),创建虚拟变量(提取新特征) 本质就是要么多变少,或少变多
    域名解析举例
    什么是域名的TTL值? ——一条域名解析记录在DNS缓存服务器中的存留时间
  • 原文地址:https://www.cnblogs.com/wx_blog/p/10371840.html
Copyright © 2011-2022 走看看