All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kukababy.plus.utils.PlusUtils Maven / Gradle / Ivy

/**
 * 
 */
package com.kukababy.plus.utils;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

import com.kukababy.plus.annotation.Column;
import com.kukababy.plus.annotation.GeneratedValue;
import com.kukababy.plus.annotation.Id;
import com.kukababy.plus.annotation.NoColumn;
import com.kukababy.plus.annotation.Table;
import com.kukababy.plus.exception.PlusRuntimeException;
import com.kukababy.plus.pager.Filter;
import com.kukababy.plus.pager.SqlFilter;
import com.kukababy.plus.pojo.Param;
import com.kukababy.plus.pojo.PlusCols;
import com.kukababy.plus.pojo.PlusMap;
import com.kukababy.plus.pojo.PlusPO;
import com.kukababy.plus.pojo.PlusVals;

/**
 * 
 * 
* *
 * 描述:
 * 
* *
* * @author [email protected] * @date 2019年3月5日 下午10:48:48 */ public class PlusUtils { /** * *
	描述:反射PlusPO类的列名,对象名相关属性
*
* * @see com.kukababy.plus.pojo.PlusCols * @param entityClass * @return PlusCols */ public static PlusCols getPlusCols(Class entityClass) { PlusCols plusCols = new PlusCols(); handleCommon(plusCols, entityClass); while (entityClass != null) {// 当父类为null的时候说明到达了最上层的父类(Object类). Field[] fields = entityClass.getDeclaredFields(); try { for (int i = 0; i < fields.length; i++) { Field field = fields[i]; field.setAccessible(true); String varName = field.getName(); String colName = varName; if (isPrimaryKey(plusCols, field)) { Column column = field.getAnnotation(Column.class); if (column != null) { if (!column.name().equals("")) { colName = column.name(); } } plusCols.getCol2Var().put(colName, varName); plusCols.setKey(colName); continue;// 有主键 } NoColumn noColumn = field.getAnnotation(NoColumn.class); if (noColumn != null) { if (!noColumn.name().equals("")) { colName = noColumn.name(); } plusCols.getCol2Var().put(colName, varName); plusCols.getNoCols().add(colName); continue;// 非字段列 } Column column = field.getAnnotation(Column.class); if (column != null) { if (!column.name().equals("")) { colName = column.name(); } } plusCols.getCol2Var().put(colName, varName); plusCols.getCols().add(colName); } } catch (IllegalArgumentException | IllegalAccessException e) { throw new PlusRuntimeException(e); } entityClass = (Class) entityClass.getSuperclass(); } return plusCols; } /** * *
	描述:反射对象的公共部分
1、表名 *
* * @param plusCols * @param entityClass */ private static void handleCommon(PlusCols plusCols, Class entityClass) { Table table = entityClass.getAnnotation(Table.class); String tableName = ""; if (table != null) { tableName = table.name(); } if (tableName.equals("")) { tableName = StringUtil.getTableName(entityClass.getSimpleName()); } plusCols.setTableName(tableName); } /** * *
	描述:达到对象的所有值部分
*
* * @param plusCols * @param entity * @return */ public static PlusVals getPlusVals(PlusCols plusCols, PlusPO entity) { PlusVals plusVals = new PlusVals(); Class entityClass = entity.getClass(); while (entityClass != null) {// 当父类为null的时候说明到达了最上层的父类(Object类). Field[] fields = entityClass.getDeclaredFields(); try { for (String colName : plusCols.getCols()) { for (Field field : fields) { field.setAccessible(true); String colTmp = field.getName(); if (colName.equals(colTmp)) { Object value = field.get(entity); plusVals.getVals().add(value); } } } for (Field field : fields) { field.setAccessible(true); String colTmp = field.getName(); if (plusCols.getKey().equals(colTmp)) { Object value = field.get(entity); plusVals.setKeyVal(value); } } } catch (IllegalArgumentException | IllegalAccessException e) { throw new PlusRuntimeException(e); } entityClass = (Class) entityClass.getSuperclass(); } return plusVals; } /** * *
	描述:反射变量是否有主键,以及主键生成的策略
*
* * @param plusCols * @param field * @return * @throws IllegalArgumentException * @throws IllegalAccessException */ private static boolean isPrimaryKey(PlusCols plusCols, Field field) throws IllegalArgumentException, IllegalAccessException { Id primaryKey = field.getAnnotation(Id.class); // 获取指定类型注解 if (primaryKey != null) { GeneratedValue generatedValue = field.getAnnotation(GeneratedValue.class); // 获取指定类型注解 if (generatedValue != null) { if (generatedValue.strategy().equals(Constant.IDENTITY)) { plusCols.setStrategy(Constant.IDENTITY); String type = field.getType().getSimpleName(); if (type.equals("int") || type.equals("Integer")) { plusCols.setKeyType(Constant.INT); } else if (type.equals("long") || type.equals("Long")) { plusCols.setKeyType(Constant.LONG); } else { throw new PlusRuntimeException("主键类型错误,支持整数类型!"); } } } return true;// 有主键 } return false; } /** * *
	描述:反射给对象的变量设置值
*
* * @param entity * @param fieldName * cityName * @param value */ public static void setValue(PlusPO entity, String fieldName, Object value) { PropertyDescriptor pd = null; try { pd = new PropertyDescriptor(fieldName, entity.getClass()); pd.getWriteMethod().invoke(entity, value); } catch (IntrospectionException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { throw new PlusRuntimeException(e); } } /** * *
	描述:通过列名得到变量名
*
* * @param colName * city_id * @param col2VarMap * {city_id:cityId} * @return */ public static String getVarNameByCol(String colName, Map col2VarMap) { for (Map.Entry entry : col2VarMap.entrySet()) { if (colName.equals(entry.getKey())) { return entry.getValue(); } } return null; } /** * *
	描述:通过变量名,得到数据库列名
*
* * @param varName * cityId * @param col2VarMap * {city_id:cityId} * @return city_id */ public static String getColNameByVar(String varName, Map col2VarMap) { for (Map.Entry entry : col2VarMap.entrySet()) { if (varName.equals(entry.getValue())) { return entry.getKey(); } } return varName; } /** * *
	描述:Map对象转PlusPO实体
已经指定列名和变量名之间的映射 *
* * @param col2VarMap * @param rowMap * @param entityClass * extends PlusPO * @return T extends PlusPO */ public static T map2PlusPO(Map col2VarMap, Map rowMap, Class entityClass) { try { T plusPO = entityClass.newInstance(); for (Map.Entry entry : rowMap.entrySet()) { String colName = entry.getKey(); String varName = getVarNameByCol(colName, col2VarMap); if (varName != null) { setValue(plusPO, varName, entry.getValue()); } } return plusPO; } catch (InstantiationException | IllegalAccessException e) { throw new PlusRuntimeException("反射赋值错误!"); } } /** *
	描述:Map对象转PlusPO实体
	rowMap 是数据库查询的一条数据
*
* * @param rowMap * @param entityClass * @return T extends PlusPO */ public static T map2PlusPO(Map rowMap, Class entityClass) { PlusCols plusCols = getPlusCols(entityClass); T plusPO = map2PlusPO(plusCols.getCol2Var(), rowMap, entityClass); return plusPO; } public static void varConvertCol(Map col2VarMap, Param... colVals) { for (Param param : colVals) { param.setCol(getColNameByVar(param.getCol(), col2VarMap)); } } /** * *
	描述:Map键值对参数,构建Sqlfilter对象
*
* * @param plusMap * {colName:colValue} * @return SqlFilter */ public static SqlFilter map2SqlFilter(PlusMap keyValMap) { SqlFilter sqlFilter = new SqlFilter(); List filters = sqlFilter.getFilters(); for (Map.Entry entry : keyValMap.entrySet()) { String key = entry.getKey(); Filter filter = new Filter(entry.getKey(), entry.getValue()); filters.add(filter); } return sqlFilter; } public static SqlFilter param2SqlFilter(Param... colVals) { SqlFilter sqlFilter = new SqlFilter(); List filters = sqlFilter.getFilters(); for (Param param : colVals) { filters.add(new Filter(param.getCol(), param.getVal())); } return sqlFilter; } public static void main(String args[]) { String sql = "seLect * from SelEct"; sql = sql.replaceAll("[sS][eE][lL][eE][cC][tT]", "select"); System.out.println(sql); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy