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

com.github.akwei.ohmybatis.IMapperFactory Maven / Gradle / Ivy

The newest version!
package com.github.akwei.ohmybatis;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("unchecked")
public class IMapperFactory implements ApplicationContextAware {

    private static ApplicationContext appctx = null;

    private static final Map> imapperMap = new HashMap<>();

    public static void addMapper(Class entityCls, Class mapperCls) {
        imapperMap.put(entityCls.getName(), mapperCls);
    }

    public static  IMapper getMapper(Class entityCls) {
        Class aClass = imapperMap.get(entityCls.getName());
        IMapper mapper = (IMapper) appctx.getBean(aClass);
        if (mapper == null) {
            throw new RuntimeException(entityCls.getName() + " must has mapper");
        }
        return mapper;
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        appctx = applicationContext;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy