• 基于注解的Redis缓存实现


    <project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.tszr</groupId>
        <artifactId>test</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.3.RELEASE</version>
        </parent>
        
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.28</version><!--$NO-MVN-MAN-VER$-->
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            
            <!-- 引入整合Redis缓存的依赖启动器 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>
        </dependencies>
        
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>11</source>
                        <target>11</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    server:
      port: 8082
      
    spring:
      datasource:
        url: jdbc:mysql://localhost:3306/springbootdata?useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=utf-8
        username: root
        password: admin
      jpa:
        show-sql: true    
      redis:
        host: 127.0.0.1
        port: 6379
        password: 
      cache:
        redis:
          time-to-live: 60000
    package com.itheima.domain;
    
    import java.io.Serializable;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    
    @Entity(name = "t_comment") // 设置ORM实体类,并指定映射的表名
    public class Comment implements Serializable{
    
        private static final long serialVersionUID = 1L;
        
        @Id // 表明映射对应的主键id
        @GeneratedValue(strategy = GenerationType.IDENTITY) // 设置主键自增策略
        private Integer id;
        private String content;
        private String author;
        @Column(name = "a_id") // 指定映射的表字段名
        private Integer aId;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getContent() {
            return content;
        }
    
        public void setContent(String content) {
            this.content = content;
        }
    
        public String getAuthor() {
            return author;
        }
    
        public void setAuthor(String author) {
            this.author = author;
        }
    
        public Integer getaId() {
            return aId;
        }
    
        public void setaId(Integer aId) {
            this.aId = aId;
        }
    
        @Override
        public String toString() {
            return "Comment{" + "id=" + id + ", content='" + content + '\'' + ", author='" + author + '\'' + ", aId=" + aId
                    + '}';
        }
    }
    package com.itheima.repository;
    
    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.data.jpa.repository.Modifying;
    import org.springframework.data.jpa.repository.Query;
    import org.springframework.transaction.annotation.Transactional;
    
    import com.itheima.domain.Comment;
    
    public interface CommentRepository extends JpaRepository<Comment,Integer>{
        // 根据评论id修改评论作者评论作者author
        @Transactional
        @Modifying
        @Query("UPDATE t_comment c SET c.author= ?1 WHERE  c.id = ?2")
        public int updateComment(String author,Integer id);
    }
    package com.itheima.service;
    
    import com.itheima.domain.Comment;
    
    public interface MyCommentService {
    
        public Comment findById(int comment_id);
        
        public Comment updateComment(Comment comment);
        
        public void deleteComment(int comment_id);
    }
    package com.itheima.serviceImpl;
    
    import java.util.Optional;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.cache.annotation.CacheEvict;
    import org.springframework.cache.annotation.CachePut;
    import org.springframework.cache.annotation.Cacheable;
    import org.springframework.stereotype.Service;
    
    import com.itheima.domain.Comment;
    import com.itheima.repository.CommentRepository;
    import com.itheima.service.MyCommentService;
    
    @Service
    public class MyCommentServiceImp implements MyCommentService {
    
        @Autowired
        private CommentRepository commentRepository;
    
        @Override
    //    @Cacheable(cacheNames="comment")
        @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;
        }
    
        @Override
        @CachePut(cacheNames = "comment",key = "#result.id")
        public Comment updateComment(Comment comment) {
            commentRepository.updateComment(comment.getAuthor(), comment.getaId());
            return comment;
        }
    
        @Override
        @CacheEvict(cacheNames = "comment")
        public void deleteComment(int comment_id) {
            commentRepository.deleteById(comment_id);
        }
    
    }
    package com.itheima.controller;
    
    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;
    
    import com.itheima.domain.Comment;
    //import com.itheima.service.CommentService;
    import com.itheima.serviceImpl.MyCommentServiceImp;
    
    @RestController
    public class CommentController {
    
        @Autowired
        private MyCommentServiceImp 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);
        }
    }
    package com.itheima;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cache.annotation.EnableCaching;
    
    @EnableCaching
    @SpringBootApplication
    public class ApplicationTest {
        public static void main(String[] args) {
            SpringApplication.run(ApplicationTest.class, args);
        }
    }

     

     

     

     

  • 相关阅读:
    git cherrypick
    【系统设计】OA需求分析,OA系统选型及各供应商对比。
    .NET MD5加密 16位/32位
    手机端网页苹果/安卓滑动翻页,未触发事件解决办法
    k3s 私有仓库问题
    NeoVimByLua格式化代码
    RestTemplate 设置超时时间
    广告电商系统开发功能模块分析
    涂游易享系统开发和涂游易享app模式制度介绍
    广告电商系统开发新功能迭代目录
  • 原文地址:https://www.cnblogs.com/tszr/p/15913590.html
Copyright © 2020-2023  润新知