网站优化的主要任务重庆网站推广平台

张小明 2026/1/10 2:04:01
网站优化的主要任务,重庆网站推广平台,wordpress邮件配置,上海平台网站建设报价Spring Boot 整合 Redis 注解实现简单 CRUD 可以关注#xff1a;小坏说Java 公众号 零基础全栈开发Java微服务版本实战-后端-前端-运维-实战企业级三个实战项目 一、项目搭建 零基础全栈开发Java微服务版本实战-后端-前端-运维-实战企业级三个实战项目 1.1 添加依赖 小坏说Java 公众号零基础全栈开发Java微服务版本实战-后端-前端-运维-实战企业级三个实战项目一、项目搭建零基础全栈开发Java微服务版本实战-后端-前端-运维-实战企业级三个实战项目1.1 添加依赖!-- pom.xml --dependencies!-- Spring Boot Web --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!-- Spring Boot Redis --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId/dependency!-- Lombok --dependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optional/dependency/dependencies1.2 配置文件# application.ymlspring:redis:host:localhostport:6379database:0server:port:8080二、实体类零基础全栈开发Java微服务版本实战-后端-前端-运维-实战企业级三个实战项目packagecom.example.entity;importlombok.Data;importorg.springframework.data.annotation.Id;importorg.springframework.data.redis.core.RedisHash;importorg.springframework.data.redis.core.index.Indexed;importjava.io.Serializable;/** * RedisHash: 声明实体类value指定Redis中的key前缀 * 存储格式: user:{id} */DataRedisHash(user)publicclassUserimplementsSerializable{IdprivateLongid;// 主键IndexedprivateStringusername;// 创建索引可以按username查询privateStringemail;privateIntegerage;}三、Repository 接口packagecom.example.repository;importcom.example.entity.User;importorg.springframework.data.repository.CrudRepository;importorg.springframework.stereotype.Repository;importjava.util.List;importjava.util.Optional;/** * CrudRepository 提供基本的CRUD方法 * 可以根据方法名自动生成查询 */RepositorypublicinterfaceUserRepositoryextendsCrudRepositoryUser,Long{// 根据用户名查询精确匹配ListUserfindByUsername(Stringusername);// 根据邮箱查询OptionalUserfindByEmail(Stringemail);// 根据年龄范围查询ListUserfindByAgeBetween(IntegerminAge,IntegermaxAge);// 删除指定用户名的用户LongdeleteByUsername(Stringusername);}四、Service 层packagecom.example.service;importcom.example.entity.User;importcom.example.repository.UserRepository;importlombok.extern.slf4j.Slf4j;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.cache.annotation.*;importorg.springframework.stereotype.Service;importjava.util.List;importjava.util.Optional;Slf4jServiceCacheConfig(cacheNamesuser)// 类级别缓存配置publicclassUserService{AutowiredprivateUserRepositoryuserRepository;/** * Cacheable: 查询缓存 * 1. 先查缓存有则直接返回 * 2. 无则执行方法将结果存入缓存 */Cacheable(key#id)publicUserfindById(Longid){log.info(查询数据库用户ID: {},id);returnuserRepository.findById(id).orElse(null);}/** * 查询所有用户 */publicListUserfindAll(){return(ListUser)userRepository.findAll();}/** * 根据用户名查询 */publicListUserfindByUsername(Stringusername){returnuserRepository.findByUsername(username);}/** * CachePut: 更新缓存 * 每次都会执行方法并将结果更新到缓存 */CachePut(key#user.id)publicUsersave(Useruser){log.info(保存用户: {},user.getUsername());returnuserRepository.save(user);}/** * CacheEvict: 删除缓存 * 方法执行后删除指定key的缓存 */CacheEvict(key#id)publicvoiddeleteById(Longid){log.info(删除用户ID: {},id);userRepository.deleteById(id);}/** * 更新用户 */Caching(putCachePut(key#user.id),evictCacheEvict(keylist)// 清除列表缓存)publicUserupdate(Useruser){log.info(更新用户: {},user.getId());returnuserRepository.save(user);}/** * 缓存用户列表 */Cacheable(keylist)publicListUserfindAllWithCache(){log.info(查询所有用户带缓存);return(ListUser)userRepository.findAll();}}五、Controller 层零基础全栈开发Java微服务版本实战-后端-前端-运维-实战企业级三个实战项目packagecom.example.controller;importcom.example.entity.User;importcom.example.service.UserService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.*;importjava.util.List;RestControllerRequestMapping(/api/users)publicclassUserController{AutowiredprivateUserServiceuserService;// 新增用户PostMappingpublicUsercreateUser(RequestBodyUseruser){returnuserService.save(user);}// 根据ID查询用户GetMapping(/{id})publicUsergetUserById(PathVariableLongid){returnuserService.findById(id);}// 查询所有用户GetMappingpublicListUsergetAllUsers(){returnuserService.findAllWithCache();}// 根据用户名查询GetMapping(/search)publicListUsergetUserByUsername(RequestParamStringusername){returnuserService.findByUsername(username);}// 更新用户PutMapping(/{id})publicUserupdateUser(PathVariableLongid,RequestBodyUseruser){user.setId(id);returnuserService.update(user);}// 删除用户DeleteMapping(/{id})publicStringdeleteUser(PathVariableLongid){userService.deleteById(id);return删除成功;}}六、主启动类零基础全栈开发Java微服务版本实战-后端-前端-运维-实战企业级三个实战项目packagecom.example;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cache.annotation.EnableCaching;SpringBootApplicationEnableCaching// 开启缓存支持publicclassApplication{publicstaticvoidmain(String[]args){SpringApplication.run(Application.class,args);}}七、测试示例7.1 使用 Postman 测试1. 新增用户POST http://localhost:8080/api/users Content-Type: application/json { username: 张三, email: zhangsanexample.com, age: 25 }2. 查询用户GET http://localhost:8080/api/users/13. 查询所有用户GET http://localhost:8080/api/users4. 更新用户PUT http://localhost:8080/api/users/1 Content-Type: application/json { username: 张三, email: zhangsan_newexample.com, age: 26 }5. 删除用户DELETE http://localhost:8080/api/users/1八、注解总结注解作用示例RedisHash实体类映射到Redis HashRedisHash(user)Id标记主键字段Id private Long id;Indexed创建二级索引支持字段查询Indexed private String username;EnableCaching启用缓存主类上EnableCachingCacheable方法结果缓存Cacheable(key #id)CachePut更新缓存CachePut(key #user.id)CacheEvict删除缓存CacheEvict(key #id)Caching组合多个缓存操作见上面Service中的update方法九、运行效果零基础全栈开发Java微服务版本实战-后端-前端-运维-实战企业级三个实战项目第一次查询用户(1)控制台打印日志查询数据库第二次查询用户(1)直接从缓存返回不打印日志更新用户(1)更新数据库并更新缓存删除用户(1)删除数据库记录并清除缓存十、注意事项实体类必须实现Serializable接口缓存注解的方法必须是public的同一个类内部调用缓存注解的方法不会生效Redis 需要提前安装并启动这个示例包含了最基本的 Redis 注解 CRUD 操作可以直接运行使用。
版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!

