Class ExampleWrapper<T,​I extends Serializable>

  • Type Parameters:
    T - 实体类类型
    I - 主键类型

    public class ExampleWrapper<T,​I extends Serializable>
    extends Object
    封装 Example 的查询条件,方便链式调用
    Author:
    liuzh
    • Constructor Detail

    • Method Detail

      • example

        public Example<T> example()
        获取查询条件
      • startSql

        public ExampleWrapper<T,​I> startSql​(String startSql)
        设置起始 SQL
        Parameters:
        startSql - 起始 SQL,添加到 SQL 前,注意防止 SQL 注入
      • endSql

        public ExampleWrapper<T,​I> endSql​(String endSql)
        设置结尾 SQL
        Parameters:
        endSql - 结尾 SQL,添加到 SQL 最后,注意防止 SQL 注入
      • orderBy

        public ExampleWrapper<T,​I> orderBy​(Fn<T,​Object> fn,
                                                 Example.Order order)
        通过方法引用方式设置排序字段
        Parameters:
        fn - 排序列的方法引用
        order - 排序方式
        Returns:
        Example
      • orderBy

        public ExampleWrapper<T,​I> orderBy​(String orderByCondition)
        支持使用字符串形式来设置 order by,用以支持一些特殊的排序方案
        Parameters:
        orderByCondition - 排序字符串(不会覆盖已有的排序内容)
        Returns:
        Example
      • orderBy

        public ExampleWrapper<T,​I> orderBy​(Supplier<String> orderByCondition)
        支持使用字符串形式来设置 order by,用以支持一些特殊的排序方案
        Parameters:
        orderByCondition - 排序字符串构造方法,比如通过数组集合循环拼接等
        Returns:
        Example
      • orderBy

        public ExampleWrapper<T,​I> orderBy​(boolean useOrderBy,
                                                 Supplier<String> orderByCondition)
        支持使用字符串形式来设置 order by,用以支持一些特殊的排序方案
        Parameters:
        useOrderBy - 条件表达式,true使用,false不使用 字符串排序
        orderByCondition - 排序字符串构造方法,比如通过数组集合循环拼接等
        Returns:
        Example
      • orderByAsc

        @SafeVarargs
        public final ExampleWrapper<T,​I> orderByAsc​(Fn<T,​Object>... fns)
        通过方法引用方式设置排序字段,升序排序
        Parameters:
        fns - 排序列的方法引用
        Returns:
        Example
      • orderByDesc

        @SafeVarargs
        public final ExampleWrapper<T,​I> orderByDesc​(Fn<T,​Object>... fns)
        通过方法引用方式设置排序字段,降序排序
        Parameters:
        fns - 排序列的方法引用
        Returns:
        Example
      • set

        public ExampleWrapper<T,​I> set​(boolean useSet,
                                             String setSql)
        设置更新字段和值
        Parameters:
        useSet - 表达式条件, true 使用,false 不使用
        setSql - "column = value"
      • set

        public ExampleWrapper<T,​I> set​(String setSql)
        设置更新字段和值
        Parameters:
        setSql - "column = value"
      • set

        public ExampleWrapper<T,​I> set​(boolean useSet,
                                             Fn<T,​Object> fn,
                                             Object value)
        设置更新字段和值
        Parameters:
        useSet - 表达式条件, true 使用,false 不使用
        fn - 字段
        value - 值
      • set

        public ExampleWrapper<T,​I> set​(boolean useSet,
                                             Fn<T,​Object> fn,
                                             Supplier<Object> supplier)
        设置更新字段和值
        Parameters:
        useSet - 表达式条件, true 使用,false 不使用
        fn - 字段
        supplier - 值构造函数
      • isNull

        public ExampleWrapper<T,​I> isNull​(boolean useCondition,
                                                Fn<T,​Object> fn)
        指定字段为 null
        Parameters:
        useCondition - 表达式条件, true 使用,false 不使用
        fn - 字段对应的 get 方法引用
      • isNull

        public ExampleWrapper<T,​I> isNull​(Fn<T,​Object> fn)
        指定字段为 null
        Parameters:
        fn - 字段对应的 get 方法引用
      • isNotNull

        public ExampleWrapper<T,​I> isNotNull​(boolean useCondition,
                                                   Fn<T,​Object> fn)
        指定字段不为 null
        Parameters:
        useCondition - 表达式条件, true 使用,false 不使用
        fn - 字段对应的 get 方法引用
      • isNotNull

        public ExampleWrapper<T,​I> isNotNull​(Fn<T,​Object> fn)
        指定字段不为 null
        Parameters:
        fn - 字段对应的 get 方法引用
      • eq

        public ExampleWrapper<T,​I> eq​(boolean useCondition,
                                            Fn<T,​Object> fn,
                                            Object value)
        字段 = 值
        Parameters:
        useCondition - 表达式条件, true 使用,false 不使用
        fn - 字段对应的 get 方法引用
        value - 值
      • eq

        public ExampleWrapper<T,​I> eq​(boolean useCondition,
                                            Fn<T,​Object> fn,
                                            Supplier<Object> supplier)
        字段 = 值
        Parameters:
        useCondition - 表达式条件, true 使用,false 不使用
        fn - 字段对应的 get 方法引用
        supplier - 值构造函数
      • ne

        public ExampleWrapper<T,​I> ne​(boolean useCondition,
                                            Fn<T,​Object> fn,
                                            Supplier<Object> supplier)
        字段 != 值
        Parameters:
        useCondition - 表达式条件, true 使用,false 不使用
        fn - 字段对应的 get 方法引用
        supplier - 值构造函数
      • ne

        public ExampleWrapper<T,​I> ne​(boolean useCondition,
                                            Fn<T,​Object> fn,
                                            Object value)
        字段 != 值
        Parameters:
        useCondition - 表达式条件, true 使用,false 不使用
        fn - 字段对应的 get 方法引用
        value - 值
      • gt

        public ExampleWrapper<T,​I> gt​(boolean useCondition,
                                            Fn<T,​Object> fn,
                                            Supplier<Object> supplier)
        字段 > 值
        Parameters:
        useCondition - 表达式条件, true 使用,false 不使用
        fn - 字段对应的 get 方法引用
        supplier - 值构造函数
      • gt

        public ExampleWrapper<T,​I> gt​(boolean useCondition,
                                            Fn<T,​Object> fn,
                                            Object value)
        字段 > 值
        Parameters:
        useCondition - 表达式条件, true 使用,false 不使用
        fn - 字段对应的 get 方法引用
        value - 值
      • ge

        public ExampleWrapper<T,​I> ge​(boolean useCondition,
                                            Fn<T,​Object> fn,
                                            Supplier<Object> supplier)
        字段 >= 值
        Parameters:
        useCondition - 表达式条件, true 使用,false 不使用
        fn - 字段对应的 get 方法引用
        supplier - 值构造函数
      • ge

        public ExampleWrapper<T,​I> ge​(boolean useCondition,
                                            Fn<T,​Object> fn,
                                            Object value)
        字段 >= 值
        Parameters:
        useCondition - 表达式条件, true 使用,false 不使用
        fn - 字段对应的 get 方法引用
        value - 值
      • lt

        public ExampleWrapper<T,​I> lt​(boolean useCondition,
                                            Fn<T,​Object> fn,
                                            Supplier<Object> supplier)
        字段 < 值
        Parameters:
        useCondition - 表达式条件, true 使用,false 不使用
        fn - 字段对应的 get 方法引用
      • lt

        public ExampleWrapper<T,​I> lt​(boolean useCondition,
                                            Fn<T,​Object> fn,
                                            Object value)
        字段 < 值
        Parameters:
        useCondition - 表达式条件, true 使用,false 不使用
        fn - 字段对应的 get 方法引用
        value - 值
      • le

        public ExampleWrapper<T,​I> le​(boolean useCondition,
                                            Fn<T,​Object> fn,
                                            Object value)
        字段 <= 值
        Parameters:
        useCondition - 表达式条件, true 使用,false 不使用
        fn - 字段对应的 get 方法引用
        value - 值
      • le

        public ExampleWrapper<T,​I> le​(boolean useCondition,
                                            Fn<T,​Object> fn,
                                            Supplier<Object> supplier)
        字段 <= 值
        Parameters:
        useCondition - 表达式条件, true 使用,false 不使用
        fn - 字段对应的 get 方法引用
        supplier - 值构造函数
      • in

        public ExampleWrapper<T,​I> in​(boolean useCondition,
                                            Fn<T,​Object> fn,
                                            Iterable values)
        字段 in (值集合)
        Parameters:
        useCondition - 表达式条件, true 使用,false 不使用
        fn - 字段对应的 get 方法引用
        values - 值集合
      • in

        public ExampleWrapper<T,​I> in​(boolean useCondition,
                                            Fn<T,​Object> fn,
                                            Supplier<Iterable> supplier)
        字段 in (值集合)
        Parameters:
        useCondition - 表达式条件, true 使用,false 不使用
        fn - 字段对应的 get 方法引用
        supplier - 值集合构造函数
      • in

        public ExampleWrapper<T,​I> in​(Fn<T,​Object> fn,
                                            Iterable values)
        字段 in (值集合)
        Parameters:
        fn - 字段对应的 get 方法引用
        values - 值集合
      • notIn

        public ExampleWrapper<T,​I> notIn​(boolean useCondition,
                                               Fn<T,​Object> fn,
                                               Iterable values)
        字段 not in (值集合)
        Parameters:
        useCondition - 表达式条件, true 使用,false 不使用
        fn - 字段对应的 get 方法引用
        values - 值集合
      • notIn

        public ExampleWrapper<T,​I> notIn​(boolean useCondition,
                                               Fn<T,​Object> fn,
                                               Supplier<Iterable> supplier)
        字段 not in (值集合)
        Parameters:
        useCondition - 表达式条件, true 使用,false 不使用
        fn - 字段对应的 get 方法引用
        supplier - 值集合构造函数
      • notIn

        public ExampleWrapper<T,​I> notIn​(Fn<T,​Object> fn,
                                               Iterable values)
        字段 not in (值集合)
        Parameters:
        fn - 字段对应的 get 方法引用
        values - 值集合
      • between

        public ExampleWrapper<T,​I> between​(boolean useCondition,
                                                 Fn<T,​Object> fn,
                                                 Object value1,
                                                 Object value2)
        字段 between value1 and value 2
        Parameters:
        useCondition - 表达式条件, true 使用,false 不使用
        fn - 字段对应的 get 方法引用
        value1 - 值1
        value2 - 值2
      • between

        public ExampleWrapper<T,​I> between​(boolean useCondition,
                                                 Fn<T,​Object> fn,
                                                 Supplier<Object> supplier1,
                                                 Supplier<Object> supplier2)
        字段 between value1 and value 2
        Parameters:
        useCondition - 表达式条件, true 使用,false 不使用
        fn - 字段对应的 get 方法引用
        supplier1 - 值1构造函数
        supplier2 - 值2构造函数
      • between

        public ExampleWrapper<T,​I> between​(Fn<T,​Object> fn,
                                                 Object value1,
                                                 Object value2)
        字段 between value1 and value 2
        Parameters:
        fn - 字段对应的 get 方法引用
        value1 - 值1
        value2 - 值2
      • notBetween

        public ExampleWrapper<T,​I> notBetween​(boolean useCondition,
                                                    Fn<T,​Object> fn,
                                                    Object value1,
                                                    Object value2)
        字段 not between value1 and value 2
        Parameters:
        useCondition - 表达式条件, true 使用,false 不使用
        fn - 字段对应的 get 方法引用
        value1 - 值1
        value2 - 值2
      • notBetween

        public ExampleWrapper<T,​I> notBetween​(boolean useCondition,
                                                    Fn<T,​Object> fn,
                                                    Supplier<Object> supplier1,
                                                    Supplier<Object> supplier2)
        字段 not between value1 and value 2
        Parameters:
        useCondition - 表达式条件, true 使用,false 不使用
        fn - 字段对应的 get 方法引用
        supplier1 - 值1构造函数
        supplier2 - 值2构造函数
      • notBetween

        public ExampleWrapper<T,​I> notBetween​(Fn<T,​Object> fn,
                                                    Object value1,
                                                    Object value2)
        字段 not between value1 and value 2
        Parameters:
        fn - 字段对应的 get 方法引用
        value1 - 值1
        value2 - 值2
      • contains

        public ExampleWrapper<T,​I> contains​(boolean useCondition,
                                                  Fn<T,​Object> fn,
                                                  String value)
        字段 like %值%
        Parameters:
        useCondition - 表达式条件, true 使用,false 不使用
        fn - 字段对应的 get 方法引用
        value - 值,两侧自动添加 %
      • contains

        public ExampleWrapper<T,​I> contains​(boolean useCondition,
                                                  Fn<T,​Object> fn,
                                                  Supplier<String> supplier)
        字段 like %值%
        Parameters:
        useCondition - 表达式条件, true 使用,false 不使用
        fn - 字段对应的 get 方法引用
        supplier - 值构造函数,两侧自动添加 %
      • contains

        public ExampleWrapper<T,​I> contains​(Fn<T,​Object> fn,
                                                  String value)
        字段 like %值%
        Parameters:
        fn - 字段对应的 get 方法引用
        value - 值,两侧自动添加 %
      • startsWith

        public ExampleWrapper<T,​I> startsWith​(boolean useCondition,
                                                    Fn<T,​Object> fn,
                                                    String value)
        字段 like 值%,匹配 value 为前缀的值
        Parameters:
        useCondition - 表达式条件, true 使用,false 不使用
        fn - 字段对应的 get 方法引用
        value - 值,右侧自动添加 %
      • startsWith

        public ExampleWrapper<T,​I> startsWith​(boolean useCondition,
                                                    Fn<T,​Object> fn,
                                                    Supplier<String> supplier)
        字段 like 值%,匹配 value 为前缀的值
        Parameters:
        useCondition - 表达式条件, true 使用,false 不使用
        fn - 字段对应的 get 方法引用
        supplier - 值构造函数,右侧自动添加 %
      • startsWith

        public ExampleWrapper<T,​I> startsWith​(Fn<T,​Object> fn,
                                                    String value)
        字段 like 值%,匹配 value 为前缀的值
        Parameters:
        fn - 字段对应的 get 方法引用
        value - 值,右侧自动添加 %
      • endsWith

        public ExampleWrapper<T,​I> endsWith​(boolean useCondition,
                                                  Fn<T,​Object> fn,
                                                  String value)
        字段 like %值,匹配 value 为后缀的值
        Parameters:
        useCondition - 表达式条件, true 使用,false 不使用
        fn - 字段对应的 get 方法引用
        value - 值,左侧自动添加 %
      • endsWith

        public ExampleWrapper<T,​I> endsWith​(boolean useCondition,
                                                  Fn<T,​Object> fn,
                                                  Supplier<String> supplier)
        字段 like %值,匹配 value 为后缀的值
        Parameters:
        useCondition - 表达式条件, true 使用,false 不使用
        fn - 字段对应的 get 方法引用
        supplier - 值构造函数,左侧自动添加 %
      • endsWith

        public ExampleWrapper<T,​I> endsWith​(Fn<T,​Object> fn,
                                                  String value)
        字段 like %值,匹配 value 为后缀的值
        Parameters:
        fn - 字段对应的 get 方法引用
        value - 值,左侧自动添加 %
      • like

        public ExampleWrapper<T,​I> like​(boolean useCondition,
                                              Fn<T,​Object> fn,
                                              String value)
        字段 like 值
        Parameters:
        useCondition - 表达式条件, true 使用,false 不使用
        fn - 字段对应的 get 方法引用
        value - 值,需要指定 '%'(多个), '_'(单个) 模糊匹配
      • like

        public ExampleWrapper<T,​I> like​(boolean useCondition,
                                              Fn<T,​Object> fn,
                                              Supplier<String> supplier)
        字段 like 值
        Parameters:
        useCondition - 表达式条件, true 使用,false 不使用
        fn - 字段对应的 get 方法引用
        supplier - 值构造函数,需要指定 '%'(多个), '_'(单个) 模糊匹配
      • like

        public ExampleWrapper<T,​I> like​(Fn<T,​Object> fn,
                                              String value)
        字段 like 值
        Parameters:
        fn - 字段对应的 get 方法引用
        value - 值,需要指定 '%'(多个), '_'(单个) 模糊匹配
      • notLike

        public ExampleWrapper<T,​I> notLike​(boolean useCondition,
                                                 Fn<T,​Object> fn,
                                                 String value)
        字段 not like 值
        Parameters:
        useCondition - 表达式条件, true 使用,false 不使用
        fn - 字段对应的 get 方法引用
        value - 值,需要指定 % 模糊匹配
      • notLike

        public ExampleWrapper<T,​I> notLike​(boolean useCondition,
                                                 Fn<T,​Object> fn,
                                                 Supplier<String> supplier)
        字段 not like 值
        Parameters:
        useCondition - 表达式条件, true 使用,false 不使用
        fn - 字段对应的 get 方法引用
        supplier - 值构造函数,需要指定 % 模糊匹配
      • notLike

        public ExampleWrapper<T,​I> notLike​(Fn<T,​Object> fn,
                                                 String value)
        字段 not like 值
        Parameters:
        fn - 字段对应的 get 方法引用
        value - 值,需要指定 % 模糊匹配
      • anyCondition

        public ExampleWrapper<T,​I> anyCondition​(boolean useCondition,
                                                      String condition)
        添加任意条件,条件一定是后端使用的,需要自己防止 SQL 注入
        Parameters:
        useCondition - 表达式条件, true 使用,false 不使用
        condition - 任意条件,例如 "length(countryname)<5"
      • anyCondition

        public ExampleWrapper<T,​I> anyCondition​(String condition)
        添加任意条件,条件一定是后端使用的,需要自己防止 SQL 注入
        Parameters:
        condition - 任意条件,例如 "length(countryname)<5"
      • anyCondition

        public ExampleWrapper<T,​I> anyCondition​(boolean useCondition,
                                                      String condition,
                                                      Object value)
        手写左边条件,右边用value值
        Parameters:
        useCondition - 表达式条件, true 使用,false 不使用
        condition - 例如 "length(countryname)="
        value - 例如 5
      • anyCondition

        public ExampleWrapper<T,​I> anyCondition​(boolean useCondition,
                                                      String condition,
                                                      Supplier<Object> supplier)
        手写左边条件,右边用value值
        Parameters:
        useCondition - 表达式条件, true 使用,false 不使用
        condition - 例如 "length(countryname)="
        supplier - 任意条件值的构造函数
      • anyCondition

        public ExampleWrapper<T,​I> anyCondition​(String condition,
                                                      Object value)
        手写左边条件,右边用value值
        Parameters:
        condition - 例如 "length(countryname)="
        value - 例如 5
      • delete

        public int delete()
        根据当前条件删除
      • update

        public int update​(T t)
        将符合当前查询条件的数据更新为提供的值, set(String)set(Fn, Object) 设置的值无效
        Parameters:
        t - 要更新的值
      • updateSelective

        public int updateSelective​(T t)
        将符合当前查询条件的数据更新为提供的值,不更新 null 的值
        Parameters:
        t - 要更新的值
      • list

        public List<T> list()
        根据当前查询条件查询
      • stream

        public Stream<T> stream()
        根据当前查询条件查询,返回 Stream
      • one

        public Optional<T> one()
        根据当前查询条件查询出一个结果,当存在多个符合条件的结果时会抛出异常 TooManyResultsException
      • first

        public Optional<T> first()
        根据当前查询条件查询出第一个结果,通过 RowBounds 实现
      • top

        public List<T> top​(int n)
        根据当前查询条件查询出前 n 个结果,通过 RowBounds 实现
        Parameters:
        n - 结果数
      • count

        public long count()
        查询符合当前条件的结果数