当前位置: 首页 > news >正文

规划设计导航网站做相亲网站的安全责任

规划设计导航网站,做相亲网站的安全责任,wordpress作者专栏,装修土巴兔一、概述 上篇文章 系列九、SpringBoot MyBatis Redis实现分布式缓存 介绍了基于xml方式实现分布式缓存的效果#xff0c;当前大家使用的技术栈基本是springboot各种框架的组合#xff0c;而springboot显著的一个特点就是去xml配置#xff0c;那么在无xml配置的情形下 MyBatis Redis实现分布式缓存 介绍了基于xml方式实现分布式缓存的效果当前大家使用的技术栈基本是springboot各种框架的组合而springboot显著的一个特点就是去xml配置那么在无xml配置的情形下又该如何实现分布式缓存呢请看下面的代码实战 二、代码实战 2.1、分布式缓存相关的注解 基于注解方式的分布式缓存主要涉及到如下几个注解         1EnableCaching一般标注在配置类上表示开启Spring的缓存如果不加此注解的话Spring自带的缓存将不生效         2CacheConfig(cacheNames xxx)一般标注在service类上用于配置cache的名字建议以当前service类的全路径名作为cache的名字         3Cacheable一般标识在service层的查询方法上表示将一个方法的返回值缓存起来  默认情况下缓存的key就是方法的参数缓存的value就是方法的返回值,如果查询 方法无参数则会使用默认的key即SimpleKey []         4CachePut(key #department.id)一般加在service层的更新方法上update,当数据库中的数据更新后缓存中的数据也要跟着更新使用此注解可以将方法的返回值 自动更新到已经存在的key上         5CacheEvict一般加在service层的删除方法上当数据库中的数据删除后相关的缓存也会被删除使用该注解的时候也可以配置按照某种条件删除某种条件CacheEvict注解中的条件例如value、cacheNames、key、keyGenerator... 2.2、项目概览 2.3、pom dependencies!-- springboot --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-devtools/artifactId/dependency!-- 数据源 --dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.26/version/dependencydependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter/artifactIdversion2.3.1/version/dependencydependencygroupIdcom.alibaba/groupIdartifactIddruid-spring-boot-starter/artifactIdversion1.1.10/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId/dependency!-- 工具 --dependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.18.30/version/dependencydependencygroupIdcn.hutool/groupIdartifactIdhutool-all/artifactIdversion5.8.21/version/dependencydependencygroupIdorg.apache.commons/groupIdartifactIdcommons-lang3/artifactId/dependencydependencygroupIdorg.apache.commons/groupIdartifactIdcommons-collections4/artifactIdversion4.4/version/dependencydependencygroupIdcom.alibaba.fastjson2/groupIdartifactIdfastjson2/artifactIdversion2.0.25/version/dependency/dependencies 2.4、yml server:port: 9999spring:redis:host: port: 6379database: 0password: 123456datasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/20231018_redis?useSSLfalseuseUnicodetruecharacterEncodingUTF8serverTimezoneGMTusername: rootpassword: 123456mybatis:mapper-locations: classpath:mapper/*.xmltype-aliases-package: org.stat.entity.modelconfiguration:map-underscore-to-camel-case: truelogging:level:org:star:mapper: debug 2.5、主启动 /*** Author : 一叶浮萍归大海* Date: 2023/12/10 12:44* Description:**/ MapperScan(basePackages org.star.mapper) SpringBootApplication public class SpringbootRedisDistributeCacheAnnotationApplication {public static void main(String[] args) {SpringApplication.run(SpringbootRedisDistributeCacheAnnotationApplication.class, args);}} 2.6、MyRedisConfig /*** Author : 一叶浮萍归大海* Date: 2023/12/10 15:28* Description:* EnableCaching的作用开启Spring的缓存如果不加此注解的话Spring自带的缓存将不生效**/ EnableCaching Configuration public class MyRedisConfig {/*** RedisTemplate k v 序列化** param connectionFactory* return*/Beanpublic RedisTemplateObject, Object redisTemplate(LettuceConnectionFactory connectionFactory) {RedisTemplateObject, Object redisTemplate new RedisTemplate();redisTemplate.setConnectionFactory(connectionFactory);redisTemplate.setKeySerializer(RedisSerializer.string());redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());redisTemplate.setHashKeySerializer(RedisSerializer.string());redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());redisTemplate.afterPropertiesSet();return redisTemplate;}Beanpublic RedisCacheManager redisCacheManager(RedisTemplate redisTemplate) {RedisCacheWriter redisCacheWriter RedisCacheWriter.nonLockingRedisCacheWriter(redisTemplate.getConnectionFactory());RedisCacheConfiguration redisCacheConfiguration RedisCacheConfiguration.defaultCacheConfig()// 设置默认的超时时间为2小时.entryTtl(Duration.ofHours(2)).serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisTemplate.getValueSerializer()))// 设置默认的缓存前缀.prefixCacheNameWith(REDIS_CACHE_);return new RedisCacheManager(redisCacheWriter, redisCacheConfiguration);}} 2.7、DepartmentDO /*** Author : 一叶浮萍归大海* Date: 2023/12/10 12:48* Description:*/ Data AllArgsConstructor NoArgsConstructor Accessors(chain true) ToString(callSuper true) public class DepartmentDO implements Serializable {/*** 编号*/private Integer id;/*** 部门名称*/private String departmentName;} 2.8、DepartmentMapper.java /*** Author : 一叶浮萍归大海* Date: 2023/12/10 12:50* Description:*/ public interface DepartmentMapper {/*** 查询所有部门* return*/ListDepartmentDO listAllDepartment();/*** 根据id查询部门信息* param id* return*/DepartmentDO getDepartmentById(Integer id);/*** 根据id和departmentName查询部门* param id* param departmentName* return*/DepartmentDO getDepartment(Integer id,String departmentName);/*** 更新Department* param department* return*/int updateDepartment(DepartmentDO department);/*** 删除部门* param id*/void deleteDepartment(Integer id); } 2.9、DepartmentMapper.xml !DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtdmapper namespaceorg.star.mapper.DepartmentMapperselect idlistAllDepartment resultTypeorg.star.entity.model.DepartmentDOselect id,department_name from department/selectselect idgetDepartmentById resultTypeorg.star.entity.model.DepartmentDOselect id,department_name from department where id #{id}/selectselect idgetDepartment resultTypeorg.star.entity.model.DepartmentDOselect id,department_name from department where id #{id} and department_name #{departmentName}/selectupdate idupdateDepartment useGeneratedKeystrue keyPropertyidupdate department set department_name #{departmentName} where id #{id}selectKey resultTypeorg.star.entity.model.DepartmentDO orderAFTER keyPropertyidselect id,department_name from department where id #{id}/selectKey/updatedelete iddeleteDepartmentdelete from department where id #{id}/delete/mapper 2.10、DepartmentService /*** Author : 一叶浮萍归大海* Date: 2023/12/10 20:00* Description:* 基于注解的分布式缓存redis中key的生成规则${prefixCacheNameWith} _ ${cacheNames} _ ${key}* 说明prefixCacheNameWith为RedisCacheManager中配置的前缀* 举例* 1listAllDepartment REDIS_CACHE_org.star.service.DepartmentService::SimpleKey []* 2getDepartmentById REDIS_CACHE_org.star.service.DepartmentService::1* 3getDepartment REDIS_CACHE_org.star.service.DepartmentService::SimpleKey [1,研发部]**/ Service CacheConfig(cacheNames org.star.service.DepartmentService) public class DepartmentService {Resourceprivate DepartmentMapper departmentMapper;/*** return* Cacheable的作用* Cacheable注解一般加在查询方法上表示将一个方法的返回值缓存起来* 默认情况下缓存的key就是方法的参数缓存的value就是方法的返回值,如果查询* 方法无参数则会使用默认的key即SimpleKey []*/Cacheablepublic ListDepartmentDO listAllDepartment() {ListDepartmentDO departments departmentMapper.listAllDepartment();return departments;}/*** 对于只有一个参数的查询方法其key位id对应的值* param id* return*/Cacheablepublic DepartmentDO getDepartmentById(Integer id) {return departmentMapper.getDepartmentById(id);}/**** 对于有多个参数的查询方法其key为所有的参数如果想修改可以单独指定例如Cacheable(key #id)* param id* param departmentName* return*/Cacheablepublic DepartmentDO getDepartment(Integer id,String departmentName) {return departmentMapper.getDepartment(id,departmentName);}/*** CachePut作用* CachePut注解一般加在更新方法上update,当数据库中的数据更新后缓存中的数据也要跟着更新使用此注解可以将方法的返回值* 自动更新到已经存在的key上示例如下* param department* return*/CachePut(key #department.id)public DepartmentDO updateDepartment(DepartmentDO department) {departmentMapper.updateDepartment(department);return department;}/*** CacheEvict()作用* CacheEvict()注解一般加在删除方法上当数据库中的数据删除后相关的缓存也会被删除使用该注解的时候也可以配置按照某种条件* 删除某种条件CacheEvict注解中的条件例如value、cacheNames、key、keyGenerator...* param id*/CacheEvictpublic void deleteDepartment(Integer id) {departmentMapper.deleteDepartment(id);}} 2.11、DepartmentServiceTest /*** Author : 一叶浮萍归大海* Date: 2023/12/10 20:07* Description:*/ SpringBootTest public class DepartmentServiceTest {Resourceprivate DepartmentService departmentService;Testpublic void listAllDepartmentTest() {ListDepartmentDO departments1 departmentService.listAllDepartment();System.out.println(departments1 departments1);System.out.println();ListDepartmentDO departments2 departmentService.listAllDepartment();System.out.println(departments2 departments2);}Testpublic void getDepartmentByIdTest() {DepartmentDO department1 departmentService.getDepartmentById(1);System.out.println(department1 department1);System.out.println();DepartmentDO department2 departmentService.getDepartmentById(1);System.out.println(department2 department2);}Testpublic void getDepartmentTest() {DepartmentDO department1 departmentService.getDepartment(1, 研发部);System.out.println(department1 department1);System.out.println();DepartmentDO department2 departmentService.getDepartment(1, 研发部);System.out.println(department2 department2);}Testpublic void updateDepartmentTest() {DepartmentDO department new DepartmentDO().setDepartmentName(研发部444).setId(1);DepartmentDO updatedDepartment departmentService.updateDepartment(department);System.out.println(updatedDepartment updatedDepartment);}Testpublic void deleteDepartmentTest() {departmentService.deleteDepartment(1);}} 2.12、测试 2.12.1、listAllDepartmentTest 2.12.2、 getDepartmentByIdTest 2.12.3、getDepartmentTest 2.12.4、 updateDepartmentTest 2.12.5、 deleteDepartmentTest
http://www.huolong8.cn/news/45859/

