org.fingertip.simpledao.utils.ModelUtil Maven / Gradle / Ivy
The newest version!
package org.fingertip.simpledao.utils;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.fingertip.simpledao.annotation.Id;
import org.fingertip.simpledao.annotation.Table;
import org.fingertip.simpledao.bean.ModelClassMessage;
import org.fingertip.simpledao.enums.ExecuteType;
import org.fingertip.simpledao.exception.DaoException;
public class ModelUtil {
/**
* 获取表名
* @param
* @return
* @throws DaoException
*/
public static String getTableName(Class entityClass) throws DaoException {
DaoException exception = new DaoException("未定义表");
if(entityClass == null){
throw new NullPointerException("实体类为空");
}
Table table = entityClass.getAnnotation(Table.class);
String value;
if(table != null){
value = table.value();
if(value == null || "".equals(value)){
throw exception;
}
}else{
String simpleName = entityClass.getSimpleName();
value = formatName(simpleName);
}
return value;
}
/**
* 获取模型类的相关信息
* @param entityClass
* @param executeType
* @return
* @throws DaoException
*/
public static ModelClassMessage getModelClassMessage(Class entityClass,ExecuteType executeType) throws DaoException{
ModelClassMessage message = new ModelClassMessage();
if(entityClass == null){
throw new NullPointerException("实体类为空");
}
if(executeType != ExecuteType.EXECUTE_TYPE_SELECT_SQL){
String tableName = null;
try {
tableName = ModelUtil.getTableName(entityClass);
} catch (Exception e) {
if(executeType != ExecuteType.EXECUTE_TYPE_SELECT_ENTITY){
throw e;
}
}
message.setTableName(tableName);
}
//获取字段信息
Map dbEntityFieldNames = new HashMap<>();
Map entityDbFieldNames = new HashMap<>();
Map dbFields = new HashMap<>();
Map entityFields = new HashMap<>();
List fields = new ArrayList<>();
try {
//子类参数
Field[] childFields = entityClass.getDeclaredFields();
if(childFields != null && childFields.length > 0){
fields.addAll(Arrays.asList(childFields));
}
//父类参数
Class> objClass = entityClass.getClass().getSuperclass();//第一个父类
for(;objClass != Object.class;objClass = objClass.getSuperclass()){
Field[] superFields = objClass.getDeclaredFields();
fields.addAll(Arrays.asList(superFields));
}
}catch (Exception e){}
for(Field field :fields){
if(field.getModifiers() != Modifier.PRIVATE){
continue;
}
org.fingertip.simpledao.annotation.Field anno = field.getAnnotation(org.fingertip.simpledao.annotation.Field.class);
String annoName = null;
String finalFieldName = null;
boolean specially = false;
if(anno != null){
if(executeType == ExecuteType.EXECUTE_TYPE_INSERT){
if(anno.ignoreInsert()){
continue;
}
}else if(executeType == ExecuteType.EXECUTE_TYPE_UPDATE){
if(anno.ignoreUpdate()){
continue;
}
}
annoName = anno.name();
if(annoName == null || annoName.equals("")){
finalFieldName = field.getName();
}else {
finalFieldName = annoName;
specially = true;
}
}
if(finalFieldName == null){
finalFieldName = field.getName();
}
if(!specially){
finalFieldName = formatName(finalFieldName);
}
dbEntityFieldNames.put(finalFieldName, field.getName());
entityDbFieldNames.put(field.getName(), finalFieldName);
dbFields.put(finalFieldName, field);
entityFields.put(field.getName(), field);
Id id = field.getAnnotation(Id.class);
if(id != null){
message.setIdName(finalFieldName);
}
}
if(dbEntityFieldNames.isEmpty()){
throw new DaoException("查询字段不明确");
}
message.setDbEntityFieldNames(dbEntityFieldNames);
message.setEntityDbFieldNames(entityDbFieldNames);
message.setDbFields(dbFields);
message.setEntityFields(entityFields);
return message;
}
/**
* 实体转为参数数组的集合
* 一个实体对应集合的一个元素(参数数组数组)
* @param entities
* @param fieldNames
* @param entityClass
* @return
*/
public static List