一:整合Mysql+Druid
主要依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<!--加這個表示該依賴在編譯的時候不會使用到核蘸,只有在運行的時候才會使用到-->
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
</dependencies>
編寫配置類
spring:
datasource:
druid:
username: root
password: root123456
jdbcUrl: jdbc:mysql://zzw.study:3306/boot-study
driverClassName: com.mysql.cj.jdbc.Driver
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
filters: stat,wall
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
# 開啟慢sql記錄功能
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
編寫自定義數(shù)據(jù)源屬性配置類
package com.study.jdbcanddruid.config;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "spring.datasource.druid")
@Getter
@Setter
public class DruidConfigProperties {
private String jdbcUrl;
private String driverClassName;
private Integer initialSize;
private Integer maxActive;
private Integer minIdle;
private long maxWait;
private boolean poolPreparedStatements;
public String filters;
private String username;
private String password;
}
編寫druid配置類
package com.study.jdbcanddruid.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.sql.SQLException;
@Configuration
@EnableConfigurationProperties(value = DruidConfigProperties.class)
@SuppressWarnings("all")
public class DruidConfig {
@Autowired
private DruidConfigProperties druidConfigProperties;
@Bean(initMethod = "init")
public DruidDataSource dataSource() throws SQLException {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUsername(druidConfigProperties.getUsername());
dataSource.setPassword(druidConfigProperties.getPassword());
dataSource.setUrl(druidConfigProperties.getJdbcUrl());
dataSource.setDriverClassName(druidConfigProperties.getDriverClassName());
dataSource.setInitialSize(druidConfigProperties.getInitialSize());
dataSource.setMinIdle(druidConfigProperties.getMinIdle());
dataSource.setMaxActive(druidConfigProperties.getMaxActive());
dataSource.setMaxWait(druidConfigProperties.getMaxWait());
dataSource.setFilters(druidConfigProperties.getFilters());
dataSource.setPoolPreparedStatements(druidConfigProperties.isPoolPreparedStatements());
return dataSource;
}
/**
* 配置druid管理后臺的servlet
* @return
*/
@Bean
public ServletRegistrationBean statViewSerlvet() {
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
// 添加IP白名單
bean.addInitParameter("allow", "127.0.0.1");
// 添加IP黑名單,當白名單和黑名單重復時啸驯,黑名單優(yōu)先級更高
bean.addInitParameter("deny", "192.168.25.123");
// 添加控制臺管理用戶
bean.addInitParameter("loginUsername", "admin");
bean.addInitParameter("loginPassword", "123456");
// 是否能夠重置數(shù)據(jù)
bean.addInitParameter("resetEnable", "false");
return bean;
}
@Bean
public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean frb = new FilterRegistrationBean(new WebStatFilter());
// 添加過濾規(guī)則
frb.addUrlPatterns("/*");
// 忽略過濾格式
frb.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*,");
return frb;
}
}
測試 編寫controller
package com.study.jdbcanddruid.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
@RestController
public class DemoController {
@Autowired
private JdbcTemplate jdbcTemplate;
@GetMapping("/test01")
public Object getTask() {
List<Map<String, Object>> maps = jdbcTemplate
.queryForList("select * from test limit 2");
return maps;
}
}
實用druid數(shù)據(jù)監(jiān)控功能:
二:整合MyBatis
添加依賴
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
配置mybaits
mybatis:
configuration:
map-underscore-to-camel-case: true
mapper-locations: classpath:/mapper/*.xml