Coder Social home page Coder Social logo

Comments (9)

abel533 avatar abel533 commented on August 17, 2024

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

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

from mapper.

laboratorys avatar laboratorys commented on August 17, 2024

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

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

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

from mapper.

laboratorys avatar laboratorys commented on August 17, 2024

代码是这样的, 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生成器实在是灾难

from mapper.

abel533 avatar abel533 commented on August 17, 2024

看代码确认了一下,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();
  }

}

from mapper.

laboratorys avatar laboratorys commented on August 17, 2024

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

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

from mapper.

abel533 avatar abel533 commented on August 17, 2024

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

from mapper.

abel533 avatar abel533 commented on August 17, 2024

@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 "";

from mapper.

laboratorys avatar laboratorys commented on August 17, 2024

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

感谢解答

from mapper.

laboratorys avatar laboratorys commented on August 17, 2024

看代码确认了一下,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;
}

from mapper.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.