一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - SpringBoot整合Mybatis使用Druid數(shù)據(jù)庫連接池

SpringBoot整合Mybatis使用Druid數(shù)據(jù)庫連接池

2021-04-08 14:53hongyangliao Java教程

這篇文章主要介紹了SpringBoot整合Mybatis使用Druid數(shù)據(jù)庫連接池,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!-- 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數(shù)據(jù)庫連接池 -->
   <dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>druid</artifactId>
     <version>1.0.26</version>
   </dependency>

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#數(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,代碼如下

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
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,代碼如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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配置方式的 <tx:annotation-driven />
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í)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://blog.csdn.net/BLUE5945/article/details/79203178

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精品国产区一区二区三区在线观看 | 第一次破学生处破 | 草逼的视频 | 成全动漫视频在线观看 | 精品国产乱码久久久久久免费 | 国产无套在线播放 | 国产欧美亚洲精品第一页青草 | 国产一卡2卡3卡四卡精品网 | 香蕉在线精品亚洲第一区 | 色777777女人色 | 久久亚洲精品专区蓝色区 | 91麻豆精品国产自产在线观看 | 国产麻豆麻豆 | 亚州日韩精品AV片无码中文 | 99re这里只有精品视频 | 欧美成人禁片在线观看俄罗斯 | 91一区二区在线观看精品 | 国产精品刺激好大好爽视频 | 午夜小视频免费观看 | 6969精品视频在线观看 | 国产精品不卡高清在线观看 | 高清国产精品久久久久 | 国产成人在线视频 | 双性受合不垅腿攻np | 天天综合天天综合 | 97操| 国产欧美日韩视频在线观看一区二区 | 国产精品免费综合一区视频 | 欧美视频在线一区 | 五月桃花网婷婷亚洲综合 | 国产精品九九热 | 亚洲 欧美 国产 综合 播放 | 公交车高h | 免费看一级a一片毛片 | 亚洲精品久久久WWW游戏好玩 | 91色香sxmv最网页版新地址 | 91禁漫| 天堂中文在线免费观看 | naruto堂同人本子汉化gg | jzzjlzz亚洲乱熟在线播放 | 成年人免费在线看的惊悚动作片 |