• 关于通用Mapper new Example使用记录


    MapperExample使

    • 需要集成 mybatis 的 generator 插件,方便自动生成 实体类和 mapper 类,还可以生成xml,不过一般我们都不用 xml
    • baseMapper 需要继承 ExampleMapper 不过只需要继承 Mapper 就可以了,因为 Mapper 已经继承了 ExampleMapper

    Example 的用法

    首先需要说明一点 ,和 Example 使用相同的还有 Condition 类 该类继承自 Example,使用方法和 Example 完全一样,只是为了避免语义有歧义重命名的一个类,这里我们都用 Example 来说明

    • 创建 Example :
    1
    Example example = new Example(XXX.class);

    其中构造方法为生成的 model 实体类,还有 2 个构造方法

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21

    /**
    * 带exists参数的构造方法,默认notNull为false,允许为空
    *
    * @param entityClass
    * @param exists - true时,如果字段不存在就抛出异常,false时,如果不存在就不使用该字段的条件
    */
    public Example(Class<?> entityClass, boolean exists) {
    ...
    }

    /**
    * 带exists参数的构造方法
    *
    * @param entityClass
    * @param exists - true时,如果字段不存在就抛出异常,false时,如果不存在就不使用该字段的条件
    * @param notNull - true时,如果值为空,就会抛出异常,false时,如果为空就不使用该字段的条件
    */
    public Example(Class<?> entityClass, boolean exists, boolean notNull) {
    ...
    }

    然后可以对 example 的实体类的单表进行查询了

    1
    2
    3
    4
    Example example = new Example(XXX.class);
    example.createCriteria().andGreaterThan("id", 100).andLessThan("id",151);
    example.or().andLessThan("id", 41);
    List<XXX> list = mapper.selectByExample(example);

    以上查询的条件是,查询 id 大于 100 并且小于 151 或者 id 小于 41 的记录

    还可以写成 sql 的方式:

    1
    2
    3
    4
    5
    Example example = new Example(XXX.class);
    example.createCriteria().andCondition("id > 100 and id <151 or id < 41");

    // andCondition() 方法可以叠加使用,像这样
    example.createCriteria().andCondition("id > 100 and id <151").orCondition("id <41");

    andCondition() 有2中使用方法:
    andCondition(String condition) : 手写条件,例如 “length(name)<5”
    andCondition(String condition, Object value) : 手写左边条件,右边用value值,例如 “length(name)=” “5”
    orCondition() 也是类似的

    example 里有很多 mysql 常用的方法,使用方法和 elasticsearch 的 java api 很类似,这里列举几个

    • Set<String> selectColumns : 查询的字段
    • Set<String> excludeColumns : 排除的查询字段
    • Map<String, EntityColumn> propertyMap : 属性和列对应
    • andAllEqualTo : 将此对象的所有字段参数作为相等查询条件,如果字段为 null,则为 is null
    • andGreaterThan : and 条件 大于
    • andBetween : and 条件 between
    • andEqualTo : 将此对象的不为空的字段参数作为相等查询条件 还有一种有 value 参数的是 = 条件
    • andGreaterThanOrEqualTo : and 条件 》=

    还有一些一看就知道意思的

    • andIn
    • andIsNotNull
    • andIsNull
    • andLessThan
    • andLessThanOrEqualTo
    • andNotLike

    上面是以 and 条件举例 ,or的条件也是一样的

  • 相关阅读:
    debian系统完全卸载mysql
    已解决:Linux虚拟机创建后无法切换到root
    已解决:win10 下MarkdownPad2渲染出错:This View has crashed!
    计算机网络---运输层
    计算机网络---网络层
    计算机网络---数据链路层
    计算机网络---概述
    计算机网络---物理层
    go的命令行参数---flag
    Go---第九章:使用共享变量实现并发(小知识点笔记)
  • 原文地址:https://www.cnblogs.com/tomingto/p/12762499.html
Copyright © 2020-2023  润新知