简单增删改查+缓存
数据库
idea目录:
1.开启哨兵,设置主从关系
开启
[root@localhost redis-5.0.8]# redis-sentinel sentinel.conf
设置主从关系(以80为例,其他一样)
slaveof 192.168.253.3 6379
以下为idea内容
1.springbootcache->springbootcache
@SpringBootApplication
@MapperScan(basePackages = {"com.zll.springbootcache.mapper"})
@EnableCaching
public class SpringbootcacheApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootcacheApplication.class, args);
}
}
2.resources->application.properties(连接数据库配置)
spring.datasource.username=root
spring.datasource.password=自己的数据库密码
spring.datasource.url=jdbc:mysql://localhost:3306/demo1?serverTimezone=Asia/Shanghai
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.redis.sentinel.master=mymaster
spring.redis.sentinel.nodes=192.168.253.3:26379
logging.level.com.zll.springbootcache.mapper=debug
3.entity->Student
@Data
public class Student {
@TableId(type = IdType.AUTO)
private Integer sid;
private String sname;
private String sex;
}
4.mapper->StudentMapper
public interface StudentMapper extends BaseMapper<Student> {
}
5.service->StudentService
@Service
public class StudentService {
@Resource
private StudentMapper studentMapper;
@Autowired
private RedisTemplate redisTemplate;
@Cacheable(cacheNames = "name::selectid",key="#id")
public Student selectId(Integer id){
Student student=studentMapper.selectById(id);
return student;
}
public void insert(Student student){
studentMapper.insert(student);//简单增加
}
@CachePut(cacheNames = "name::selectid",key="#student.sid")
public Student update(Student student){
studentMapper.updateById(student);
return student;
}
@CacheEvict(cacheNames = "name::selectid",key="#id")
public void delete(Integer id){
studentMapper.deleteById(id);
}
}
6.controller->StudentController
@RestController
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping("selectid/{id}")
public Student selectid(@PathVariable Integer id){
return studentService.selectId(id);
}
@PostMapping("insertstudent")
public String insert(Student student){
studentService.insert(student);
return "添加成功";
}
@PutMapping("updatestudent")
public Student update(Student student){
studentService.update(student);
return student;
}
@DeleteMapping("deletestudent/{id}")
public String delete(@PathVariable Integer id){
studentService.delete(id);
return "删除成功";
}
}
7.config->RedisConfig
详情配置请点击链接: https://blog.csdn.net/w1939481704/article/details/116304689.
8.运行结果
增加(简单)
利用postman接口测试
查询
利用postman接口测试
利用redisPlus测试
修改
利用postman接口测试
利用redisPlus测试
删除
利用postman接口测试
利用redisPlus测试