Search in sources :

Example 1 with ApplySql

use of com.chao.cloud.common.extra.mybatis.common.ApplySql in project chao-cloud by chaojunzi.

the class MybatisUtil method parseApplySql.

/**
 * 解析{@link ApplySql}
 *
 * @param <T>     实体类类型
 * @param wrapper 构造条件
 * @param v       ApplySql->实体类型
 */
static <T> void parseApplySql(AbstractWrapper<T, ?, ?> wrapper, Object v) {
    if (v instanceof ApplySql) {
        ApplySql as = (ApplySql) v;
        ApplySqlSegment normalSegment = as.getNormalSegment();
        List<String> groupByList = as.getGroupByList();
        List<String> orderByList = as.getOrderByList();
        ApplySqlSegment havingSegment = as.getHavingSegment();
        // 自定义sql
        if (normalSegment != null) {
            wrapper.apply(normalSegment.getSqlSegment(), getValueArray(normalSegment.getValueList()));
        }
        Method doItMethod = ReflectUtil.getMethodByName(wrapper.getClass(), SqlTemplate.DO_IT.getTemplate());
        // group by
        if (CollUtil.isNotEmpty(groupByList)) {
            ISqlSegment seg = () -> CollUtil.join(groupByList, StrUtil.COMMA);
            ReflectUtil.invoke(wrapper, doItMethod, true, new ISqlSegment[] { SqlKeyword.GROUP_BY, seg });
        }
        // having
        if (havingSegment != null) {
            wrapper.having(havingSegment.getSqlSegment(), getValueArray(havingSegment.getValueList()));
        }
        // order by
        if (CollUtil.isNotEmpty(orderByList)) {
            ISqlSegment seg = () -> CollUtil.join(orderByList, StrUtil.COMMA);
            ReflectUtil.invoke(wrapper, doItMethod, true, new ISqlSegment[] { SqlKeyword.ORDER_BY, seg });
        }
    }
}
Also used : ISqlSegment(com.baomidou.mybatisplus.core.conditions.ISqlSegment) Method(java.lang.reflect.Method) ApplySql(com.chao.cloud.common.extra.mybatis.common.ApplySql) ApplySqlSegment(com.chao.cloud.common.extra.mybatis.common.ApplySql.ApplySqlSegment)

Aggregations

ISqlSegment (com.baomidou.mybatisplus.core.conditions.ISqlSegment)1 ApplySql (com.chao.cloud.common.extra.mybatis.common.ApplySql)1 ApplySqlSegment (com.chao.cloud.common.extra.mybatis.common.ApplySql.ApplySqlSegment)1 Method (java.lang.reflect.Method)1