com.github.akwei.ohmybatis.IMapperFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ohmybatis Show documentation
Show all versions of ohmybatis Show documentation
can use insert object, inserBatch, update object, deleteById, selectById
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;
}
}