io.github.nichetoolkit.mybatis.defaults.DefaultSqlScriptResolver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mybatis-toolkit-context Show documentation
Show all versions of mybatis-toolkit-context Show documentation
Mybatis toolkit context project for Spring Boot
The newest version!
package io.github.nichetoolkit.mybatis.defaults;
import io.github.nichetoolkit.mybatis.*;
import io.github.nichetoolkit.mybatis.error.MybatisSqlScriptLackError;
import io.github.nichetoolkit.rest.util.GeneralUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.builder.annotation.ProviderContext;
import org.springframework.core.annotation.AnnotationUtils;
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
/**
* DefaultSqlScriptResolver
* The default sql script resolver class.
* @author Cyan ([email protected])
* @see io.github.nichetoolkit.mybatis.MybatisSqlScriptResolver
* @see lombok.extern.slf4j.Slf4j
* @since Jdk1.8
*/
@Slf4j
public class DefaultSqlScriptResolver implements MybatisSqlScriptResolver {
@Override
public MybatisSqlScript resolve(ProviderContext context, MybatisTable table, MybatisSqlScript sqlScript) {
Class> mapperType = context.getMapperType();
Method mapperMethod = context.getMapperMethod();
/* 接口注解 */
List resolvers = parseAnnotations(mapperType, ElementType.TYPE, mapperType.getAnnotations());
/* 方法注解 */
resolvers.addAll(parseAnnotations(mapperMethod, ElementType.METHOD, mapperMethod.getAnnotations()));
/* 参数注解 */
Parameter[] parameters = mapperMethod.getParameters();
Annotation[][] parameterAnnotations = mapperMethod.getParameterAnnotations();
for (int i = 0; i < parameters.length; i++) {
resolvers.addAll(parseAnnotations(parameters[i], ElementType.PARAMETER, parameterAnnotations[i]));
}
/* 去重,排序 */
resolvers = resolvers.stream().distinct()
.sorted(Comparator.comparing(wrapper -> ((MybatisOrder) wrapper).getOrder()).reversed())
.collect(Collectors.toList());
for (MybatisSqlScriptResolver resolver : resolvers) {
sqlScript = resolver.resolve(context, table, sqlScript);
}
return sqlScript;
}
/**
* parseAnnotations
* The parse annotations method.
* @param target {@link java.lang.Object} The target parameter is Object
type.
* @param type {@link java.lang.annotation.ElementType} The type parameter is ElementType
type.
* @param annotations {@link java.lang.annotation.Annotation} The annotations parameter is Annotation
type.
* @return {@link java.util.List} The parse annotations return object is List
type.
* @see java.lang.Object
* @see java.lang.annotation.ElementType
* @see java.lang.annotation.Annotation
* @see java.util.List
*/
private List parseAnnotations(Object target, ElementType type, Annotation[] annotations) {
List> classes = new ArrayList<>();
for (Annotation annotation : annotations) {
Class extends Annotation> annotationType = annotation.annotationType();
if (annotationType == MybatisSqlResolver.class) {
classes.addAll(Arrays.asList(((MybatisSqlResolver) annotation).value()));
} else if (annotationType.isAnnotationPresent(MybatisSqlResolver.class)) {
MybatisSqlResolver annotationTypeAnnotation = AnnotationUtils.getAnnotation(annotationType, MybatisSqlResolver.class);
if (GeneralUtils.isNotEmpty(annotationTypeAnnotation) && GeneralUtils.isNotEmpty(annotationTypeAnnotation.value())) {
classes.addAll(Arrays.asList(annotationTypeAnnotation.value()));
}
}
}
return classes.stream().map(c -> (DefaultSqlResolver) newInstance(c, target, type, annotations))
.collect(Collectors.toList());
}
/**
* newInstance
* The new instance method.
* @param {@link java.lang.Object} The parameter can be of any type.
* @param instanceClass {@link java.lang.Class} The instance class parameter is Class
type.
* @param target {@link java.lang.Object} The target parameter is Object
type.
* @param type {@link java.lang.annotation.ElementType} The type parameter is ElementType
type.
* @param annotations {@link java.lang.annotation.Annotation} The annotations parameter is Annotation
type.
* @return T The new instance return object is T
type.
* @see java.lang.Class
* @see java.lang.Object
* @see java.lang.annotation.ElementType
* @see java.lang.annotation.Annotation
*/
public T newInstance(Class instanceClass, Object target, ElementType type, Annotation[] annotations) {
try {
return instanceClass.getConstructor(Object.class, ElementType.class, Annotation[].class).newInstance(target, type, annotations);
} catch (InvocationTargetException | InstantiationException | IllegalAccessException |
NoSuchMethodException exception) {
GeneralUtils.printStackTrace(log,exception,true);
String message = "the sql script of '" + instanceClass + "' type to instance has error, " + exception.getMessage();
throw new MybatisSqlScriptLackError(message, exception);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy