一、添加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); }
五、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); } }
六、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);} }