mybatis-mapper / mapper

MyBatis Mapper
https://mapper.mybatis.io
Apache License 2.0
325 stars 47 forks source link

ExampleMapper请求添加selectByExampleSelective方法 #100

Closed laboratorys closed 2 months ago

laboratorys commented 2 months ago

只有updateByExampleSelective,没有selectByExampleSelective,selectByExampleSelective还是比较常用的吧

abel533 commented 2 months ago

闻所未闻。。你写个示例看看,我不确定这个方法想实现什么。

如果指定查询列,已有方法。。如果判断在添加where条件,都有带判断条件的方法。

laboratorys commented 2 months ago

闻所未闻。。你写个示例看看,我不确定这个方法想实现什么。

如果指定查询列,已有方法。。如果判断在添加where条件,都有带判断条件的方法。

我的问题,确实没有这类方法,就是想给查询条件加个判断,不为空的时候再拼接where条件,列表搜索场景,实际用应该比较少,大部分都是自己写sql,if标签判断

laboratorys commented 2 months ago

代码是这样的, examplePageDto.getName() 不为空时可以不拼接条件,还有个疑问,ExamplePageDto继承了PageParam,同时使用Mapper和PageHelper分页,有没有更优雅的写法,我现在是在父类自己封装了一个方法调用PageHelper.startPage

io.mybatis.mapper.example.Example<Example> example = new io.mybatis.mapper.example.Example<>();
example.createCriteria().andLike(Example::getName, "%" + examplePageDto.getName() + "%");
return super.pageQuery(examplePageDto, () -> baseMapper.selectByExample(example), s -> {
    s.setName("用户_" + s.getName());
    return s;
});

还有个问题想问下可不可以不指定@Entity.Column,只指定ID和Table,其他默认驼峰转下划线这样的策略配置,如果需要排除的加个ignore注解即可,有的实体几十几百字段,不用Rui生成器实在是灾难

abel533 commented 2 months ago

看代码确认了一下,Example没有带条件的方法,ExampleWrapper有,建议试试Wrapper这种。

最新版本不需要都指定column注解,代码会判断是否排除:

public class DefaultEntityColumnFactory implements EntityColumnFactory {

  @Override
  public Optional<List<EntityColumn>> createEntityColumn(EntityTable entityTable, EntityField field, Chain chain) {
    if (field.isAnnotationPresent(Entity.Column.class)) {
      //省略
    } else if (!field.isAnnotationPresent(Entity.Transient.class)) {
      //省略
    }
    return Optional.empty();
  }

}
laboratorys commented 2 months ago

最新版本不需要都指定column注解,代码会判断是否排除:

OK,问下,有指定字段和实体属性对应得策略吗?驼峰转下划线

abel533 commented 2 months ago

可以指定策略,支持配置文件和注解,用法看这里:https://github.com/mybatis-mapper/mapper/releases/tag/2.0.0

abel533 commented 2 months ago

@Entity.Table 注解有下面的属性可以针对单个表进行配置:

    /**
     * 名称规则、样式,同时应用于表名和列名,不想用于表名时,直接指定表名 {@link #value()}即可。
     * <p>
     * 2.0版本之前默认为 {@link Style#NORMAL}, 2.0版本之后默认使用 {@link Style#LOWER_UNDERSCORE}
     * <p>
     * 可以通过 {@link Style#DEFAULT_STYLE_KEY} = 格式 来修改默认值
     */
    String style() default "";
laboratorys commented 2 months ago

io.mybatis.mapper.example.Example example = new io.mybatis.mapper.example.Example<>();

感谢解答

laboratorys commented 2 months ago

看代码确认了一下,Example没有带条件的方法,ExampleWrapper有,建议试试Wrapper这种。

最新版本不需要都指定column注解,代码会判断是否排除:

public class DefaultEntityColumnFactory implements EntityColumnFactory {

  @Override
  public Optional<List<EntityColumn>> createEntityColumn(EntityTable entityTable, EntityField field, Chain chain) {
    if (field.isAnnotationPresent(Entity.Column.class)) {
      //省略
    } else if (!field.isAnnotationPresent(Entity.Transient.class)) {
      //省略
    }
    return Optional.empty();
  }

}

看了下源码,ExampleWrapper当value为null时会抛出异常

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

好像是可以把判断条件写在前面,这样也行吧,虽然麻烦点但是更灵活

public ExampleWrapper<T, I> like(boolean useCondition, Fn<T, Object> fn, String value) {
    return useCondition ? this.like(fn, value) : this;
}