直接贴上save方法的实现:
public Entity save(Entity entity) throws DataServiceException {
PK id = entity.getId();
if (id == null) { //此处仅仅是进行了判断null,没有进行空字符串判断。
this.insert(entity);
} else {
this.update(entity);
}
return this.selectByPrimaryKey(entity.getId());
}
很显然底层进行save操作的时候,先进行了id为空的判断,如果id为空就执行insert,如果id不为就执行update。注意此处的判断条件是null,id=""仍然执行update。
总结:遇到问题的时候不要第一时间去网上查阅资料,或者是问别人,这样很容易忘记。一定先查看它的底层实现原理,再去查阅其他资料