com.kukababy.plus.utils.SqlUtil Maven / Gradle / Ivy
The newest version!
/**
*
*/
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.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.alibaba.fastjson.JSON;
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.Orderby;
import com.kukababy.plus.pager.SqlFilter;
import com.kukababy.plus.pager.SqlInfo;
import com.kukababy.plus.pager.SqlWhere;
import com.kukababy.plus.pojo.P;
import com.kukababy.plus.pojo.SqlPo;
import com.kukababy.plus.pojo.SqlVal;
/**
*
*
*
*
* 描述:
*
*
*
*
* @author [email protected]
* @date 2019年3月5日 下午10:48:48
*/
public class SqlUtil {
/**
*
* 描述:MongoFilter变量名转字段名
*
*
* @param mongoFilter
* @param col2VarMap
*/
@Deprecated
public static void convertSqlFilterVar2Col(SqlFilter sqlFilter, Map col2VarMap) {
if (sqlFilter.getFilters() != null) {
for (Filter filter : sqlFilter.getFilters()) {
convertFilterVar2Col(filter, col2VarMap);
}
}
if (sqlFilter.getOrderbys() != null) {
convertOrderbyVar2Col(sqlFilter.getOrderbys(), col2VarMap);
}
}
/**
*
* 描述:Filter变量名转字段名
*
*
* @param filter
* @param col2VarMap
*/
public static void convertFilterVar2Col(Filter filter, Map col2VarMap) {
for (Entry entry : col2VarMap.entrySet()) {
if (entry.getValue().equals(filter.getCol())) {// 使用的是变量名
filter.setCol(entry.getKey());// 改成字段名
break;
}
}
if (filter.getFilters() != null) {// 下级继续转换
for (Filter childFilter : filter.getFilters()) {
convertFilterVar2Col(childFilter, col2VarMap);
}
}
}
/**
*
* 描述:排序字段变量名转字段名
*
*
* @param sorts
* @param col2VarMap
*/
public static void convertOrderbyVar2Col(List orderbys, Map col2VarMap) {
for (Orderby orderby : orderbys) {
for (Entry entry : col2VarMap.entrySet()) {
if (entry.getValue().equals(orderby.getCol())) {// 使用的是变量名
orderby.setCol(entry.getKey());// 改成字段名
break;
}
}
}
}
/**
*
*
描述:反射PlusPO类的列名,对象名相关属性
*
*
* @see com.kukababy.plus.pojo.SqlPo
* @param entityClass
* @return PlusCols
*/
public static SqlPo getSqlPo(Class> entityClass, boolean camel) {
SqlPo sqlPo = new SqlPo();
handleCommon(sqlPo, 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 (camel) {
colName = StringUtil.camel2Underline(colName);
}
if (isPrimaryKey(sqlPo, field)) {
Column column = field.getAnnotation(Column.class);
if (column != null) {
if (!column.name().equals("")) {
colName = column.name();
}
}
sqlPo.getCol2VarMap().put(colName, varName);
sqlPo.setKey(colName);
continue;// 有主键
}
NoColumn noColumn = field.getAnnotation(NoColumn.class);
if (noColumn != null) {
if (!noColumn.name().equals("")) {
colName = noColumn.name();
}
sqlPo.getCol2VarMap().put(colName, varName);
sqlPo.getNoCols().add(colName);
continue;// 非字段列
}
Column column = field.getAnnotation(Column.class);
if (column != null) {
if (!column.name().equals("")) {
colName = column.name();
}
}
sqlPo.getCol2VarMap().put(colName, varName);
sqlPo.getCols().add(colName);
}
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new PlusRuntimeException(e);
}
entityClass = (Class>) entityClass.getSuperclass();
}
return sqlPo;
}
public static SqlPo getSqlPo(Class> entityClass) {
return getSqlPo(entityClass, false);
}
/**
*
*
描述:反射对象的公共部分
1、表名
*
*
* @param plusCols
* @param entityClass
*/
private static void handleCommon(SqlPo sqlPo, Class> entityClass) {
Table table = entityClass.getAnnotation(Table.class);
String tableName = "";
if (table != null) {
tableName = table.name();
}
if (tableName.equals("")) {
tableName = StringUtil.getTableName(entityClass.getSimpleName());
}
sqlPo.setTableName(tableName);
}
/**
*
*
描述:得到对象的所有值部分
*
*
* @param plusCols
* @param entity
* @return
*/
public static SqlVal getSqlVal(SqlPo sqlPo, Object entity) {
SqlVal sqlVal = new SqlVal();
Class> entityClass = entity.getClass();
while (entityClass != null) {// 当父类为null的时候说明到达了最上层的父类(Object类).
Field[] fields = entityClass.getDeclaredFields();
try {
for (Field field : fields) {
field.setAccessible(true);
String varTmp = field.getName();
for (String colName : sqlPo.getCols()) {
String varName = sqlPo.getCol2VarMap().get(colName);
if (varName.equals(varTmp)) {
Object value = field.get(entity);
sqlVal.getVals().add(value);
}
}
}
for (Field field : fields) {
field.setAccessible(true);
String varName = sqlPo.getCol2VarMap().get(sqlPo.getKey());
String varTmp = field.getName();
if (varName.equals(varTmp)) {
Object value = field.get(entity);
sqlVal.setKeyVal(value);
}
}
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new PlusRuntimeException(e);
}
entityClass = (Class>) entityClass.getSuperclass();
}
return sqlVal;
}
/**
*
*
描述:反射变量是否有主键,以及主键生成的策略
*
*
* @param plusCols
* @param field
* @return
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
private static boolean isPrimaryKey(SqlPo sqlPo, 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)) {
sqlPo.setStrategy(Constant.IDENTITY);
String type = field.getType().getSimpleName();
if (type.equals("int") || type.equals("Integer")) {
sqlPo.setKeyType(Constant.INT);
} else if (type.equals("long") || type.equals("Long")) {
sqlPo.setKeyType(Constant.LONG);
} else {
throw new PlusRuntimeException("主键类型错误,支持整数类型!");
}
}
}
return true;// 有主键
}
return false;
}
/**
*
*
描述:反射给对象的变量设置值
*
*
* @param entity
* @param fieldName
* cityName
* @param value
*/
public static void setValue(Object entity, String varName, Object value) {
PropertyDescriptor pd = null;
try {
pd = new PropertyDescriptor(varName, entity.getClass());
pd.getWriteMethod().invoke(entity, value);
} catch (IntrospectionException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new PlusRuntimeException(e);
}
}
/**
*
*
描述:通过变量名,得到数据库列名
*
*
* @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
*/
private 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 = col2VarMap.get(colName);
if (varName != null) {
setValue(plusPO, varName, entry.getValue());
}
}
return plusPO;
} catch (InstantiationException | IllegalAccessException e) {
throw new PlusRuntimeException("反射赋值错误!");
}
}
/**
*
描述:Map对象转PlusPO实体
rowMap 是数据库查询的一条数据
*
*
* @param rowMap
* @param entityClass
*/
public static T map2PlusPO(Map rowMap, Class entityClass) {
return map2PlusPO(rowMap, entityClass, false);
}
public static T map2PlusPO(Map rowMap, Class entityClass, boolean camel) {
SqlPo sqlPo = getSqlPo(entityClass, camel);
T plusPO = map2PlusPO(sqlPo.getCol2VarMap(), rowMap, entityClass);
return plusPO;
}
public static void varConvertCol(Map col2VarMap, P... colVals) {
for (P param : colVals) {
param.setCol(getColNameByVar(param.getCol(), col2VarMap));
}
}
/**
*
*
描述:Map键值对参数,构建Sqlfilter对象
*
*
* @param plusMap
* {colName:colValue}
* @return SqlFilter
*/
public static SqlFilter map2SqlFilter(Map keyValMap) {
SqlFilter sqlFilter = new SqlFilter();
List filters = sqlFilter.getFilters();
for (Map.Entry entry : keyValMap.entrySet()) {
Filter filter = new Filter(entry.getKey(), entry.getValue());
filters.add(filter);
}
return sqlFilter;
}
public static SqlFilter param2SqlFilter(P... colVals) {
SqlFilter sqlFilter = new SqlFilter();
List filters = sqlFilter.getFilters();
for (P param : colVals) {
filters.add(new Filter(param.getCol(), param.getVal()));
}
return sqlFilter;
}
public static SqlFilter list2SqlFilter(List colVals) {
SqlFilter sqlFilter = new SqlFilter();
List filters = sqlFilter.getFilters();
for (P 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