访问接口
查看打印值
查看接口,controller层到mapper层,去redis查看,如果没有就从数据库查出来,在redis中存进去,如果redis中存在就从redis中拿值,redis做为缓存是真的快。
在这里插入图片描述
在这里插入图片描述

pom.xml

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>

        <!--redis-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <version>2.3.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

application.properties

# 应用名称
spring.application.name=bootredis
# 应用服务 WEB 访问端口
server.port=8080
#下面这些内容是为了让MyBatis映射
#指定Mybatis的Mapper文件
mybatis.mapper-locations=classpath:mappers/*xml
#指定Mybatis的实体目录
mybatis.type-aliases-package=com.example.bootredis.mybatis.entity
# 数据库驱动:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# 数据源名称
spring.datasource.name=defaultDataSource
# 数据库连接地址
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC
# 数据库用户名&密码:
spring.datasource.username=root
spring.datasource.password=123456

spring.redis.host=127.0.0.1
spring.redis.database=0
spring.redis.port=6379

PaymentMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.bootredis.dao.PaymentDao">
    <resultMap id="BaseResultMap" type="com.example.bootredis.entity.Payment">
        <id column="id" property="id"></id>
        <id column="serial" property="serial"></id>
    </resultMap>

    <sql id="Base_Column_List">
        id,serial
    </sql>


    <sql id="Query_Column_List">
        <where>
            <if test="id!=null">id=#{id}</if>
            <if test="serial!=null">and serial=#{serial}</if>
        </where>
    </sql>
    <select id="selectId" parameterType="java.lang.Long" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"></include>
        from
        payment
        where id=#{arg0}
    </select>

    <select id="selectPayment"  resultType="com.example.bootredis.entity.Payment">
        select * from payment
        <include refid="Query_Column_List"></include>
    </select>

    <insert id="createPayment" parameterType="com.example.bootredis.entity.Payment" useGeneratedKeys="true" keyColumn="id">
        insert into payment
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id!=null">`id`,</if>
            <if test="serial!=null">`serial`,</if>
        </trim>
        values
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id!=null">#{id},</if>
            <if test="serial!=null">#{serial},</if>
        </trim>
    </insert>
</mapper>

PaymentService

package com.example.bootredis.service;

import com.example.bootredis.entity.Payment;

public interface PaymentService {

    Payment selectPayment(Long id);

    Payment selectId(Long id);
}

PaymentServiceImpl

package com.example.bootredis.service.impl;

import com.example.bootredis.dao.PaymentDao;
import com.example.bootredis.entity.Payment;
import com.example.bootredis.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;


@Service
@Slf4j
public class PaymentServiceImpl implements PaymentService {

    @Autowired
    private PaymentDao paymentDao;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Payment selectPayment(Long id) {
        return paymentDao.selectPayment(id);
    }

    @Override
    public Payment selectId(Long id) {

       String key=id.toString();
        ValueOperations<String,Payment> operations= redisTemplate.opsForValue();

      Boolean haskey=redisTemplate.hasKey(key);

      if(haskey){
          Payment payment=operations.get(key);
          log.info("redis中有这个数据");
          log.info(payment.getSerial()+"Payment表中Serial字段");
          log.info(payment.toString()+"Payment表中的所有信息");
          return payment;
      }else {
          Payment payment=this.paymentDao.selectId(id);
          log.info("从数据库当中获得的信息serial字段"+payment.getSerial());
          log.info("从数据库当中获得的信息"+payment.toString());
          operations.set(key,payment,5, TimeUnit.MINUTES);
          return payment;

      }


    }
}

在这里插入图片描述
Payment

package com.example.bootredis.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable {

    private Long id;
    private String serial;


}

CommonResult

package com.example.bootredis.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T> {

    private Integer code;

    private String msg;

    private T data;


    public   CommonResult success(T data){
        return new CommonResult(200,"响应成功",data);
    }

    public   CommonResult fail(T data){
        return new CommonResult(500,"响应失败",data);
    }

}

PaymentDao

package com.example.bootredis.dao;


import com.example.bootredis.entity.Payment;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface PaymentDao {

    Payment selectPayment(Long id);

    Payment selectId(Long id);

}

PaymentController

package com.example.bootredis.controller;


import com.example.bootredis.dao.PaymentDao;
import com.example.bootredis.entity.CommonResult;
import com.example.bootredis.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @RequestMapping("/selectPayment/{id}")
    public CommonResult selectId(@PathVariable("id") Long id){
        return new CommonResult().success(this.paymentService.selectId(id));
    }

}

在这里插入图片描述
两个字段测试
数据库表代码就不贴出来了,可以自行建

redis安装
参考这篇文章
windows上的redis
https://how2j.cn/k/redis/redis-redisclient/1790.html
在这里插入图片描述

案例地址:
链接:https://pan.baidu.com/s/1Y5XIYci1UUmy3Mdb3Bd_Fg
提取码:kb3o
复制这段内容后打开百度网盘手机App,操作更方便哦

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