All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.developframework.resource.spring.mybatis.MybatisResourceManager Maven / Gradle / Ivy

The newest version!
package com.github.developframework.resource.spring.mybatis;

import com.github.developframework.resource.*;
import com.github.developframework.resource.spring.mybatis.utils.WhereBuilder;
import develop.toolkit.base.struct.Pager;
import develop.toolkit.base.struct.PagerResult;
import develop.toolkit.base.struct.TwoValues;
import develop.toolkit.base.utils.CollectionAdvice;
import lombok.Getter;

import javax.annotation.PostConstruct;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * Mybatis资源管理器
 *
 * @author qiushui on 2020-05-28.
 */
public class MybatisResourceManager<
        PO extends MPO,
        ID extends Serializable,
        DAOMAPPER extends BaseDaoMapper
        > extends AbstractResourceManager {

    @Getter
    protected final DAOMAPPER daoMapper;

    public MybatisResourceManager(DAOMAPPER daoMapper, Class entityClass, String resourceName) {
        super(new ResourceDefinition<>(entityClass, resourceName));
        this.daoMapper = daoMapper;
    }

    @PostConstruct
    public void init() {
        this.resourceHandler = new MybatisResourceHandler<>(daoMapper, resourceDefinition);
        this.resourceOperateRegistry = new ResourceOperateRegistry<>(this);
    }

    @Override
    public  AddCheckExistsLogic byFieldAddCheck(Class dtoClass, String... fields) {
        return new ByFieldMybatisAddCheckExistsLogic<>(resourceDefinition, daoMapper, fields);
    }

    @Override
    public  ModifyCheckExistsLogic byFieldModifyCheck(Class dtoClass, String... fields) {
        return new ByFieldMybatisModifyCheckExistsLogic<>(resourceDefinition, daoMapper, fields);
    }

    @Override
    public List listForIds(String id, ID[] ids) {
        List list = daoMapper.findListByWhere(
                resourceDefinition.getEntityClass(),
                new WhereBuilder().in(id, ids).build(),
                null,
                null
        );
        return CollectionAdvice.sort(list, ids, (po, i) -> po.getId().equals(i));
    }

    @Override
    public List listForIds(String id, Collection ids) {
        List list = daoMapper.findListByWhere(
                resourceDefinition.getEntityClass(),
                new WhereBuilder().in(id, ids).build(),
                null,
                null
        );
        return ids instanceof List ? CollectionAdvice.sort(list, ids, (po, i) -> po.getId().equals(i)) : list;
    }

    public > List list(SEARCH search) {
        List list = resourceHandler.query(search);
        return execSearchOperate(list);
    }

    public > List list(SEARCH search, OrderBy... orderByArray) {
        List list = daoMapper.findList(resourceDefinition.getEntityClass(), search, orderByArray, null);
        return execSearchOperate(list);
    }

    public > PagerResult pager(Pager pager, SEARCH search, OrderBy... orderByArray) {
        TwoValues limit = TwoValues.of(pager.getOffset(), pager.getSize());
        List list = daoMapper.findList(resourceDefinition.getEntityClass(), search, orderByArray, limit);
        long total = daoMapper.countBy(resourceDefinition.getEntityClass(), search);
        return new PagerResult<>(pager, execSearchOperate(list), total);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy