• springboot整合其它项目


    目录

    一,整合Druid

     

    二,整合Redis(非注解式缓存 )

    三,整合Redis(注解式缓存 )


    一,整合Druid

    学习网址:https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter

     添加配置,不要最新的,进过测试SQL监控失效

    pom.xml依赖

    1. <dependency>
    2. <groupId>com.alibabagroupId>
    3. <artifactId>druid-spring-boot-starterartifactId>
    4. <version>1.1.10version>
    5. dependency>

    application.yml

    1. mybatis:
    2. mapper-locations: classpath:mappers/**/*.xml
    3. type-aliases-package: com.dengxiyan.springboot04.entity
    4. logging:
    5. level:com.dengxiyan.springboot04: debug
    6. server:
    7. port: 8080
    8. spring:
    9. application:
    10. name: springboot04
    11. datasource:
    12. driver-class-name: com.mysql.jdbc.Driver
    13. name: defaultDataSource
    14. mima: 123456
    15. url: jdbc:mysql://localhost:3306/mybatis_ssm?useUnicode=true&characterEncoding=UTF-8
    16. username: root
    17. type: com.alibaba.druid.pool.DruidDataSource
    18. druid:
    19. #2.连接池配置
    20. #初始化连接池的连接数量 大小,最小,最大
    21. initial-size: 5
    22. min-idle: 5
    23. max-active: 20
    24. #配置获取连接等待超时的时间
    25. max-wait: 60000
    26. #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
    27. time-between-eviction-runs-millis: 60000
    28. # 配置一个连接在池中最小生存的时间,单位是毫秒
    29. min-evictable-idle-time-millis: 30000
    30. validation-query: SELECT 1 FROM DUAL
    31. test-while-idle: true
    32. test-on-borrow: true
    33. test-on-return: false
    34. # 是否缓存preparedStatement,也就是PSCache 官方建议MySQL下建议关闭 个人建议如果想用SQL防火墙 建议打开
    35. pool-prepared-statements: true
    36. max-pool-prepared-statement-per-connection-size: 20
    37. # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
    38. filter:
    39. stat:
    40. merge-sql: true
    41. slow-sql-millis: 5000
    42. #3.基础监控配置
    43. web-stat-filter:
    44. enabled: true
    45. url-pattern: /*
    46. #设置不统计哪些URL
    47. exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
    48. session-stat-enable: true
    49. session-stat-max-count: 100
    50. freemarker:
    51. cache: false
    52. charset: utf-8
    53. expose-request-attributes: true
    54. expose-session-attributes: true
    55. suffix: .ftl
    56. template-loader-path: classpath:/templates/
    57. resources:
    58. static-locations: classpath:/static/**
    59. mvc:
    60. static-path-pattern: /static/**
    61. pagehelper:
    62. reasonable: true
    63. supportMethodsArguments: true
    64. page-size-zero: true
    65. helper-dialect: mysql

    效果图

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

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

     

    二,整合Redis(非注解式缓存 )

    application.yml相关配置

    1. mybatis:
    2. mapper-locations: classpath:mappers/**/*.xml
    3. type-aliases-package: com.dengxiyan.springboot04.entity
    4. logging:
    5. level:com.dengxiyan.springboot04: debug
    6. server:
    7. port: 8080
    8. spring:
    9. application:
    10. name: springboot04
    11. datasource:
    12. driver-class-name: com.mysql.jdbc.Driver
    13. name: defaultDataSource
    14. mima: 123456
    15. url: jdbc:mysql://localhost:3306/mybatis_ssm?useUnicode=true&characterEncoding=UTF-8
    16. username: root
    17. type: com.alibaba.druid.pool.DruidDataSource
    18. druid:
    19. #2.连接池配置
    20. #初始化连接池的连接数量 大小,最小,最大
    21. initial-size: 5
    22. min-idle: 5
    23. max-active: 20
    24. #配置获取连接等待超时的时间
    25. max-wait: 60000
    26. #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
    27. time-between-eviction-runs-millis: 60000
    28. # 配置一个连接在池中最小生存的时间,单位是毫秒
    29. min-evictable-idle-time-millis: 30000
    30. validation-query: SELECT 1 FROM DUAL
    31. test-while-idle: true
    32. test-on-borrow: true
    33. test-on-return: false
    34. # 是否缓存preparedStatement,也就是PSCache 官方建议MySQL下建议关闭 个人建议如果想用SQL防火墙 建议打开
    35. pool-prepared-statements: true
    36. max-pool-prepared-statement-per-connection-size: 20
    37. # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
    38. filter:
    39. stat:
    40. merge-sql: true
    41. slow-sql-millis: 5000
    42. #3.基础监控配置
    43. web-stat-filter:
    44. enabled: true
    45. url-pattern: /*
    46. #设置不统计哪些URL
    47. exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
    48. session-stat-enable: true
    49. session-stat-max-count: 100
    50. freemarker:
    51. cache: false
    52. charset: utf-8
    53. expose-request-attributes: true
    54. expose-session-attributes: true
    55. suffix: .ftl
    56. template-loader-path: classpath:/templates/
    57. resources:
    58. static-locations: classpath:/static/**
    59. mvc:
    60. static-path-pattern: /static/**
    61. redis:
    62. host: 192.168.230.129
    63. password: xiaodeng_redis
    64. port: 6379
    65. database: 0
    66. pagehelper:
    67. reasonable: true
    68. supportMethodsArguments: true
    69. page-size-zero: true
    70. helper-dialect: mysql

    配置类RedisConfig.java

    1. package com.dengxiyan.springboot04.config;
    2. import org.springframework.context.annotation.Bean;
    3. import org.springframework.context.annotation.Configuration;
    4. import org.springframework.data.redis.connection.RedisConnectionFactory;
    5. import org.springframework.data.redis.core.RedisTemplate;
    6. import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
    7. import org.springframework.data.redis.serializer.StringRedisSerializer;
    8. @Configuration
    9. public class RedisConfig {
    10. @Bean
    11. public RedisTemplate<String,Object> getRedisTemplate(RedisConnectionFactory connectionFactory){
    12. RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
    13. redisTemplate.setKeySerializer(new StringRedisSerializer());
    14. redisTemplate.setHashKeySerializer(new StringRedisSerializer());
    15. redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    16. redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
    17. // redisTemplate.afterPropertiesSet();
    18. redisTemplate.setConnectionFactory(connectionFactory);
    19. return redisTemplate;
    20. }
    21. }

    service层ClazzBizImpl.java使用

    1. @Autowired
    2. private RedisTemplate<String,Object> redisTemplate;
    3. @Override
    4. public List<Clazz> listPager(Clazz clazz, PageBean pageBean) {
    5. List<Clazz> clzs = clazzMapper.listPager(clazz);
    6. redisTemplate.opsForValue().set("clz:1",clzs.get(0));
    7. redisTemplate.opsForValue().set("clzs",clzs);
    8. // redisTemplate.opsForHash().entries()
    9. return clzs;
    10. }

     测试时有云服务器的可用云服务器,没有的打开虚拟机即可

     

    三,整合Redis(注解式缓存 )

    redisConfig.java

    1. package com.dengxiyan.springboot04.config;
    2. import org.springframework.cache.annotation.EnableCaching;
    3. import org.springframework.context.annotation.Bean;
    4. import org.springframework.context.annotation.Configuration;
    5. import org.springframework.data.redis.cache.RedisCacheConfiguration;
    6. import org.springframework.data.redis.cache.RedisCacheManager;
    7. import org.springframework.data.redis.connection.RedisConnectionFactory;
    8. import org.springframework.data.redis.core.RedisTemplate;
    9. import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
    10. import org.springframework.data.redis.serializer.RedisSerializationContext;
    11. import org.springframework.data.redis.serializer.StringRedisSerializer;
    12. import java.time.Duration;
    13. import java.util.HashMap;
    14. import java.util.HashSet;
    15. import java.util.Map;
    16. import java.util.Set;
    17. @EnableCaching
    18. @Configuration
    19. public class RedisConfig {
    20. private final int defaultExpireTime = 600;
    21. private final int userCacheExpireTime = 60;
    22. private final String userCacheName = "test";
    23. @Bean
    24. public RedisTemplate<String,Object> getRedisTemplate(RedisConnectionFactory connectionFactory){
    25. RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
    26. redisTemplate.setKeySerializer(new StringRedisSerializer());
    27. redisTemplate.setHashKeySerializer(new StringRedisSerializer());
    28. redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    29. redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
    30. // redisTemplate.afterPropertiesSet();
    31. redisTemplate.setConnectionFactory(connectionFactory);
    32. return redisTemplate;
    33. }
    34. @Bean
    35. public RedisCacheManager redis(RedisConnectionFactory connectionFactory){
    36. RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig();
    37. // 设置缓存管理器管理的缓存的默认过期时间
    38. defaultCacheConfig = defaultCacheConfig.entryTtl(Duration.ofSeconds(defaultExpireTime))
    39. // 设置 key为string序列化
    40. .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
    41. // 设置value为json序列化
    42. .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()))
    43. // 不缓存空值
    44. .disableCachingNullValues();
    45. Set<String> cacheNames = new HashSet<>();
    46. cacheNames.add(userCacheName);
    47. // 对每个缓存空间应用不同的配置
    48. Map<String, RedisCacheConfiguration> configMap = new HashMap<>();
    49. configMap.put(userCacheName, defaultCacheConfig.entryTtl(Duration.ofSeconds(userCacheExpireTime)));
    50. RedisCacheManager cacheManager = RedisCacheManager.builder(connectionFactory)
    51. .cacheDefaults(defaultCacheConfig)
    52. .initialCacheNames(cacheNames)
    53. .withInitialCacheConfigurations(configMap)
    54. .build();
    55. return cacheManager;
    56. }
    57. }

    测试注解式开发,修改代码如下

    ClazzBiz.java

    1. package com.dengxiyan.springboot04.biz;
    2. import com.dengxiyan.springboot04.entity.Clazz;
    3. import com.dengxiyan.springboot04.util.PageBean;
    4. import java.util.List;
    5. import java.util.Map;
    6. public interface ClazzBiz {
    7. int deleteByPrimaryKey(Integer cid);
    8. int insert(Clazz record);
    9. int insertSelective(Clazz record);
    10. Clazz selectByPrimaryKey(Integer cid);
    11. int updateByPrimaryKeySelective(Clazz record);
    12. int updateByPrimaryKey(Clazz record);
    13. @Cacheable(value = "redis-cache-clzs-",key = "'clzid:'+#clazz.cid")
    14. List<Clazz> listPager(Clazz clazz, PageBean pageBean);
    15. List<Map> listMapPager(Clazz clazz, PageBean pageBean);
    16. }

     

  • 相关阅读:
    k8s资源对象(一)
    13、Callable介绍(Thread如何启动Callable)
    Hive(12):Hive的函数之自定义函数
    实用技能 | 推荐6款论文一键AI改写修改网站
    【日拱一卒行而不辍20220920】自制操作系统
    扬帆牧哲—跨境电商商标注册要注意的地方
    【爬虫】charles手机抓包环境设置(设置系统证书)
    代码随想录算法训练营day52||674. 最长连续递增序列||718. 最长重复子数组||1143.最长公共子序列
    CSS——图标字体
    puppeteer在mac和linux上表现不一致的问题记录
  • 原文地址:https://blog.csdn.net/weixin_66202611/article/details/127674257