栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot 缓存管理 基础环境搭建

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot 缓存管理 基础环境搭建

一、JPI对数据操作

1、创建Spring Boot项目,引入相关依赖

① t_comment ,这个表预先插入几条测试数据。 ② 使用 Spring Initializr 方式创建一个 Spring Boot 项目,在 Dependencies 依赖选择项中 JPA 依赖、 MySQL 依赖和 Web 依赖,以及Thymeleaf模板依赖。

        

3.在application.propertise中连接数据库源

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=true  //注意将此行代码springboot字段换成自己的数据库名称
spring.datasource.username=root
spring.datasource.password=root

4、编写数据库表对应的实体类(省略getXX()、setXX()方法和toString()方法)

package com.example.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity(name="t_comment") //设计实体并指定映射的表名
public class Comment {
    @Id//表名影射对应的主键
    @GeneratedValue (strategy  = GenerationType.IDENTITY) //设计主键自增策略

    private Integer id;
    private String content;
    private String autnor;
    @Column(name = "a_id") //如果实体类中属性和表中值不一样加此注解
    private Integer a_id;

    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 getAutnor() {
        return autnor;
    }

    public void setAutnor(String autnor) {
        this.autnor = autnor;
    }

    public Integer getA_id() {
        return a_id;
    }

    public void setA_id(Integer a_id) {
        this.a_id = a_id;
    }

    @Override
    public String toString() {
        return "Comment{" +
                "id=" + id +
                ", content='" + content + ''' +
                ", autnor='" + autnor + ''' +
                ", a_id=" + a_id +
                '}';
    }
}

5、编写数据库操作的Repository接口文件CommentRepository

package com.example.repository;

import com.example.model.Comment;
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;

public interface CommentRepository extends JpaRepository {

    
    @Transactional//添加事务
    @Modifying//定义个性化更新操作
    @Query("update t_comment c SET c.autnor=?1 where c.id=?2")
    public int updateComment(String autnor,int id);

}

5、编写业务操作类Service文件CommentService,在该类中编写数据的查询、修改和删除操作

package com.example.service;


import com.example.model.Comment;
import com.example.repository.CommentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class CommentService {
    @Autowired
    private CommentRepository commentRepository;

    @Cacheable(cacheNames = "comment")//对方法进行结果缓存 
public Comment findById(int comment_id){
        Optional optional=commentRepository.findById(comment_id);
        if(optional.isPresent()){
            return optional.get();
        }
        return  null;
    }

    public Comment updateComment(Comment comment){
        commentRepository.updateComment(comment.getAutnor(),comment.getA_id());
        return comment;
    }
    public void deleteComment(int comment_id){
        commentRepository.deleteById(comment_id);
    }

}

6、编写Web访问层Controller文件CommentController,使用注入的CommentService实例对象编写对Comment评论数据的查询、修改和删除方法。(具体代码见备注)

package com.example.controller;

import com.example.model.Comment;
import com.example.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}")//用于处理请求方法GET类型
    public Comment findBy(@PathVariable("id") int comment_id){
        Comment comment = commentService.findById(comment_id);
        return comment;
    }

    @GetMapping("/update/{id}/{autnor}")
        public Comment updateComment(@PathVariable("id") int comment_id,@PathVariable("autnor") String autnor){
            Comment comment = commentService.findById(comment_id);
            comment.setAutnor(autnor);
            Comment updateComment = commentService.updateComment(comment);
            return updateComment;
        }

    @GetMapping("/delete/{id}")
    public void deletComment(@PathVariable("id") int comment_id){
        commentService.deleteComment(comment_id);
    }


}

7,在项目自带的测试类中添加@EnableCaching开启缓存

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@EnableCaching//开启缓存
@SpringBootApplication
public class Demo2Application {

    public static void main(String[] args) {
        SpringApplication.run(Demo2Application.class, args);
    }

}
二、JPI对数据更新操作

1.在服务层添加注解

2.autnor值改变

 三、基于Redis插件对数据的操作

1、在pom.xml里面添加Redis依赖

 
            org.springframework.boot
            spring-boot-starter-data-redis
        

2.Redis服务器链接配置

spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=  //此处写自己设置的的Redis密码

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/769767.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号