86 lines
3.4 KiB
Java
86 lines
3.4 KiB
Java
package com.njcn.influx.base;
|
||
|
||
|
||
import cn.hutool.core.util.ArrayUtil;
|
||
import com.njcn.influx.ano.Delete;
|
||
import com.njcn.influx.ano.Select;
|
||
import com.njcn.influx.ano.Insert;
|
||
import com.njcn.influx.core.InfluxExecutor;
|
||
import com.njcn.influx.core.ParameterHandler;
|
||
import com.njcn.influx.core.ResultSetHandler;
|
||
import com.njcn.influx.query.InfluxQueryWrapper;
|
||
|
||
import java.lang.annotation.Annotation;
|
||
import java.lang.reflect.InvocationHandler;
|
||
import java.lang.reflect.Method;
|
||
import java.lang.reflect.Parameter;
|
||
import java.util.List;
|
||
|
||
/**
|
||
* @author hongawen
|
||
*/
|
||
public class ProxyMapper implements InvocationHandler {
|
||
|
||
private ParameterHandler parameterHandler;
|
||
private InfluxExecutor executor;
|
||
private ResultSetHandler resultSetHandler;
|
||
|
||
public ProxyMapper(ParameterHandler parameterHandler, InfluxExecutor executor, ResultSetHandler resultSetHandler) {
|
||
this.parameterHandler = parameterHandler;
|
||
this.executor = executor;
|
||
this.resultSetHandler = resultSetHandler;
|
||
}
|
||
|
||
@Override
|
||
public Object invoke(Object proxy, Method method, Object[] args) {
|
||
Annotation[] annotations = method.getAnnotations();
|
||
//没有注解的。查看参数是否为InfluxQueryWrapper
|
||
if (ArrayUtil.isEmpty(annotations)) {
|
||
Parameter[] parameters = method.getParameters();
|
||
if (args.length == 1 && args[0] instanceof InfluxQueryWrapper) {
|
||
InfluxQueryWrapper influxQueryWrapper = (InfluxQueryWrapper) args[0];
|
||
Class resultType = influxQueryWrapper.getResultEntity();
|
||
List<Object> resultList = executor.select(influxQueryWrapper.generateSql(), resultType);
|
||
//根据返回类型返回结果
|
||
Class<?> returnType = method.getReturnType();
|
||
return resultSetHandler.handleResultSet(resultList, returnType);
|
||
}
|
||
}
|
||
|
||
//采用注解的形式,即将sql写在注解里
|
||
if (annotations.length == 1) {
|
||
Annotation annotation = annotations[0];
|
||
Class<? extends Annotation> annotationType = annotation.annotationType();
|
||
//是查询的
|
||
if (annotationType == Select.class) {
|
||
Select selectAnnotation = method.getAnnotation(Select.class);
|
||
//拼接sql
|
||
String sql = selectAnnotation.value();
|
||
Parameter[] parameters = method.getParameters();
|
||
sql = parameterHandler.handleParameter(parameters, args, sql);
|
||
//查询结果
|
||
Class resultType = selectAnnotation.resultType();
|
||
List<Object> list = executor.select(sql, resultType);
|
||
//根据返回类型返回结果
|
||
Class<?> returnType = method.getReturnType();
|
||
return resultSetHandler.handleResultSet(list, returnType);
|
||
}
|
||
//是插入的
|
||
if (annotationType == Insert.class) {
|
||
executor.insert(args);
|
||
}
|
||
//是删除的
|
||
if (annotationType == Delete.class) {
|
||
Delete deleteAnnotation = method.getAnnotation(Delete.class);
|
||
//拼接sql
|
||
String sql = deleteAnnotation.value();
|
||
Parameter[] parameters = method.getParameters();
|
||
sql = parameterHandler.handleParameter(parameters, args, sql);
|
||
//执行sql
|
||
executor.delete(sql);
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
}
|