Search in sources :

Example 1 with ApplySqlSegment

use of com.chao.cloud.common.extra.mybatis.common.ApplySql.ApplySqlSegment 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)

Example 2 with ApplySqlSegment

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

the class MybatisUtil method buildApplySql.

/**
 * 构造applysql
 *
 * @param <T>       实体类类型
 * @param wrapper   构造条件
 * @param beanClass 实体类类型
 * @return {@link ApplySql}
 */
static <T> ApplySql buildApplySql(LambdaQueryWrapper<T> wrapper, Class<T> beanClass) {
    // sql片段
    ApplySqlSegment normalSegment = null;
    List<String> groupByList = Collections.emptyList();
    ApplySqlSegment havingSegment = null;
    List<String> orderByList = Collections.emptyList();
    // 判断bean是否加载
    TableInfo info = FunctionUtil.buildTableInfo(beanClass);
    Assert.notNull(info, "无效的数据实体 beanClass={}", beanClass.getName());
    // 获取groupBy和orderBy
    MergeSegments segments = wrapper.getExpression();
    if (segments == null) {
        return null;
    }
    // 1.where之后的条件
    normalSegment = buildApplySqlSegment(segments.getNormal(), wrapper.getParamNameValuePairs());
    // 2.group by
    groupByList = CollUtil.map(segments.getGroupBy(), ISqlSegment::getSqlSegment, true);
    // 3.having 之后的条件
    havingSegment = buildApplySqlSegment(segments.getHaving(), wrapper.getParamNameValuePairs());
    // 4.order by
    orderByList = CollUtil.map(segments.getOrderBy(), ISqlSegment::getSqlSegment, true);
    return // 
    ApplySql.of().setNormalSegment(// 
    normalSegment).setGroupByList(// 
    groupByList).setHavingSegment(// 
    havingSegment).setOrderByList(orderByList);
}
Also used : MergeSegments(com.baomidou.mybatisplus.core.conditions.segments.MergeSegments) TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo) ApplySqlSegment(com.chao.cloud.common.extra.mybatis.common.ApplySql.ApplySqlSegment)

Aggregations

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