自用redis 代码存入同库不同表的代码

自用redis 代码存入同库不同表的代码,每次使用仅需加入新的service和更改返回id的数据类型即可

import com.yase.mybatisX.entitiy.IEmp;
import com.yase.mybatisX.entitiy.Student;
import com.yase.mybatisX.service.IEmpService;
import com.yase.mybatisX.service.StudentService;
import com.yase.mybatisX.service.TeacherService;
import lombok.extern.log4j.Log4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("emp")
public class StudentController {
    @Resource
    private RedisTemplate redisTemplate;
    //这个是查询数据表的源头,更换数据表,需要更换不同的service
//    @Resource
//    private StudentService service;
//
//    @Resource
//    private TeacherService service;

    @Resource
    private IEmpService service;

    @GetMapping("/{id}")
    public Object get(@PathVariable int id){
        String key = "menber" + id;
        Object redisValue = redisTemplate.opsForValue().get(key);
        //缓存中没有,则进入数据库查询
        if (redisValue == null){
            System.out.println("查询数据库");
            //数据库查询到id的数据,每次更改数据表,需要更改返回值类型与之对应
//            Student byId = service.getById(id);
            IEmp byId = service.getById(id);
            //加入缓存
            redisTemplate.opsForValue().set(key,byId);
            return byId;
        }else {
            System.out.println("查询缓存");
            //缓存中有,直接返回查询数据
            return redisValue;
        }
    }

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值