Example example = new Example(Receiver.class);
example.createCriteria().andEqualTo(Receiver.CODE, station.getCode());
int count = receiverMapper.selectCountByExample(example);
这几行代码有没有问题,
实际上达不到我们业务要求的效果,当station.getCode()为空的时候,我们想当然的以为就是查询为空时的条数,但实际并不是。。。
查看andEqualTo的源码
public Example.Criteria andEqualTo(String property, Object value) {
this.addCriterion(this.column(property) + " =", value, this.property(property));
return (Example.Criteria)this;
}
继续查看addCriterion方法
1 protected void addCriterion(String condition, Object value, String property) {
2 if (value == null) {
3 if (this.notNull) {
4 throw new MapperException("Value for " + property + " cannot be null");
5 }
6 } else if (property != null) {
7 this.criteria.add(new Example.Criteri

本文揭示了在使用MyBatis的Example创建器时,andEqualTo方法在处理station.getCode()为null的情况下的行为,指出它可能导致全表查询。关键在于理解NotNull属性和非空条件的设置。通过分析Criteria类和addCriterion方法,提出在使用时务必进行空值检查以达到预期业务效果。

1111

被折叠的 条评论
为什么被折叠?



