自助优化排名工具seo技术服务外包公司

张小明 2026/1/8 14:03:52
自助优化排名工具,seo技术服务外包公司,邢台设计公司,推广之家官网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进行投诉反馈,一经查实,立即删除!

大学生实训网站建设心得wordpress数据卡

博主介绍:✌️码农一枚 ,专注于大学生项目实战开发、讲解和毕业🚢文撰写修改等。全栈领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围:&am…

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

吕梁建站公司乌克兰网站服务器

Sketch文本查找替换插件:设计师必备的批量文本处理神器 【免费下载链接】Sketch-Find-And-Replace Sketch plugin to do a find and replace on text within layers 项目地址: https://gitcode.com/gh_mirrors/sk/Sketch-Find-And-Replace 你是否曾在Sketch设…

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

分栏式的网站有哪些做一百度网站

Unix 系统管理脚本实用指南(上) 在 Unix 系统管理中,有许多实用的脚本可以帮助我们更高效地完成各种任务,如磁盘配额管理、磁盘使用情况查看等。下面将详细介绍几个重要的脚本及其使用方法。 1. 磁盘配额分析脚本 在进行磁盘配额分析时,我们可以使用一些特定的脚本。 …

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

企业做网站的注意什么问题wordpress 获取文章数

快速搭建个人专属音乐空间:any-listen私有化部署终极指南 【免费下载链接】any-listen A cross-platform private song playback service. 项目地址: https://gitcode.com/gh_mirrors/an/any-listen 厌倦了商业音乐平台的广告轰炸和功能限制?想要…

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

自行车网站模板品牌网鞋有哪些牌子

Windows Vista 调试与同步特性深度解析 1. 进程间通信变化 Windows Vista 在进程间通信方面的改变主要局限于单个物理系统内部。在异构网络中运行时,其网络可观察行为与之前的操作系统相似,基于网络流量解析的技术仍然适用。不过,同一物理系统内各组件间的通信模型发生了变…

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

北京商城网站建设费用建立一个网站平台需要多少钱

Windows Cleaner终极教程:5分钟快速拯救C盘爆红的完整方案 【免费下载链接】WindowsCleaner Windows Cleaner——专治C盘爆红及各种不服! 项目地址: https://gitcode.com/gh_mirrors/wi/WindowsCleaner 还在为C盘红色警告而烦恼?系统运…

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