做网站比较便宜discuz 网站风格

FaceFusion与Unity集成方案:为游戏添加AI换脸功能 在如今的游戏开发中,玩家对“个性化”和“沉浸感”的要求越来越高。我们不再满足于选择预设的脸型或肤色——越来越多的人希望自己的脸能真正出现在游戏角色上,仿佛进入了一个属于自己的虚拟…

张小明 2026/1/1 15:25:51 网站建设

网站论坛推广文案怎么做模板做网站优缺点

本文介绍了一个GitHub上免费的优质大语言模型课程,提供科学家和工程师双路径学习路线。科学家路径涵盖模型训练各环节,工程师路径专注应用开发。课程包含8个开箱即用的Google Colab工具,大幅降低技术门槛,支持自动化评估、模型合并…

张小明 2026/1/7 14:18:22 网站建设

做网站应该注意些什么问题做服装要看国外哪些网站

MobaXterm简介与核心优势定义:多功能远程管理工具(SSH、X11、RDP、VNC等)核心功能:多协议支持、内置X服务器、标签式会话、文件传输优势对比:与PuTTY、SecureCRT等工具的差异化特性www.jqzxbz.cn基础环境配置与优化安装…

张小明 2025/12/29 4:45:27 网站建设

网站服务器续费做百度手机网站点击

欧拉(openEuler)和CentOS的基础指令大多通用,但在软件包管理、内核参数优化、系统镜像管理等关键场景,存在明显指令差异,以下列举3个典型示例,具体区别如下: 软件包安装指令 两者虽同属RPM生态&…

张小明 2025/12/29 4:45:29 网站建设

网站开发工作室企业网站建设的要素

使用Python安装脚本自动化部署YOLO环境 在智能工厂的质检线上,一台边缘计算盒子正对高速传送带上的产品进行实时缺陷检测。工程师刚接手新设备时却遇到难题:系统无法加载预训练模型,报错指向PyTorch版本不兼容。类似场景在AI项目落地中屡见不…

张小明 2025/12/29 4:45:28 网站建设