91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

SpringBoot整合Mybatis使用Druid數據庫連接池

發布時間:2020-10-25 11:46:31 來源:腳本之家 閱讀:155 作者:hongyangliao 欄目:編程語言

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

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

 <!-- spring mybatis -->
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>1.1.1</version>
    </dependency>

    <!-- mysql -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <scope>runtime</scope>
    </dependency>

    <!-- druid數據庫連接池 -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.0.26</version>
    </dependency>

在resource目錄下,創建jdbc.properties配置文件,加入以下配置

#數據庫配置
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 
# 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒
spring.datasource.timeBetweenEvictionRunsMillis=60000 
# 配置一個連接在池中最小生存的時間,單位是毫秒
spring.datasource.minEvictableIdleTimeMillis=300000
# 測試連接是否有效的sql
spring.datasource.validationQuery=select 'x'
# 建議配置為true,不影響性能,并且保證安全性
# 申請連接的時候檢測,如果空閑時間大于timeBetweenEvictionRunsMillis,執行validationQuery檢測連接是否有效
spring.datasource.testWhileIdle=true
# 申請連接時執行validationQuery檢測連接是否有效
spring.datasource.testOnBorrow=false
# 歸還連接時執行validationQuery檢測連接是否有效
spring.datasource.testOnReturn=false
# 要啟用PSCache,必須配置大于0,當大于0時,poolPreparedStatements自動觸發修改為true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
# 屬性類型是字符串,通過別名的方式配置擴展插件,常用的插件有:
# 監控統計用的filter:stat
# 日志用的filter:log4j
# 防御sql注入的filter:wall
spring.datasource.filters=stat,log4j,wall

創建數據源配置類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;

/**
 * 數據源
 *
 * @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中,首先使用被標注的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 {
    /**
     * 數據庫用戶名
     */
    @Value("${spring.datasource.username}")
    private String userName;
    /**
     * 驅動名稱
     */
    @Value("${spring.datasource.driver-class-name}")
    private String driverClass;
    /**
     * 數據庫連接url
     */
    @Value("${spring.datasource.url}")
    private String url;
    /**
     * 數據庫密碼
     */
    @Value("${spring.datasource.password}")
    private String password;

    /**
     * 數據庫連接池初始化大小
     */
    @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 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;
    }
  }
}

創建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 // 啟注解事務管理,等同于xml配置方式的 <tx:annotation-driven />
public class SessionFactoryConfig {

 /**
  * mybatis 配置路徑
  */
 private static String MYBATIS_CONFIG = "mybatis-config.xml";

 @Autowired
 private DataSource dataSource;


 /***
  * 創建sqlSessionFactoryBean
  * 并且設置configtion 如駝峰命名.等等
  * 設置mapper 映射路徑
  * 設置datasource數據源
  *
  * @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();
  // 設置mybatis configuration 掃描路徑
  sqlSessionFactory.setConfigLocation(new ClassPathResource(MYBATIS_CONFIG));
  // 設置datasource
  sqlSessionFactory.setDataSource(dataSource);
  return sqlSessionFactory;
 }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

漳州市| 法库县| 西华县| 江川县| 元阳县| 布拖县| 盘锦市| 宁陕县| 偃师市| 崇信县| 湖州市| 西林县| 镇远县| 灯塔市| 万荣县| 蕲春县| 资阳市| 阿图什市| 吕梁市| 新田县| 江达县| 行唐县| 深州市| 准格尔旗| 青冈县| 海林市| 威宁| 安平县| 晴隆县| 四平市| 互助| 濮阳县| 平潭县| 鄂托克前旗| 靖安县| 无棣县| 施甸县| 黎城县| 武平县| 弥渡县| 涿州市|