初始化第一版influxORM
This commit is contained in:
26
src/main/java/com/njcn/influx/base/InfluxDbBaseMapper.java
Normal file
26
src/main/java/com/njcn/influx/base/InfluxDbBaseMapper.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package com.njcn.influx.base;
|
||||
|
||||
|
||||
import com.njcn.influx.ano.Insert;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author hongawen
|
||||
*/
|
||||
public interface InfluxDbBaseMapper<T> {
|
||||
|
||||
/***
|
||||
* 插入数据
|
||||
* @param entity 数据
|
||||
*/
|
||||
@Insert
|
||||
void insertOne(T entity);
|
||||
|
||||
/***
|
||||
* 批量插入数据
|
||||
* @param entityList 数据
|
||||
*/
|
||||
@Insert
|
||||
void insertBatch(List<T> entityList);
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.njcn.influx.base;
|
||||
|
||||
import com.njcn.influx.core.InfluxExecutor;
|
||||
import com.njcn.influx.core.ParameterHandler;
|
||||
import com.njcn.influx.core.ResultSetHandler;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.lang.reflect.Proxy;
|
||||
|
||||
public class InfluxProxyMapperFactory<T> implements FactoryBean {
|
||||
|
||||
@Resource
|
||||
InfluxExecutor executor;
|
||||
|
||||
private Class<T> interfaceClass;
|
||||
|
||||
public InfluxProxyMapperFactory(Class<T> interfaceClass) {
|
||||
this.interfaceClass = interfaceClass;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getObject() throws Exception {
|
||||
Object proxyInstance = Proxy.newProxyInstance(
|
||||
interfaceClass.getClassLoader(), new Class[]{interfaceClass}, new ProxyMapper(new ParameterHandler(), executor, new ResultSetHandler()));
|
||||
return proxyInstance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getObjectType() {
|
||||
return interfaceClass;
|
||||
}
|
||||
}
|
||||
68
src/main/java/com/njcn/influx/base/ProxyMapper.java
Normal file
68
src/main/java/com/njcn/influx/base/ProxyMapper.java
Normal file
@@ -0,0 +1,68 @@
|
||||
package com.njcn.influx.base;
|
||||
|
||||
|
||||
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 java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Parameter;
|
||||
import java.util.List;
|
||||
|
||||
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) throws Throwable {
|
||||
Annotation[] annotations = method.getAnnotations();
|
||||
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();
|
||||
|
||||
String database = deleteAnnotation.database();
|
||||
sql = parameterHandler.handleParameter(parameters, args, sql);
|
||||
//执行sql
|
||||
executor.delete(sql, database);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
70
src/main/java/com/njcn/influx/base/ProxyMapperRegister.java
Normal file
70
src/main/java/com/njcn/influx/base/ProxyMapperRegister.java
Normal file
@@ -0,0 +1,70 @@
|
||||
package com.njcn.influx.base;
|
||||
|
||||
import com.njcn.influx.utils.InfluxStrUtil;
|
||||
import com.njcn.influx.utils.ManualRegisterBeanUtil;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.core.io.support.ResourcePatternResolver;
|
||||
import org.springframework.core.io.support.ResourcePatternUtils;
|
||||
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
|
||||
import org.springframework.core.type.classreading.MetadataReader;
|
||||
import org.springframework.core.type.classreading.MetadataReaderFactory;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.SystemPropertyUtils;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.io.IOException;
|
||||
import java.util.HashSet;
|
||||
|
||||
public class ProxyMapperRegister {
|
||||
|
||||
private String mapperLocation;
|
||||
ConfigurableApplicationContext applicationContext;
|
||||
ResourceLoader resourceLoader;
|
||||
|
||||
public ProxyMapperRegister(String mapperLocation, ConfigurableApplicationContext applicationContext, ResourceLoader resourceLoader) {
|
||||
this.mapperLocation = mapperLocation;
|
||||
this.applicationContext = applicationContext;
|
||||
this.resourceLoader = resourceLoader;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void afterInit() {
|
||||
//获取mapper包下所有的Mapper
|
||||
HashSet<Class<?>> classes = new HashSet<>();
|
||||
String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
|
||||
.concat(ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(mapperLocation))
|
||||
.concat("/**/*.class"));
|
||||
ResourcePatternResolver resolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
|
||||
MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resourceLoader);
|
||||
MetadataReader metadataReader;
|
||||
try {
|
||||
Resource[] resources = resolver.getResources(packageSearchPath);
|
||||
for (Resource resource : resources) {
|
||||
if (resource.isReadable()) {
|
||||
metadataReader = metadataReaderFactory.getMetadataReader(resource);
|
||||
String className = metadataReader.getClassMetadata().getClassName();
|
||||
Class<?> aClass = Class.forName(className);
|
||||
|
||||
//当这个Mapper实现InfluxBaseMapper时加集合
|
||||
Class<?>[] interfaces = aClass.getInterfaces();
|
||||
for (Class<?> anInterface : interfaces) {
|
||||
if (anInterface == InfluxDbBaseMapper.class) {
|
||||
classes.add(aClass);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
for (Class<?> aClass : classes) {
|
||||
//注册接口类至工厂Bean,再用动态代理生成对应Mapper
|
||||
ManualRegisterBeanUtil.registerBean(applicationContext, InfluxStrUtil.captureName(aClass.getSimpleName()), InfluxProxyMapperFactory.class, aClass);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user