SpringBoot整合Mybatis使用Druid数据库连接池

时间:2022-11-15 15:10:38

本文实例为大家分享了SpringBoot整合Mybatis使用Druid数据库连接池的方法,具体内容如下

在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数据库连接池 -->
   <dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>druid</artifactId>
     <version>1.0.26</version>
   </dependency>

在resource目录下,创建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
#数据库配置
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,代码如下

 

?
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;
 
/**
 * 数据源
 *
 * @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,代码如下

?
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 // 启注解事务管理,等同于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;
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

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