目录
学习网址:https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter
添加配置,不要最新的,进过测试SQL监控失效
pom.xml依赖
- <dependency>
- <groupId>com.alibabagroupId>
- <artifactId>druid-spring-boot-starterartifactId>
- <version>1.1.10version>
- dependency>
application.yml
- mybatis:
- mapper-locations: classpath:mappers/**/*.xml
- type-aliases-package: com.dengxiyan.springboot04.entity
- logging:
- level:com.dengxiyan.springboot04: debug
- server:
- port: 8080
- spring:
- application:
- name: springboot04
- datasource:
- driver-class-name: com.mysql.jdbc.Driver
- name: defaultDataSource
- mima: 123456
- url: jdbc:mysql://localhost:3306/mybatis_ssm?useUnicode=true&characterEncoding=UTF-8
- username: root
- type: com.alibaba.druid.pool.DruidDataSource
- druid:
- #2.连接池配置
- #初始化连接池的连接数量 大小,最小,最大
- initial-size: 5
- min-idle: 5
- max-active: 20
- #配置获取连接等待超时的时间
- max-wait: 60000
- #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
- time-between-eviction-runs-millis: 60000
- # 配置一个连接在池中最小生存的时间,单位是毫秒
- min-evictable-idle-time-millis: 30000
- validation-query: SELECT 1 FROM DUAL
- test-while-idle: true
- test-on-borrow: true
- test-on-return: false
- # 是否缓存preparedStatement,也就是PSCache 官方建议MySQL下建议关闭 个人建议如果想用SQL防火墙 建议打开
- pool-prepared-statements: true
- max-pool-prepared-statement-per-connection-size: 20
- # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
- filter:
- stat:
- merge-sql: true
- slow-sql-millis: 5000
- #3.基础监控配置
- web-stat-filter:
- enabled: true
- url-pattern: /*
- #设置不统计哪些URL
- exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
- session-stat-enable: true
- session-stat-max-count: 100
- freemarker:
- cache: false
- charset: utf-8
- expose-request-attributes: true
- expose-session-attributes: true
- suffix: .ftl
- template-loader-path: classpath:/templates/
- resources:
- static-locations: classpath:/static/**
- mvc:
- static-path-pattern: /static/**
- pagehelper:
- reasonable: true
- supportMethodsArguments: true
- page-size-zero: true
- helper-dialect: mysql
效果图

输入yil文件中对应的用户名及密码即可 进入首页

在我们访问地址之后 URL监控会有限显示

application.yml相关配置
- mybatis:
- mapper-locations: classpath:mappers/**/*.xml
- type-aliases-package: com.dengxiyan.springboot04.entity
- logging:
- level:com.dengxiyan.springboot04: debug
- server:
- port: 8080
- spring:
- application:
- name: springboot04
- datasource:
- driver-class-name: com.mysql.jdbc.Driver
- name: defaultDataSource
- mima: 123456
- url: jdbc:mysql://localhost:3306/mybatis_ssm?useUnicode=true&characterEncoding=UTF-8
- username: root
- type: com.alibaba.druid.pool.DruidDataSource
- druid:
- #2.连接池配置
- #初始化连接池的连接数量 大小,最小,最大
- initial-size: 5
- min-idle: 5
- max-active: 20
- #配置获取连接等待超时的时间
- max-wait: 60000
- #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
- time-between-eviction-runs-millis: 60000
- # 配置一个连接在池中最小生存的时间,单位是毫秒
- min-evictable-idle-time-millis: 30000
- validation-query: SELECT 1 FROM DUAL
- test-while-idle: true
- test-on-borrow: true
- test-on-return: false
- # 是否缓存preparedStatement,也就是PSCache 官方建议MySQL下建议关闭 个人建议如果想用SQL防火墙 建议打开
- pool-prepared-statements: true
- max-pool-prepared-statement-per-connection-size: 20
- # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
- filter:
- stat:
- merge-sql: true
- slow-sql-millis: 5000
- #3.基础监控配置
- web-stat-filter:
- enabled: true
- url-pattern: /*
- #设置不统计哪些URL
- exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
- session-stat-enable: true
- session-stat-max-count: 100
-
- freemarker:
- cache: false
- charset: utf-8
- expose-request-attributes: true
- expose-session-attributes: true
- suffix: .ftl
- template-loader-path: classpath:/templates/
- resources:
- static-locations: classpath:/static/**
- mvc:
- static-path-pattern: /static/**
- redis:
- host: 192.168.230.129
- password: xiaodeng_redis
- port: 6379
- database: 0
- pagehelper:
- reasonable: true
- supportMethodsArguments: true
- page-size-zero: true
- helper-dialect: mysql
配置类RedisConfig.java
- package com.dengxiyan.springboot04.config;
-
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.data.redis.connection.RedisConnectionFactory;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
- import org.springframework.data.redis.serializer.StringRedisSerializer;
-
-
- @Configuration
- public class RedisConfig {
- @Bean
- public RedisTemplate<String,Object> getRedisTemplate(RedisConnectionFactory connectionFactory){
- RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
-
- redisTemplate.setKeySerializer(new StringRedisSerializer());
- redisTemplate.setHashKeySerializer(new StringRedisSerializer());
- redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
- redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
- // redisTemplate.afterPropertiesSet();
-
- redisTemplate.setConnectionFactory(connectionFactory);
- return redisTemplate;
- }
- }
service层ClazzBizImpl.java使用
- @Autowired
- private RedisTemplate<String,Object> redisTemplate;
-
- @Override
- public List<Clazz> listPager(Clazz clazz, PageBean pageBean) {
- List<Clazz> clzs = clazzMapper.listPager(clazz);
- redisTemplate.opsForValue().set("clz:1",clzs.get(0));
- redisTemplate.opsForValue().set("clzs",clzs);
- // redisTemplate.opsForHash().entries()
- return clzs;
- }
测试时有云服务器的可用云服务器,没有的打开虚拟机即可

redisConfig.java
- package com.dengxiyan.springboot04.config;
-
- import org.springframework.cache.annotation.EnableCaching;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.data.redis.cache.RedisCacheConfiguration;
- import org.springframework.data.redis.cache.RedisCacheManager;
- import org.springframework.data.redis.connection.RedisConnectionFactory;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
- import org.springframework.data.redis.serializer.RedisSerializationContext;
- import org.springframework.data.redis.serializer.StringRedisSerializer;
-
- import java.time.Duration;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Map;
- import java.util.Set;
-
-
- @EnableCaching
- @Configuration
- public class RedisConfig {
- private final int defaultExpireTime = 600;
-
- private final int userCacheExpireTime = 60;
-
- private final String userCacheName = "test";
-
- @Bean
- public RedisTemplate<String,Object> getRedisTemplate(RedisConnectionFactory connectionFactory){
- RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
-
- redisTemplate.setKeySerializer(new StringRedisSerializer());
- redisTemplate.setHashKeySerializer(new StringRedisSerializer());
- redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
- redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
- // redisTemplate.afterPropertiesSet();
-
- redisTemplate.setConnectionFactory(connectionFactory);
- return redisTemplate;
- }
-
- @Bean
- public RedisCacheManager redis(RedisConnectionFactory connectionFactory){
- RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig();
- // 设置缓存管理器管理的缓存的默认过期时间
- defaultCacheConfig = defaultCacheConfig.entryTtl(Duration.ofSeconds(defaultExpireTime))
- // 设置 key为string序列化
- .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
- // 设置value为json序列化
- .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()))
- // 不缓存空值
- .disableCachingNullValues();
-
- Set<String> cacheNames = new HashSet<>();
- cacheNames.add(userCacheName);
-
- // 对每个缓存空间应用不同的配置
- Map<String, RedisCacheConfiguration> configMap = new HashMap<>();
- configMap.put(userCacheName, defaultCacheConfig.entryTtl(Duration.ofSeconds(userCacheExpireTime)));
-
- RedisCacheManager cacheManager = RedisCacheManager.builder(connectionFactory)
- .cacheDefaults(defaultCacheConfig)
- .initialCacheNames(cacheNames)
- .withInitialCacheConfigurations(configMap)
- .build();
-
- return cacheManager;
- }
- }
测试注解式开发,修改代码如下
ClazzBiz.java
- package com.dengxiyan.springboot04.biz;
-
- import com.dengxiyan.springboot04.entity.Clazz;
- import com.dengxiyan.springboot04.util.PageBean;
-
- import java.util.List;
- import java.util.Map;
-
- public interface ClazzBiz {
- int deleteByPrimaryKey(Integer cid);
-
- int insert(Clazz record);
-
- int insertSelective(Clazz record);
-
- Clazz selectByPrimaryKey(Integer cid);
-
- int updateByPrimaryKeySelective(Clazz record);
-
- int updateByPrimaryKey(Clazz record);
-
- @Cacheable(value = "redis-cache-clzs-",key = "'clzid:'+#clazz.cid")
- List<Clazz> listPager(Clazz clazz, PageBean pageBean);
-
- List<Map> listMapPager(Clazz clazz, PageBean pageBean);
- }
