1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      SpringBoot整合Mybatis使用Druid數(shù)據(jù)庫連接池

      本文實例為大家分享了SpringBoot整合Mybatis使用Druid數(shù)據(jù)庫連接池的方法,具體內(nèi)容如下

      創(chuàng)新互聯(lián)主營新鄉(xiāng)網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶APP軟件開發(fā),新鄉(xiāng)h5小程序開發(fā)搭建,新鄉(xiāng)網(wǎng)站營銷推廣歡迎新鄉(xiāng)等地區(qū)企業(yè)咨詢

      在SpringBoot項目中,增加如下依賴

       
          
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.1.1
          
      
          
          
            mysql
            mysql-connector-java
            runtime
          
      
          
          
            com.alibaba
            druid
            1.0.26
          
      
      

      在resource目錄下,創(chuàng)建jdbc.properties配置文件,加入以下配置

      #數(shù)據(jù)庫配置
      spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false
      spring.datasource.username=admin
      spring.datasource.password=admin
      spring.datasource.driver-class-name=com.mysql.jdbc.Driver
      # 連接池配置
      # 初始化大小,最小,最大
      spring.datasource.initialSize=5 
      spring.datasource.minIdle=5 
      spring.datasource.maxActive=20 
      # 配置獲取連接等待超時的時間
      spring.datasource.maxWait=60000 
      # 配置間隔多久才進行一次檢測,檢測需要關(guān)閉的空閑連接,單位是毫秒
      spring.datasource.timeBetweenEvictionRunsMillis=60000 
      # 配置一個連接在池中最小生存的時間,單位是毫秒
      spring.datasource.minEvictableIdleTimeMillis=300000
      # 測試連接是否有效的sql
      spring.datasource.validationQuery=select 'x'
      # 建議配置為true,不影響性能,并且保證安全性
      # 申請連接的時候檢測,如果空閑時間大于timeBetweenEvictionRunsMillis,執(zhí)行validationQuery檢測連接是否有效
      spring.datasource.testWhileIdle=true
      # 申請連接時執(zhí)行validationQuery檢測連接是否有效
      spring.datasource.testOnBorrow=false
      # 歸還連接時執(zhí)行validationQuery檢測連接是否有效
      spring.datasource.testOnReturn=false
      # 要啟用PSCache,必須配置大于0,當(dāng)大于0時,poolPreparedStatements自動觸發(fā)修改為true
      spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
      # 屬性類型是字符串,通過別名的方式配置擴展插件,常用的插件有:
      # 監(jiān)控統(tǒng)計用的filter:stat
      # 日志用的filter:log4j
      # 防御sql注入的filter:wall
      spring.datasource.filters=stat,log4j,wall
      

      創(chuàng)建數(shù)據(jù)源配置類DataSourceConfig.java,代碼如下

      package com.liao.mybatis;
      
      import com.alibaba.druid.pool.DruidDataSource;
      import org.mybatis.spring.annotation.MapperScan;
      import org.slf4j.Logger;
      import org.slf4j.LoggerFactory;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.beans.factory.annotation.Value;
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;
      import org.springframework.context.annotation.PropertySource;
      import org.springframework.stereotype.Component;
      
      import javax.sql.DataSource;
      import java.sql.SQLException;
      
      /**
       * 數(shù)據(jù)源
       *
       * @author hongyangliao
       * @ClassName: DataSourceConfig
       * @Date 18-1-2 下午8:56
       */
      @Configuration
      @MapperScan("com.liao.**.dao")
      public class DataSourceConfig {
        private static final Logger logger = LoggerFactory.getLogger(DataSourceConfig.class);
      
        @Autowired
        private JdbcConfig jdbcConfig;
      
        @Bean
        @Primary //在同樣的DataSource中,首先使用被標(biāo)注的DataSource
        public DataSource dataSource() {
          DruidDataSource druidDataSource = new DruidDataSource();
          druidDataSource.setUrl(jdbcConfig.getUrl());
          druidDataSource.setUsername(jdbcConfig.getUserName());
          druidDataSource.setPassword(jdbcConfig.getPassword());
          druidDataSource.setInitialSize(jdbcConfig.getInitialSize());
          druidDataSource.setMinIdle(jdbcConfig.getMinIdle());
          druidDataSource.setMaxActive(jdbcConfig.getMaxActive());
          druidDataSource.setTimeBetweenEvictionRunsMillis(jdbcConfig.getTimeBetweenEvictionRunsMillis());
          druidDataSource.setMinEvictableIdleTimeMillis(jdbcConfig.getMinEvictableIdleTimeMillis());
          druidDataSource.setValidationQuery(jdbcConfig.getValidationQuery());
          druidDataSource.setTestWhileIdle(jdbcConfig.isTestWhileIdle());
          druidDataSource.setTestOnBorrow(jdbcConfig.isTestOnBorrow());
          druidDataSource.setTestOnReturn(jdbcConfig.isTestOnReturn());
          druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(jdbcConfig.getMaxPoolPreparedStatementPerConnectionSize());
          try {
            druidDataSource.setFilters(jdbcConfig.getFilters());
          } catch (SQLException e) {
            if (logger.isInfoEnabled()) {
              logger.info(e.getMessage(), e);
            }
          }
          return druidDataSource;
        }
      
      
        /**
         * Jdbc配置類
         *
         * @author hongyangliao
         * @ClassName: JdbcConfig
         * @Date 18-1-2 下午9:00
         */
        @PropertySource(value = "classpath:jdbc.properties")
        @Component
        public static class JdbcConfig {
          /**
           * 數(shù)據(jù)庫用戶名
           */
          @Value("${spring.datasource.username}")
          private String userName;
          /**
           * 驅(qū)動名稱
           */
          @Value("${spring.datasource.driver-class-name}")
          private String driverClass;
          /**
           * 數(shù)據(jù)庫連接url
           */
          @Value("${spring.datasource.url}")
          private String url;
          /**
           * 數(shù)據(jù)庫密碼
           */
          @Value("${spring.datasource.password}")
          private String password;
      
          /**
           * 數(shù)據(jù)庫連接池初始化大小
           */
          @Value("${spring.datasource.initialSize}")
          private int initialSize;
      
          /**
           * 數(shù)據(jù)庫連接池最小最小連接數(shù)
           */
          @Value("${spring.datasource.minIdle}")
          private int minIdle;
      
          /**
           * 數(shù)據(jù)庫連接池最大連接數(shù)
           */
          @Value("${spring.datasource.maxActive}")
          private int maxActive;
      
          /**
           * 獲取連接等待超時的時間
           */
          @Value("${spring.datasource.maxWait}")
          private long maxWait;
      
          /**
           * 多久檢測一次
           */
          @Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
          private long timeBetweenEvictionRunsMillis;
      
          /**
           * 連接在池中最小生存的時間
           */
          @Value("${spring.datasource.minEvictableIdleTimeMillis}")
          private long minEvictableIdleTimeMillis;
      
          /**
           * 測試連接是否有效的sql
           */
          @Value("${spring.datasource.validationQuery}")
          private String validationQuery;
      
          /**
           * 申請連接的時候檢測,如果空閑時間大于timeBetweenEvictionRunsMillis,檢測連接是否有效
           */
          @Value("${spring.datasource.testWhileIdle}")
          private boolean testWhileIdle;
      
          /**
           * 申請連接時,檢測連接是否有效
           */
          @Value("${spring.datasource.testOnBorrow}")
          private boolean testOnBorrow;
      
          /**
           * 歸還連接時,檢測連接是否有效
           */
          @Value("${spring.datasource.testOnReturn}")
          private boolean testOnReturn;
      
          /**
           * PSCache大小
           */
          @Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
          private int maxPoolPreparedStatementPerConnectionSize;
      
          /**
           * 通過別名的方式配置擴展插件
           */
          @Value("${spring.datasource.filters}")
          private String filters;
      
          public String getUserName() {
            return userName;
          }
      
          public void setUserName(String userName) {
            this.userName = userName;
          }
      
          public String getDriverClass() {
            return driverClass;
          }
      
          public void setDriverClass(String driverClass) {
            this.driverClass = driverClass;
          }
      
          public String getUrl() {
            return url;
          }
      
          public void setUrl(String url) {
            this.url = url;
          }
      
          public String getPassword() {
            return password;
          }
      
          public void setPassword(String password) {
            this.password = password;
          }
      
          public int getInitialSize() {
            return initialSize;
          }
      
          public void setInitialSize(int initialSize) {
            this.initialSize = initialSize;
          }
      
          public int getMinIdle() {
            return minIdle;
          }
      
          public void setMinIdle(int minIdle) {
            this.minIdle = minIdle;
          }
      
          public int getMaxActive() {
            return maxActive;
          }
      
          public void setMaxActive(int maxActive) {
            this.maxActive = maxActive;
          }
      
          public long getMaxWait() {
            return maxWait;
          }
      
          public void setMaxWait(long maxWait) {
            this.maxWait = maxWait;
          }
      
          public long getTimeBetweenEvictionRunsMillis() {
            return timeBetweenEvictionRunsMillis;
          }
      
          public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) {
            this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
          }
      
          public long getMinEvictableIdleTimeMillis() {
            return minEvictableIdleTimeMillis;
          }
      
          public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) {
            this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
          }
      
          public String getValidationQuery() {
            return validationQuery;
          }
      
          public void setValidationQuery(String validationQuery) {
            this.validationQuery = validationQuery;
          }
      
          public boolean isTestWhileIdle() {
            return testWhileIdle;
          }
      
          public void setTestWhileIdle(boolean testWhileIdle) {
            this.testWhileIdle = testWhileIdle;
          }
      
          public boolean isTestOnBorrow() {
            return testOnBorrow;
          }
      
          public void setTestOnBorrow(boolean testOnBorrow) {
            this.testOnBorrow = testOnBorrow;
          }
      
          public boolean isTestOnReturn() {
            return testOnReturn;
          }
      
          public void setTestOnReturn(boolean testOnReturn) {
            this.testOnReturn = testOnReturn;
          }
      
          public int getMaxPoolPreparedStatementPerConnectionSize() {
            return maxPoolPreparedStatementPerConnectionSize;
          }
      
          public void setMaxPoolPreparedStatementPerConnectionSize(int maxPoolPreparedStatementPerConnectionSize) {
            this.maxPoolPreparedStatementPerConnectionSize = maxPoolPreparedStatementPerConnectionSize;
          }
      
          public String getFilters() {
            return filters;
          }
      
          public void setFilters(String filters) {
            this.filters = filters;
          }
        }
      }
      
      

      創(chuàng)建Session工廠配置類SessionFactoryConfig.java,代碼如下

      package com.liao.mybatis;
      
      import java.io.IOException;
      
      import javax.sql.DataSource;
      
      import org.mybatis.spring.SqlSessionFactoryBean;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;
      import org.springframework.core.io.ClassPathResource;
      import org.springframework.transaction.annotation.EnableTransactionManagement;
      
      @Configuration
      @EnableTransactionManagement // 啟注解事務(wù)管理,等同于xml配置方式的 
      public class SessionFactoryConfig {
      
       /**
        * mybatis 配置路徑
        */
       private static String MYBATIS_CONFIG = "mybatis-config.xml";
      
       @Autowired
       private DataSource dataSource;
      
      
       /***
        * 創(chuàng)建sqlSessionFactoryBean
        * 并且設(shè)置configtion 如駝峰命名.等等
        * 設(shè)置mapper 映射路徑
        * 設(shè)置datasource數(shù)據(jù)源
        *
        * @Title: createSqlSessionFactoryBean
        * @author: hongyangliao
        * @Date: 18-1-3 上午9:52
        * @param
        * @return org.mybatis.spring.SqlSessionFactoryBean sqlSessionFactoryBean實例
        * @throws
        */
       @Bean(name = "sqlSessionFactory")
       public SqlSessionFactoryBean createSqlSessionFactoryBean() throws IOException {
        SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
        // 設(shè)置mybatis configuration 掃描路徑
        sqlSessionFactory.setConfigLocation(new ClassPathResource(MYBATIS_CONFIG));
        // 設(shè)置datasource
        sqlSessionFactory.setDataSource(dataSource);
        return sqlSessionFactory;
       }
      }
      

      以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。


      分享文章:SpringBoot整合Mybatis使用Druid數(shù)據(jù)庫連接池
      文章鏈接:http://ef60e0e.cn/article/poggoe.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        白沙| 凤冈县| 双桥区| 凭祥市| 含山县| 诏安县| 古交市| 丰顺县| 赤壁市| 灵石县| 宁远县| 临夏市| 迁安市| 彰武县| 独山县| 岢岚县| 唐河县| 东港市| 环江| 浙江省| 岗巴县| 涡阳县| 南宁市| 永顺县| 崇义县| 建德市| 宜黄县| 定州市| 宁国市| 台北市| 鸡西市| 原平市| 靖西县| 绥中县| 台北市| 噶尔县| 米脂县| 环江| 郴州市| 永宁县| 西和县|