• Spring Boot基于注解的Redis缓存实现


    一、添加Spring Data Redis依赖启动器

            <!--Spring Data Redis 依赖启动器-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>

    二、Redis服务连接配置

    #MySQL数据库连接配置
    spring.datasource.url=jdbc:mysql://192.168.152.120:3306/springboottest?serverTimezone=UTC
    spring.datasource.username=root
    spring.datasource.password=1
    #显示使用JPA进行数据库查询的SQL语句
    spring.jpa.show-sql=true
    #redis相关配置
    spring.redis.host=192.168.152.120
    spring.redis.port=6379
    spring.redis.password=1

    三、实体类

     四、repository

    package com.uos.cache.repository;
    
    
    import com.uos.cache.domain.Comment;
    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.data.jpa.repository.Modifying;
    import org.springframework.data.jpa.repository.Query;
    
    import javax.transaction.Transactional;
    
    public interface CommentRepository extends JpaRepository<Comment,Integer> {
        // 根据评论id修改评论作者
        @Transactional
        @Modifying
        @Query("UPDATE t_comment c SET c.author= ?1 WHERE  c.id = ?2")
        public int updateComment(String author,Integer id);
    }
    CommentRepository

    五、service

    package com.uos.cache.service;
    
    import com.uos.cache.domain.Comment;
    import com.uos.cache.repository.CommentRepository;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.cache.annotation.*;
    import org.springframework.stereotype.Service;
    
    import java.util.Optional;
    
    @Service
    public class CommentService {
        @Autowired
        private CommentRepository commentRepository;
        @Cacheable(cacheNames = "comment",unless = "#result==null")
        public Comment findById(int comment_id){
            Optional<Comment> optional = commentRepository.findById(comment_id);
            if(optional.isPresent()){
                return optional.get();
            }
            return null;
        }
        @CachePut(cacheNames = "comment",key = "#result.id")
        public Comment updateComment(Comment comment){
            commentRepository.updateComment(comment.getAuthor(), comment.getaId());
            return comment;
        }
        @CacheEvict(cacheNames = "comment")
        public void deleteComment(int comment_id){
            commentRepository.deleteById(comment_id);
        }
    }
    CommentService

    六、controller

    package com.uos.cache.controller;
    
    
    import com.uos.cache.domain.Comment;
    import com.uos.cache.service.CommentService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class CommentController {
        @Autowired
        private CommentService commentService;
        @GetMapping("/get/{id}")
        public Comment findById(@PathVariable("id") int comment_id){
            Comment comment = commentService.findById(comment_id);
            return comment;
        }
        @GetMapping("/update/{id}/{author}")
        public Comment updateComment(@PathVariable("id") int comment_id,
                                     @PathVariable("author") String author){
            Comment comment = commentService.findById(comment_id);
            comment.setAuthor(author);
            Comment updateComment = commentService.updateComment(comment);
            return updateComment;
        }
        @GetMapping("/delete/{id}")
        public void deleteComment(@PathVariable("id") int comment_id){
            commentService.deleteComment(comment_id);}
    
    }
    CommentController

    七、主程序类

     八、测试结果

    查询测试

     更新测试

     删除测试

  • 相关阅读:
    HDU
    01字典树模板
    扩展欧几里得和乘法逆元
    HDOJ-1156 Brownie Points II 线段树/树状数组(模板)
    CF-825E Minimal Labels 反向拓扑排序
    CF-831D Office Keys 思维题
    RMQ 解决区间查询问题
    hdu 5073 有坑+方差贪心
    hdu 5074 相邻数和最大dp
    hdu 5078 水题
  • 原文地址:https://www.cnblogs.com/my-program-life/p/12067656.html
Copyright © 2020-2023  润新知