● 需求:演示 Spring Boot 如何通过 jdbc+HikariDataSource 完成对 Mysql 操作 说明: HikariDataSource : 目前市面上非常优秀的数据源, 是 springboot2 默认数据源
-- 创建 spring_boot
DROP DATABASE IF EXISTS spring_boot; CREATE DATABASE spring_boot;
USE spring_boot;
-- 创建家居表
CREATE TABLE furn( `id` INT(11) PRIMARY KEY AUTO_INCREMENT, ## id
`name` VARCHAR(64) NOT NULL, ## 家居名
`maker` VARCHAR(64) NOT NULL, ## 厂商
`price` DECIMAL(11,2) NOT NULL, ## 价格
`sales` INT(11) NOT NULL, ## 销量
`stock` INT(11) NOT NULL, ## 库存
`img_path` VARCHAR(256) NOT NULL ## 照片路径
);
-- 初始化家居数据
INSERT INTO furn(`id` , `name` , `maker` , `price` , `sales` , `stock` , `img_path`) VALUES(NULL , ' 北 欧 风 格 小 桌 子 ' , ' 熊 猫 家 居 ' , 180 , 666 , 7 , 'assets/images/product-image/1.jpg');
INSERT INTO furn(`id` , `name` , `maker` , `price` , `sales` , `stock` , `img_path`) VALUES(NULL , ' 简 约 风 格 小 椅 子 ' , ' 熊 猫 家 居 ' , 180 , 666 , 7 , 'assets/images/product-image/2.jpg');
INSERT INTO furn(`id` , `name` , `maker` , `price` , `sales` , `stock` , `img_path`) VALUES(NULL , ' 典 雅 风 格 小 台 灯 ' , ' 蚂 蚁 家 居 ' , 180 , 666 , 7 , 'assets/images/product-image/3.jpg');
INSERT INTO furn(`id` , `name` , `maker` , `price` , `sales` , `stock` , `img_path`) VALUES(NULL , ' 温 馨 风 格 盆 景 架 ' , ' 蚂 蚁 家 居 ' , 180 , 666 , 7 , 'assets/images/product-image/4.jpg'); SELECT * FROM furn;
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-jdbcartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.49version>
dependency>
spring:
datasource:
#说明:如果没有指定useSSL=true,启动项目可能会爆红警告,环境问题
url: jdbc:mysql://127.0.0.1:3306/spring_boot?useSSL=true&useUnicode=true&characterEncoding=UTF-8
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
5.创建src\main\java\com\llp\springboot\bean\Furn.java
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Furn {
private Integer id;
private String name;
private String maker;
private BigDecimal price;
private Integer sales;
private Integer stock;
private String imgPath = "assets/images/product-image/1.jpg";
}
1.创建src\test\java\com\llp\springboot\ApplicationTests.java 测试类
/**
* 如何在springboot中开发测试类,需要引入如下的依赖
*
* org.springframework.boot
* spring-boot-starter-test
*
*/
@SpringBootTest
public class ApplicationTests {
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
public void contextLoads() {
BeanPropertyRowMapper<Furn> rowMapper = new BeanPropertyRowMapper(Furn.class);
List<Furn> furns = jdbcTemplate.query("select * from furn", rowMapper);
for (Furn furn : furns) {
System.out.println(furn);
}
System.out.println(jdbcTemplate.getDataSource().getClass());
}
}
可以看到springBoot默认使用的是HikariDataSource 数据源
https://github.com/alibaba/druid
https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98
HiKariCP: 目前市面上非常优秀的数据源, 是 springboot2 默认数据源
Druid: 性能优秀,Druid 提供性能卓越的连接池功能外【Java 基础】,还集成了 SQL 监 控,黑名单拦截等功能,强大的监控特性,通过 Druid 提供的监控功能,可以清楚知道连 接池和 SQL 的工作情况,所以根据项目需要,我们也要掌握 Druid 和 SpringBoot 整合
整合 Druid 到 Spring-Boot 方式
● 自定义方式
● 引入 starter 方式
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.17version>
dependency>
2.创建src\main\java\com\llp\springboot\config\DruidDataSourceConfig.java 配置类
@Configuration
public class DruidDataSourceConfig {
//编写方法,注入DruidDataSource
//为什么我们注入自己的DataSource , 默认的HiKariDatasource失效?
//1. 默认的数据源是如配置? @ConditionalOnMissingBean({ DataSource.class, XADataSource.class })
// 解读通过@ConditionalOnMissingBean({ DataSource.class}) 判断如果容器有DataSource Bean 就不注入默认的HiKariDatasource
@ConfigurationProperties("spring.datasource")
@Bean
public DataSource dataSource() {
//1. 配置了 @ConfigurationProperties("spring.datasource")
// 就可以读取到application.yml的配置,注意:我们需要将bean注入到spring ioc容器中、bean中提供get\set方法
//2. 我们就不需要调用DruidDataSource 对象的setXxx, 会自动关联
DruidDataSource druidDataSource = new DruidDataSource();
//druidDataSource.setUrl();
//druidDataSource.setUsername();
//druidDataSource.setPassword();
return druidDataSource;
}
}
在配置监控功能之前,我们先来看看如何配置 Druid 的监控页面
<servlet>
<servlet-name>DruidStatViewservlet-name>
<servlet-class>com.alibaba.druid.support.http.StatViewServletservlet-class>
<init-param>
<param-name>resetEnableparam-name>
<param-value>trueparam-value>
init-param>
<init-param>
<param-name>loginUsernameparam-name>
<param-value>druidparam-value>
init-param>
<init-param>
<param-name>loginPasswordparam-name>
<param-value>druidparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>DruidStatViewservlet-name>
<url-pattern>/druid/*url-pattern>
servlet-mapping>
1.在springBoot项目中,我们需要将StatViewServlet注入到容器中并配置登录的用户名和密码等信息
/**
*
*
* DruidStatView
* com.alibaba.druid.support.http.StatViewServlet
*
*
* resetEnable
* true
*
*
*
* loginUsername
* druid
*
*
*
* loginPassword
* druid
*
*
*
* DruidStatView
* /druid/*
*
* @return
*/
@Bean
public ServletRegistrationBean registrationBean(){
StatViewServlet statViewServlet = new StatViewServlet();
//配置监控页面的访问路径
ServletRegistrationBean registrationBean = new ServletRegistrationBean(statViewServlet, "/druid/*");
//配置登录用户名和密码,结合上面这段 init-param即配置servlet的初始化参数,key和其保持一致,value值我们可以自行定义
registrationBean.addInitParameter("loginUsername","llp");
registrationBean.addInitParameter("loginPassword","123");
return registrationBean;
}
2.启动完成测试http://localhost:8080/druid/login.html,我这里配置了拦截器匹配规则是/**,但是访问druid的监控是走的servlet不会走拦截器,因此可以直接访问
https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_StatFilter
1.那么如何在springboot项目中配置druid的sql监控功能呢,从上面可以看到我们需要在注入druidDataSource数据源对象的地方添加一个属性 druidDataSource.setFilters("stat");
@ConfigurationProperties("spring.datasource")
@Bean
public DataSource dataSource() throws SQLException {
//1. 配置了 @ConfigurationProperties("spring.datasource")
// 就可以读取到application.yml的配置,注意:我们需要将bean注入到spring ioc容器中、bean中提供get\set方法
//2. 我们就不需要调用DruidDataSource 对象的setXxx, 会自动关联
DruidDataSource druidDataSource = new DruidDataSource();
//配置sql监控
druidDataSource.setFilters("stat");
return druidDataSource;
}
2.模拟操作 DB 的请求
@Controller
@RequiredArgsConstructor
public class DruidSqlController {
private final JdbcTemplate jdbcTemplate;
@ResponseBody
@GetMapping("/sql")
public List<Furn> getFurnList(){
BeanPropertyRowMapper<Furn> furnBeanPropertyRowMapper = new BeanPropertyRowMapper<>(Furn.class);
List<Furn> furnList = jdbcTemplate.query("select * from furn", furnBeanPropertyRowMapper);
System.out.println(furnList);
return furnList;
}
}
3.我们来看下sql监控的效果
https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_%E9%85%8D%E7%BD%AEWebStatFilter
1.修改src\main\java\com\llp\springboot\config\DruidDataSourceConfig.java
/**
* 配置druid的web应用和url监控功能
*
* DruidWebStatFilter
* com.alibaba.druid.support.http.WebStatFilter
*
* exclusions
* *.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*
*
*
*
* DruidWebStatFilter
* /*
*
*/
@Bean
public FilterRegistrationBean webStatFilter(){
WebStatFilter webStatFilter = new WebStatFilter();
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(webStatFilter);
filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
return filterRegistrationBean;
}
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
/**
* 1.拦截器会先拦截controller的路径映射
* 2.如果找不到则去静态资源下查找
* 3.这里配置/**会拦截所有的请求,包括静态资源
* 4.这里不拦截静态资源,为什么直接写成 "/images/**","/css/**" ?
* 5.在SpringBoot中引入了spring-boot-starter-web 依赖,每个starter都会对于的自动装配
* 6.在WebProperties中配置了类路径映射,因此可以直接省略/static/
* private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
* "classpath:/resources/", "classpath:/static/", "classpath:/public/" };
*
*/
registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**").excludePathPatterns("/","/login","/upload.html","/upload","/images/**","/css/**","/sql");
}
}
3.重启项目,看看 Web 应用和 URI 监控页面
https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE-wallfilter
1.修改src\main\java\com\llp\springboot\config\DruidDataSourceConfig.java
@ConfigurationProperties("spring.datasource")
@Bean
public DataSource dataSource() throws SQLException {
//1. 配置了 @ConfigurationProperties("spring.datasource")
// 就可以读取到application.yml的配置,注意:我们需要将bean注入到spring ioc容器中、bean中提供get\set方法
//2. 我们就不需要调用DruidDataSource 对象的setXxx, 会自动关联
DruidDataSource druidDataSource = new DruidDataSource();
//stat:配置sql监控, wall:配置druid sql防火墙
druidDataSource.setFilters("stat,wall");
return druidDataSource;
}
2.完成测试,重启项目,看看 SQL 防火墙监控是否生效
注意:Durid 的Session 监控的是用户的系统不是druid的后台系统
https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98
@ConfigurationProperties("spring.datasource.druid")
3.这样我们之前配置的druid就失效了
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.1.17version>
dependency>
spring:
datasource:
#说明:如果没有指定useSSL=true,启动项目可能会爆红警告,环境问题
url: jdbc:mysql://127.0.0.1:3306/spring_boot?useSSL=true&useUnicode=true&characterEncoding=UTF-8
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
druid:
#配置druid和监控功能
stat-view-servlet:
enabled: true
login-username: llp
login-password: 123
reset-enable: false
#配置web监控
web-stat-filter:
enabled: true
#配置路径,如果没有设置默认为/*
url-pattern: /*
exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
filter:
#配置sql监控
stat:
#1000毫秒及以上就是满慢sql
slow-sql-millis: 1000
#启用慢查询log日志
log-slow-sql: true
enabled: true
#配置sql防火墙
wall:
#启用
enabled: true
config:
#不允许删除表的sql操作
drop-table-allow: false
#不孕执行select *的sql执行
select-all-column-allow: false
7.删除德鲁伊广告配置,新增配置类如下
/**
* 去除druid底部的广告配置类
*/
@Configuration
@ConditionalOnWebApplication
@AutoConfigureAfter(DruidDataSourceAutoConfigure.class)
@ConditionalOnProperty(name = "spring.datasource.druid.stat-view-servlet.enabled", havingValue = "true", matchIfMissing = true)
public class RemoveDruidOtherConfig {
/**
* 方法描述: 除去页面底部的广告
*/
@Bean
public FilterRegistrationBean removeDruidAdFilterBean(DruidStatProperties properties) {
final String filePath = "support/http/resources/js/common.js";
// 获取web监控页面的参数
DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();
// 提取common.js的配置路径
String pattern = config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*";
String jsPattern = pattern.replaceAll("\\*", "js/common.js");
//创建filter进行过滤
Filter filter = new Filter() {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest req, ServletResponse rep, FilterChain chain) throws IOException, ServletException {
chain.doFilter(req, rep);
// 重置缓冲区,响应头不会被重置
rep.resetBuffer();
// 获取common.js
String text = Utils.readFromResource(filePath);
// 正则替换, 除去底部的广告信息
text = text.replaceAll("
" , "");
text = text.replaceAll("powered.*?shrek.wang", "");
rep.getWriter().write(text);
}
@Override
public void destroy() {
}
};
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(filter);
registrationBean.addUrlPatterns(jsPattern);
return registrationBean;
}
}