共计 2231 个字符,预计需要花费 6 分钟才能阅读完成。
引入 starter
修改 pom.xml 文件, 整合 mybatis 需要如下的包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!–mybatis 的 starter–>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</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.1.12</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
durid 连接池配置
修改 yml 文件
spring:
datasource:
username: root
password: root
url: jdbc:mysql://127.0.0.1:3306/sff_test
driver-class-name: com.mysql.jdbc.Driver
# 指定自己使用的数据源
type: com.alibaba.druid.pool.DruidDataSource
# DruidDataSource 其他属性配置
druid:
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
filter:
stat:
enabled: true
log-slow-sql: true
wall:
enabled: true
注入 duird 配置
@Configurable
public class DruidConfig {
@Bean
@ConfigurationProperties(“spring.datasource.druid”)
public DataSource dataSourceTwo() {
return DruidDataSourceBuilder.create().build();
}
/**
* 配置一个管理后台的 Servlet
*
* @return
*/
@Bean
public ServletRegistrationBean statViewServlet() {
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), “/druid/*”);
Map<String, String> initParams = new HashMap<>();
initParams.put(“loginUsername”, “admin”);
initParams.put(“loginPassword”, “admin”);
initParams.put(“allow”, “”);// 默认就是允许所有访问
bean.setInitParameters(initParams);
return bean;
}
/**
* 配置一个 web 监控的 filter
*
* @return
*/
@Bean
public FilterRegistrationBean webStatFilter() {
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(new WebStatFilter());
Map<String, String> initParams = new HashMap<>();
// 通过 localhost:8080/druid/ 可以访问到监控后台
initParams.put(“exclusions”, “*.js,*.css,/druid/*”);
bean.setInitParameters(initParams);
bean.setUrlPatterns(Arrays.asList(“/*”));
return bean;
}
}