package org.yeshi.utils.generater.vo.dao;
|
|
import org.springframework.data.annotation.Id;
|
import org.springframework.data.annotation.Transient;
|
import org.yeshi.utils.StringUtil;
|
import org.yeshi.utils.generater.annotation.admin.DaoQueryCondition;
|
import org.yeshi.utils.generater.entity.*;
|
import org.yeshi.utils.generater.mybatis.Column;
|
import org.yeshi.utils.generater.mybatis.ColumnParseUtil;
|
import org.yeshi.utils.generater.mybatis.Table;
|
import org.yeshi.utils.generater.vo.ExceptionVO;
|
|
import java.lang.annotation.Annotation;
|
import java.lang.reflect.Field;
|
import java.util.ArrayList;
|
import java.util.HashSet;
|
import java.util.List;
|
import java.util.Set;
|
|
public class MyBatisDBDaoVO extends BaseData {
|
|
private ClassInfo mapper;
|
private ClassInfo baseMapper;
|
//主键
|
private MybatisColumnData identify;
|
//属性
|
private List<MybatisColumnData> columnList;
|
private List<DaoQueryColumnData> queryList;
|
private String table;
|
|
public static class Builder {
|
|
private ClassInfo baseMapper;
|
private Class entity;
|
private ClassInfo mapper;
|
|
public Builder setBaseMapper(ClassInfo baseMapper) {
|
this.baseMapper = baseMapper;
|
return this;
|
}
|
|
public Builder setEntity(Class entity) {
|
this.entity = entity;
|
return this;
|
}
|
|
public Builder setMapper(ClassInfo mapper) {
|
this.mapper = mapper;
|
return this;
|
}
|
|
//获取table
|
|
private String getTableName(Class clz) throws Exception {
|
Annotation[] as = clz.getAnnotations();
|
for (Annotation a : as) {
|
if (a instanceof Table) {
|
Table t = (Table) a;
|
return t.value();
|
}
|
}
|
throw new Exception("未获取到表名称");
|
}
|
|
public MyBatisDBDaoVO build() throws Exception {
|
|
if (entity == null) {
|
throw new Exception("entity不能为空");
|
}
|
|
if (baseMapper == null) {
|
throw new Exception("baseMapper不能为空");
|
}
|
|
if (mapper == null) {
|
throw new Exception("mapper不能为空");
|
}
|
|
//获取table名称
|
String table = getTableName(entity);
|
|
Field[] fields = entity.getDeclaredFields();
|
MybatisColumnData identity = null;
|
List<MybatisColumnData> columlist = new ArrayList<>();
|
List<DaoQueryColumnData> daoQueryColumnList = new ArrayList<>();
|
for (Field fd : fields) {
|
//private/public 非final,static属性才会参与解析
|
if (fd.getModifiers() != 1 && fd.getModifiers() != 2) {
|
continue;
|
}
|
boolean valid = true;
|
|
Annotation[] as = fd.getAnnotations();
|
for (Annotation a : as) {
|
//不参与解析
|
if (a instanceof Transient || a instanceof java.beans.Transient) {
|
valid = false;
|
break;
|
}
|
}
|
|
if (!valid) {
|
continue;
|
}
|
|
String property = fd.getName();
|
String column = fd.getName();
|
for (Annotation a : as) {
|
//不参与解析
|
if (a instanceof Column) {
|
column = ((Column) a).name();
|
break;
|
}
|
}
|
|
MybatisColumnData columData = new MybatisColumnData(column, property, fd.getType().getSimpleName(), ColumnParseUtil.getJDBCType(fd.getType().getName()));
|
|
|
for (Annotation a : as) {
|
//主键
|
if (a instanceof Id) {
|
identity = columData;
|
identity.setType(fd.getType().getName());
|
valid = false;
|
break;
|
}
|
}
|
|
if (!valid) {
|
continue;
|
}
|
|
columlist.add(columData);
|
|
|
//获取查询条件
|
for (Annotation a : as) {
|
if (a instanceof DaoQueryCondition) {
|
if (fd.getType().getSimpleName().equalsIgnoreCase("Date")) {
|
daoQueryColumnList.add(new DaoQueryColumnData("max" + StringUtil.firstCharToUpper(columData.getProperty()), columData, fd.getType().getSimpleName(), DaoQueryCondition.QueryType.lt));
|
daoQueryColumnList.add(new DaoQueryColumnData("min" + StringUtil.firstCharToUpper(columData.getProperty()), columData, fd.getType().getSimpleName(), DaoQueryCondition.QueryType.gte));
|
} else {
|
daoQueryColumnList.add(new DaoQueryColumnData(columData.getProperty(), columData, fd.getType().getSimpleName(), ((DaoQueryCondition) a).queryType()));
|
}
|
break;
|
}
|
}
|
}
|
|
if (identity == null)
|
throw new Exception("尚未指定主键");
|
|
|
MyBatisDBDaoVO vo = new MyBatisDBDaoVO();
|
vo.setBaseMapper(baseMapper);
|
vo.setEntity(new ClassInfo(entity.getSimpleName(), entity.getName()));
|
vo.setIdentify(identity);
|
vo.setTable(table);
|
vo.setQueryList(daoQueryColumnList);
|
vo.setPackageName(mapper.getClazz().replace("." + mapper.getName(), ""));
|
vo.setMapper(mapper);
|
vo.setColumnList(columlist);
|
return vo;
|
}
|
}
|
|
public ClassInfo getMapper() {
|
return mapper;
|
}
|
|
public void setMapper(ClassInfo mapper) {
|
this.mapper = mapper;
|
}
|
|
public ClassInfo getBaseMapper() {
|
return baseMapper;
|
}
|
|
public void setBaseMapper(ClassInfo baseMapper) {
|
this.baseMapper = baseMapper;
|
}
|
|
|
public MybatisColumnData getIdentify() {
|
return identify;
|
}
|
|
public void setIdentify(MybatisColumnData identify) {
|
this.identify = identify;
|
}
|
|
public List<MybatisColumnData> getColumnList() {
|
return columnList;
|
}
|
|
public void setColumnList(List<MybatisColumnData> columnList) {
|
this.columnList = columnList;
|
}
|
|
public List<DaoQueryColumnData> getQueryList() {
|
return queryList;
|
}
|
|
public void setQueryList(List<DaoQueryColumnData> queryList) {
|
this.queryList = queryList;
|
}
|
|
public String getTable() {
|
return table;
|
}
|
|
public void setTable(String table) {
|
this.table = table;
|
}
|
}
|