• 使用缓存与JPA操作


    <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>
        </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    
    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);
        }
    }

  • 相关阅读:
    JavaScript之DOM查询
    JavaScript之this解析
    Qt之pro文件解析
    Qt5 调试之详细日志文件输出(qInstallMessageHandler)
    修改 Ubuntu的源为阿里源
    Unable to acquire the dpkg frontend lock
    gcc编译中文字符串后,windows控制台输出乱码
    stm32f103 time2配置,转载
    取反
    单片机,struct ,union定义标志,节约RAM
  • 原文地址:https://www.cnblogs.com/tszr/p/15913513.html
Copyright © 2020-2023  润新知