Mapper icon indicating copy to clipboard operation
Mapper copied to clipboard

Example对象new时为啥notnull是false,其他框架都是true

Open triennium-coder opened this issue 6 months ago • 4 comments

Image 这会导致如果参数传null,这个查询条件就被忽略的。容易导致查询范围变大

triennium-coder avatar Jul 24 '25 04:07 triennium-coder

Image

这种默认值不能从框架直接修改,影响范围太大,可以考虑自己封装一个newExample的方法,自己调用赋值true。如果你有更好的方法,可以继续沟通或者PR。

        protected void addCriterion(String condition, Object value, String property) {
            if (value == null) {
                if (notNull) {
                    throw new MapperException("Value for " + property + " cannot be null");
                } else {
                    return;
                }
            }
            if (property == null) {
                return;
            }
            criteria.add(new Criterion(condition, value));
        }

        protected void addCriterion(String condition, Object value1, Object value2, String property) {
            if (value1 == null || value2 == null) {
                if (notNull) {
                    throw new MapperException("Between values for " + property + " cannot be null");
                } else {
                    return;
                }
            }
            if (property == null) {
                return;
            }
            criteria.add(new Criterion(condition, value1, value2));
        }

abel533 avatar Jul 24 '25 05:07 abel533

现在看到这种null的时候我还在想,要不要直接转换为 column is null。

abel533 avatar Jul 24 '25 05:07 abel533

这些做法都太隐式了,报错才是最对的。历史问题是不是可以提供一个新的StrictExample类

triennium-coder avatar Jul 25 '25 02:07 triennium-coder

增加一个配置项,可以配一下 不配置默认走老逻辑 这样可以兼顾老用户

wien13 avatar Jul 29 '25 02:07 wien13