相关文章:

  • 淄博网站建设推广网络推广软件赚钱
  • 免费制作网络商城网站wordpress文章末尾添加相关文章
  • 哪个网站做设计兼职不用压金域名手机网站源码
  • 北京网络网站建设福鼎手机网站建设
  • 济阳做网站技术网站源码wordpress
  • 支付网站搭建手机站是什么意思
  • 手袋 技术支持 东莞网站建设全民建网站
  • 自适应 网站开发申请带域名的免费空间
  • 网站跳出率是什么意思百度手机助手下载2022新版
  • 嘉兴网站制作网站建设rdm响应式网站开发
  • 新办公司网上核名在哪个网站做下载app浏览器
  • 一个网站如何工作流程游戏推广是做什么的
  • 织梦淘宝客网站爱站网官网关键词
  • Wordpress导航标签icon企业网站seo推广
  • 浦东做网站的公司哪些群体对网站开发有需求
  • 城乡建设部网站首页大连建设网站
  • 用老薛主机做网站wordpress卢松松自适应
  • 网站开发流程php网页开发基础实验总结
  • 网站收录在下降开源的网站后台程序
  • 公司建设网站费用会计怎么记seo网站优化流程
  • 网站建设建站公司ps做的网站图片好大
  • 网站打赏怎么做的被邀请做刷客会不会碰到钓鱼网站
  • 网站制作网站建设单位前端做网站需要学什么软件
  • 设计服务网站做网站浏览器
  • 巴塘网站建设尚义住房和城乡规划建设局网站
  • 免费微网站系统源码怎样用服务器做网站
  • 做网站技术人员林业建设协会网站
  • 网站建设与管理方向辽宁省城乡和建设厅网站
  • 陕西高端建设网站开封网站建设公司
  • 湖北省勘察设计协会网站微信公众平台登录界面