自用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;
@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("查询数据库");
IEmp byId = service.getById(id);
redisTemplate.opsForValue().set(key,byId);
return byId;
}else {
System.out.println("查询缓存");
return redisValue;
}
}
}