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

jasmine.framework.test.liquibase.loader.DefaultTestDataLoader Maven / Gradle / Ivy

There is a newer version: 1.3.8
Show newest version
package jasmine.framework.test.liquibase.loader;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.context.ApplicationContext;

import java.util.List;

/**
 * @author mh.z
 */
public class DefaultTestDataLoader extends AbstractTestDataLoader {
    private BaseMapper baseMapper;

    /** 实体类的名称后缀 */
    private static final String ENTITY_SUFFIX_1 = "DO";
    private static final String ENTITY_SUFFIX_2 = "EO";
    /** mapper的名称后缀 */
    private static final String MAPPER_SUFFIX = "Mapper";

    @Override
    public void init(ApplicationContext applicationContext, Class type) {
        super.init(applicationContext, type);

        String mapperBeanName = getMapperBeanName(type);
        baseMapper = (BaseMapper) applicationContext.getBean(mapperBeanName);
    }

    @Override
    protected void insertRecords(List records) {
        Assert.notNull(records, "records null");

        for (Object record : records) {
            baseMapper.insert(record);
        }
    }

    @Override
    protected void deleteRecords(List recordIds) {
        Assert.notNull(recordIds, "recordIds null");

        baseMapper.deleteBatchIds(recordIds);
    }

    /**
     * 返回指定类型对应的 mapper 的名称
     *
     * @param type
     * @return
     */
    protected String getMapperBeanName(Class type) {
        Assert.notNull(type, "type null");

        String simpleClassName = type.getSimpleName();
        String mapperPrefix = StrUtil.lowerFirst(simpleClassName);

        if (mapperPrefix.endsWith(ENTITY_SUFFIX_1)
                || mapperPrefix.endsWith(ENTITY_SUFFIX_2)) {
            mapperPrefix = mapperPrefix.substring(0, mapperPrefix.length() - 2);
        }

        String mapperBeanName = mapperPrefix + MAPPER_SUFFIX;
        return mapperBeanName;
    }

}