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

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

Spring Boot基于注解的Redis缓存实现_第1张图片

 

 

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-redisartifactId>
        dependency>

二、Redis服务连接配置

Spring Boot基于注解的Redis缓存实现_第2张图片

 

 

#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

三、实体类

Spring Boot基于注解的Redis缓存实现_第3张图片

 

 四、repository

Spring Boot基于注解的Redis缓存实现_第4张图片

 

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 {
    // 根据评论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

Spring Boot基于注解的Redis缓存实现_第5张图片

 

 

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 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

Spring Boot基于注解的Redis缓存实现_第6张图片

 

 

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

 

 

七、主程序类

Spring Boot基于注解的Redis缓存实现_第7张图片

 

 八、测试结果

查询测试

Spring Boot基于注解的Redis缓存实现_第8张图片

 

 更新测试

Spring Boot基于注解的Redis缓存实现_第9张图片

 

 删除测试

Spring Boot基于注解的Redis缓存实现_第10张图片

你可能感兴趣的:(Spring Boot基于注解的Redis缓存实现)