Commit a6af90cb authored by yanzg's avatar yanzg

将源码打包进jar包

parent 92e9827a
...@@ -8,7 +8,8 @@ import com.yanzuoguang.util.base.ObjectHelper; ...@@ -8,7 +8,8 @@ import com.yanzuoguang.util.base.ObjectHelper;
import com.yanzuoguang.util.helper.StringHelper; import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.log.Log; import com.yanzuoguang.util.log.Log;
import com.yanzuoguang.util.vo.MapRow; import com.yanzuoguang.util.vo.MapRow;
import org.springframework.beans.*; import org.springframework.beans.BeanUtils;
import org.springframework.beans.TypeMismatchException;
import org.springframework.dao.DataRetrievalFailureException; import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.JdbcUtils; import org.springframework.jdbc.support.JdbcUtils;
...@@ -29,10 +30,15 @@ import java.util.Map; ...@@ -29,10 +30,15 @@ import java.util.Map;
*/ */
public class AllBeanRowMapper<T> implements RowMapper<T> { public class AllBeanRowMapper<T> implements RowMapper<T> {
/**
* 缓存的处理类
*/
private static final Map<Class<?>, Object> CACHE = new HashMap<>();
/** /**
* 需要映射的Class * 需要映射的Class
*/ */
private Class<T> mappedClass; private final Class<T> mappedClass;
/** /**
* 是否属于映射方式 * 是否属于映射方式
...@@ -52,20 +58,33 @@ public class AllBeanRowMapper<T> implements RowMapper<T> { ...@@ -52,20 +58,33 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
/** /**
* 构造函数 * 构造函数
* *
* @param mappedClass * @param mappedClass 类型
*/ */
public AllBeanRowMapper(Class<T> mappedClass, ConfigDb configDb) { public AllBeanRowMapper(Class<T> mappedClass, ConfigDb configDb) {
initialize(mappedClass); this.mappedClass = mappedClass;
this.configDb = configDb; this.configDb = configDb;
initialize();
} }
/** /**
* 初始化元素之间的映射关系 * 获取可以转化实体
* *
* @param mappedClass the mapped class. * @param cls 类型
* @param <T> 泛型类型
* @return 可转换的实体
*/ */
protected void initialize(Class<T> mappedClass) { public static <T> AllBeanRowMapper<T> getInstance(Class<T> cls, ConfigDb configDb) {
this.mappedClass = mappedClass; if (!CACHE.containsKey(cls)) {
CACHE.put(cls, new AllBeanRowMapper<>(cls, configDb));
}
Object o = CACHE.get(cls);
return (AllBeanRowMapper<T>) o;
}
/**
* 初始化元素之间的映射关系
*/
protected void initialize() {
if (ObjectHelper.isSub(MapRow.class, mappedClass) || ObjectHelper.isSub(Map.class, mappedClass)) { if (ObjectHelper.isSub(MapRow.class, mappedClass) || ObjectHelper.isSub(Map.class, mappedClass)) {
isMapping = true; isMapping = true;
} else { } else {
...@@ -107,8 +126,8 @@ public class AllBeanRowMapper<T> implements RowMapper<T> { ...@@ -107,8 +126,8 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
/** /**
* 获取列明 * 获取列明
* *
* @param name * @param name 待转换的名称
* @return * @return 名称
*/ */
private static String underscoreNameBase(String name) { private static String underscoreNameBase(String name) {
if (!StringUtils.hasLength(name)) { if (!StringUtils.hasLength(name)) {
...@@ -120,8 +139,8 @@ public class AllBeanRowMapper<T> implements RowMapper<T> { ...@@ -120,8 +139,8 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
/** /**
* 将行进行转换 * 将行进行转换
* *
* @param from * @param from 名称
* @return * @return 行信息
*/ */
public static MapRow toLoweRow(MapRow from) { public static MapRow toLoweRow(MapRow from) {
MapRow to = new MapRow(); MapRow to = new MapRow();
...@@ -134,8 +153,8 @@ public class AllBeanRowMapper<T> implements RowMapper<T> { ...@@ -134,8 +153,8 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
/** /**
* 获取空值字段 * 获取空值字段
* *
* @param from * @param from 名称
* @return * @return 行信息
*/ */
public static Object getLoweRowField(MapRow from, String field) { public static Object getLoweRowField(MapRow from, String field) {
field = underscoreNameBase(field); field = underscoreNameBase(field);
...@@ -156,13 +175,12 @@ public class AllBeanRowMapper<T> implements RowMapper<T> { ...@@ -156,13 +175,12 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
@Override @Override
public T mapRow(ResultSet rs, int rowNumber) throws SQLException, TypeMismatchException { public T mapRow(ResultSet rs, int rowNumber) throws SQLException, TypeMismatchException {
Assert.state(this.mappedClass != null, "Mapped class was not specified"); Assert.state(this.mappedClass != null, "Mapped class was not specified");
T mappedObject = BeanUtils.instantiate(this.mappedClass); T mappedObject = BeanUtils.instantiateClass(this.mappedClass);
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
ResultSetMetaData resultSet = rs.getMetaData();
int columnCount = resultSet.getColumnCount();
for (int index = 1; index <= columnCount; index++) { for (int index = 1; index <= columnCount; index++) {
String column = JdbcUtils.lookupColumnName(rsmd, index); String column = JdbcUtils.lookupColumnName(resultSet, index);
String underscoredName = underscoreName(column); String underscoredName = underscoreName(column);
if (this.isMapping) { if (this.isMapping) {
Object value = JdbcUtils.getResultSetValue(rs, index); Object value = JdbcUtils.getResultSetValue(rs, index);
...@@ -170,12 +188,9 @@ public class AllBeanRowMapper<T> implements RowMapper<T> { ...@@ -170,12 +188,9 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
value = JdbcUtils.getResultSetValue(rs, index, String.class); value = JdbcUtils.getResultSetValue(rs, index, String.class);
} }
((Map) mappedObject).put(getCamelCase(column), value); ((Map) mappedObject).put(getCamelCase(column), value);
} else if (!this.typeField.containsKey(underscoredName)) { } else if (this.typeField.containsKey(underscoredName)) {
continue;
} else {
MethodField pd = this.typeField.get(underscoredName); MethodField pd = this.typeField.get(underscoredName);
Class<?> type; Class<?> type;
if (pd.getField() != null) { if (pd.getField() != null) {
type = pd.getField().getType(); type = pd.getField().getType();
} else { } else {
...@@ -210,8 +225,8 @@ public class AllBeanRowMapper<T> implements RowMapper<T> { ...@@ -210,8 +225,8 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
/** /**
* 设置SameCase命名方式 * 设置SameCase命名方式
* *
* @param column * @param column 待转换的列名
* @return * @return 转换后的列名
*/ */
private String getCamelCase(String column) { private String getCamelCase(String column) {
switch (configDb.getRowNameType()) { switch (configDb.getRowNameType()) {
...@@ -224,24 +239,4 @@ public class AllBeanRowMapper<T> implements RowMapper<T> { ...@@ -224,24 +239,4 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
} }
return column; return column;
} }
/**
* 缓存的处理类
*/
private static final Map<Class, Object> Cache = new HashMap<Class, Object>();
/**
* 获取可以转化实体
*
* @param cls 类型
* @param <T> 泛型类型
* @return 可转换的实体
*/
public static <T extends Object> AllBeanRowMapper<T> getInstance(Class<T> cls, ConfigDb configDb) {
if (!Cache.containsKey(cls)) {
Cache.put(cls, new AllBeanRowMapper<T>(cls, configDb));
}
AllBeanRowMapper<T> ret = (AllBeanRowMapper<T>) Cache.get(cls);
return ret;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment