Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
947 views
in Technique[技术] by (71.8m points)

mybatis-plus queryWrapper如何舍弃null值?

QueryWrapper我用的是这种形式的

eq(R column, Object val)

文档里说null值不会加入sql的生成,但是实际测试还是会。

mp有这种形式的

eq(boolean condition, R column, Object val)

但是这种要多写很多!=null的判断

所以有没有什么方法(或者说要配置什么)才能让QueryWrapper中的null值不参与sql的生成


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

好家伙,我还跑去翻源码,翻了一会发现跑偏了,直接对queryWrapper改造不就行了。

public class CustomAbstractWrapper<T> extends QueryWrapper<T> {
    @Override
 protected QueryWrapper<T> addCondition(boolean condition, String column, SqlKeyword sqlKeyword, Object val) {
        if(Objects.isNull(val)){
            condition = false;
        }
        return super.addCondition(condition, column, sqlKeyword, val);
 }
}

重写queryWrapper.addCondition就行


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...