com.ly.mybatis.mapperservice.util.MPSUtil Maven / Gradle / Ivy
The newest version!
package com.ly.mybatis.mapperservice.util;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ly.mybatis.mapperservice.model.*;
import com.ly.mybatis.mapperservice.service.builder.RepositoryTemplate;
import com.ly.mybatis.mapperservice.service.builder.RepositoryTemplateBuilder;
import com.ly.mybatis.mapperservice.service.core.*;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.*;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.ResolvableType;
import org.springframework.core.env.Environment;
import org.springframework.lang.Nullable;
import javax.annotation.Resource;
import java.io.IOException;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.util.*;
@SuppressWarnings({"all"})
public class MPSUtil {
private static final Set> entityClasses = new HashSet<>();
private static final Map, Class>> boMap = new HashMap<>();
private static final Map, Class>> formMap = new HashMap<>();
private static final Map, Class>> conditionFormMap = new HashMap<>();
private static final Map, Batch extends BaseMapper>, ?, ?, ?, ?>> batchMap = new HashMap<>();
private static final Map, Delete extends BaseMapper>, ?, ?, ?, ?>> deleteMap = new HashMap<>();
private static final Map, Select extends BaseMapper>, ?, ?, ?, ?>> selectMap = new HashMap<>();
@Resource
private ApplicationContext applicationContext;
private static ApplicationContext applicationContextInstance;
@EventListener(ContextRefreshedEvent.class)
public void init() {
applicationContextInstance = applicationContext;
inject(batchMap, getBeansOfType(Batch.class));
inject(deleteMap, getBeansOfType(Delete.class));
inject(selectMap, getBeansOfType(Select.class));
Map mapperMap = applicationContext.getBeansOfType(BaseMapper.class);
for (Map.Entry entry : mapperMap.entrySet()) {
String s = entry.getKey();
BaseMapper mapper = entry.getValue();
try {
RepositoryTemplateBuilder builder = new RepositoryTemplateBuilder(applicationContext, mapper);
if (builder.parse()) {
RepositoryTemplate o = builder.getTemplate();
batchMap.putIfAbsent(builder.getEntityClass(), o);
deleteMap.putIfAbsent(builder.getEntityClass(), o);
selectMap.putIfAbsent(builder.getEntityClass(), o);
entityClasses.add(builder.getEntityClass());
boMap.putIfAbsent(builder.getBoClass(), builder.getEntityClass());
formMap.putIfAbsent(builder.getFormClass(), builder.getEntityClass());
conditionFormMap.putIfAbsent(builder.getConditionFormClass(), builder.getEntityClass());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static boolean isEntity(Object obj) {
return entityClasses.contains(getObjectClass(obj));
}
public static boolean isForm(Object obj) {
return formMap.containsKey(getObjectClass(obj));
}
public static boolean isBo(Object obj) {
return boMap.containsKey(getObjectClass(obj));
}
public static boolean isConditionForm(Object obj) {
return conditionFormMap.containsKey(getObjectClass(obj));
}
public static Class> getObjectClass(Object obj) {
if (obj == null) {
throw new RuntimeException("null值对象,无法解析所属类");
}
if (obj instanceof Class>) {
return (Class>) obj;
} else if (obj instanceof Collection>) {
Collection> collection = (Collection>) obj;
return collection.stream()
.findFirst()
.map(Object::getClass)
.orElseThrow(RuntimeException::new);
} else if (obj instanceof Map, ?>) {
Map, ?> map = (Map, ?>) obj;
return map.values().stream()
.findFirst()
.map(Object::getClass)
.orElseThrow(RuntimeException::new);
} else {
return obj.getClass();
}
}
public static Class getEntityClass(Object obj) {
Class> clazz = getObjectClass(obj);
if (entityClasses.contains(clazz)) {
return (Class) clazz;
}
if (formMap.containsKey(clazz)) {
return (Class) formMap.get(clazz);
}
if (boMap.containsKey(clazz)) {
return (Class) boMap.get(clazz);
}
if (conditionFormMap.containsKey(clazz)) {
return (Class) conditionFormMap.get(clazz);
}
throw new RuntimeException("没有支持的类:" + clazz.getName());
}
// save or update
public static Result insert(Object object) {
return MPSUtil.batchOptional(object)
.map(insert -> {
if (isForm(object)) {
return insert.insertForm((Form) object);
} else {
return insert.insert((Entity) object);
}
})
.orElseGet(() -> {
Result result = new Result<>();
result.setSuccess(false)
.setMessage("unknown error");
return result;
});
}
public static BatchResult insertBatch(
Collection> objects,
int batchSize
) {
if (objects.isEmpty()) {
return new BatchResult<>();
}
return MPSUtil.batchOptional(objects)
.map(batch -> {
if (isForm(objects)) {
return batch.insertFormBatch((Collection