缓存注解key的基本数据类型要求必须要统一
//根据id查询缓存的值
@Cacheable(value = "testCache")
public TestEntity listById(int id){
return testMapper.listById(id);
}
//修改缓存的值
@CachePut(value = "testCache" ,key = "#result.id")
public TestEntity updateById(TestEntity testEntity){
System.out.println("运行结果:"+testMapper.updateById(testEntity));
System.out.println("id:"+testEntity.getId());
return testEntity;
}
//实体类修改前
public class TestEntity {
private String id;//注意这里
private String name;
private String sex;
}
//实体类修改后
public class TestEntity {
private int id;
private String name;
private String sex;
}
现在修改完成可以总结出来一个结论,key的基本数据类型要求必须要统一。
在cache中,数据类型的要求是非常严格的,同样的id的为1,int类型和String类型是不同。
我一开始在这个错误上面困扰了好久,因为mybatis返回的结果中,对于int和String类型的id并没有提示什么错误,一开始打断点,扒源码发现对这个key的生成并没有什么改变,我写的result.id一直传到了生成id的地方,尽管如何生成的并没有看的十分明白。
直到今天我再反复测试的时候,在写到”result.”的时候忽然发现id是String类型的才恍然大悟。