com.tmsps.ne4spring.orm.ORMUtil Maven / Gradle / Ivy
package com.tmsps.ne4spring.orm;
import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.tmsps.ne4spring.orm.model.DataModel;
import com.tmsps.ne4spring.utils.ChkUtil;
/**
* @author zhangwei [email protected]
*/
public class ORMUtil {
private static Logger log = LoggerFactory.getLogger("Ne4Spring");
public static List getColumnLables(ResultSet rs) {
List columnLables = new ArrayList();
ResultSetMetaData metaData = null;
try {
metaData = rs.getMetaData();
int count = metaData.getColumnCount();
for (int i = 0; i < count; i++) {
columnLables.add(metaData.getColumnLabel(i + 1));
}
} catch (SQLException e) {
e.printStackTrace();
}
return columnLables;
}
public static T fillMapToBean(final Class> clazz, Map map) {
if (ChkUtil.isNull(map)) {
return null;
}
try {
@SuppressWarnings("unchecked")
T obj = (T) clazz.getDeclaredConstructor().newInstance();
List fs = ClassUtil.getClassFields(clazz);
for (Field field : fs) {
Object val = map.get(field.getName());
ClassUtil.setClassVal(field, obj, val);
}
return obj;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static T fillMapToAnyBean(final Class clazz, Map map) {
if (ChkUtil.isNull(map)) {
return null;
}
try {
T obj = clazz.getDeclaredConstructor().newInstance();
List fs = ClassUtil.getClassFields(clazz);
for (Field field : fs) {
Object val = map.get(field.getName());
ClassUtil.setClassVal(field, obj, val);
}
return obj;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String replaceFormatSqlOrderBy(String sql) {
sql = sql.replaceAll("(\\s)+", " ");
int index = sql.toLowerCase().lastIndexOf("order by");
if (index > sql.toLowerCase().lastIndexOf(")")) {
String sql1 = sql.substring(0, index);
String sql2 = sql.substring(index);
sql2 = sql2.replaceAll("[oO][rR][dD][eE][rR] [bB][yY] [\u4e00-\u9fa5a-zA-Z0-9_.]+((\\s)+(([dD][eE][sS][cC])|([aA][sS][cC])))?(( )*,( )*[\u4e00-\u9fa5a-zA-Z0-9_.]+(( )+(([dD][eE][sS][cC])|([aA][sS][cC])))?)*", "");
return sql1 + sql2;
}
return sql;
}
/**
* @author zhangwei [email protected]
*/
public static Map convertResultSetToMap(ResultSet rs) {
Map map = new HashMap();
try {
ResultSetMetaData rm = rs.getMetaData();
int columnCount = rm.getColumnCount();
for (int i = 1; i <= columnCount; i++) {
map.put(rm.getColumnLabel(i), rs.getObject(i));
}
} catch (SQLException e) {
e.printStackTrace();
log.error(e.getMessage());
}
return map;
}
public static T fillResultSetToBean(final ResultSet rs, final Class clazz) {
if (ChkUtil.isNotNull(rs)) {
return fillMapToAnyBean(clazz, convertResultSetToMap(rs));
}
return null;
}
public static List fillMapToList(final List