- 浏览: 128683 次
- 性别:
- 来自: 深圳
文章分类
- 全部博客 (165)
- 数据库 (45)
- 架构 (0)
- java web前端+框架 (9)
- java web后端+框架 (56)
- Eclipse插件 (1)
- 解题思路 (2)
- Eclipse (2)
- linux (6)
- jquery (4)
- 正则 (3)
- jsp (1)
- javascript (8)
- oracle (39)
- 生活 (1)
- weblogic (5)
- tomcat (3)
- Jrebel (1)
- powerdesigner (1)
- svn (1)
- log4j (1)
- IDE (1)
- POI (2)
- jvm (2)
- ssh (1)
- http (1)
- notepad++ (2)
- 润乾 (1)
- 设计模式 (0)
- 实用类Utils (2)
- 算法 (1)
- xml (1)
- 实用类 (2)
最新评论
package com.achievo.ems.web.util;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.keyvalue.AbstractMapEntry;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.achievo.core.common.ErrorCodes;
import com.achievo.core.exception.ServiceException;
import com.achievo.ems.web.dto.BaseDto;
import com.achievo.ems.web.dto.exam.ExamineeInfoDto;
import com.achievo.ems.web.dto.exam.PositionDto;
import com.achievo.ems.web.dto.score.PScoreOrigDto;
import com.achievo.ems.web.service.score.ConnUtils2;
/**
* JdbcTemplate帮助类-行记录转换成Dto
*
* @author vernon.chen
*
*/
public class JdbcTemplateRowMapper {
private static Logger logger = LoggerFactory.getLogger(JdbcTemplateRowMapper.class);
private final static String SET = "set";
public static <T extends BaseDto> List<T> resultSet2DtoList(
ResultSet rs,
T baseDto) {
try {
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount();
List<KeyValuePair2<Integer, String>> kvColList = new ArrayList<KeyValuePair2<Integer, String>>();
for (int column = 1; column <= columnCount; column++) {
int columnType = md.getColumnType(column);// 列类型
// String columnName = md.getColumnName(column);//field的原始名字
String columnLabel = md.getColumnLabel(column);// field的别名
KeyValuePair2<Integer, String> kvCol = new KeyValuePair2<Integer, String>(columnType, columnLabel);
kvColList.add(kvCol);
}
List<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();
while (rs.next()) {
HashMap<String, Object> e = new HashMap<String, Object>();
for (KeyValuePair2<Integer, String> kvCol : kvColList) {
String colAlias = kvCol.getValue();// 列别名
Object object = rs.getObject(colAlias);// 列值
e.put(colAlias, object);
}
rows.add(e);
}
return JdbcTemplateRowMapper.rowMap2DtoList(rows, baseDto);
} catch (Exception e) {
logger.error(e.getMessage(), e);
throw new ServiceException(ErrorCodes.ERRORCODES_SYSTEM_ERROR.getStrVlue(), e);
}
}
/**
* 多行记录转换为Dto集合
*
* @param rows
* 多行记录
* @param baseDto
* baseDto实例
* @return Dto集合
*/
public static <T extends BaseDto> List<T> rowMap2DtoList(
List<Map<String, Object>> rows,
T baseDto) {
List<T> dtoList = new ArrayList<T>();
if (CollectionUtils.isEmpty(rows)) {
return dtoList;
}
Map<String, String> filedsMap = new HashMap<String, String>();
T dto = null;
Constructor<T> constructor = null;
Class<?> conargs[] = {};
Class<T> clazz = (Class<T>) baseDto.getClass();
Set<String> keySet = null;
String filedName = null;
Object filedValue = null;
BeanMap beanMap = new BeanMap(baseDto);
@SuppressWarnings("unchecked")
Set<AbstractMapEntry> entrySet = beanMap.entrySet();
String filedNameTmp = null;
for (AbstractMapEntry entry : entrySet) {
filedNameTmp = entry.getKey().toString();
filedsMap.put(filedNameTmp.toUpperCase(), filedNameTmp);
}
for (Map<String, Object> map : rows) {
try {
constructor = clazz.getConstructor(conargs);
dto = (T) constructor.newInstance();
} catch (SecurityException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (NoSuchMethodException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (IllegalArgumentException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (InstantiationException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (IllegalAccessException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (InvocationTargetException e1) {
logger.error("JdbcTemplateRowMapper", e1);
}
dtoList.add(dto);
keySet = map.keySet();
for (String key : keySet) {
try {
if (keySet.contains(key)) {
filedName = filedsMap.get(key.toUpperCase());
filedValue = map.get(key);
} else {
continue;
}
if (StringUtils.isNotBlank(filedName) && null != filedValue) {
JdbcTemplateRowMapper.setProperty(dto, filedName, filedValue);
}
} catch (IllegalAccessException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (InvocationTargetException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (Exception e1) {
logger.error("JdbcTemplateRowMapper", e1);
}
}
}
filedsMap.clear();
filedsMap = null;
dto = null;
constructor = null;
conargs = null;
clazz = null;
filedName = null;
filedValue = null;
beanMap.clear();
beanMap = null;
// entrySet.clear();//UnsupportedOperationException
entrySet = null;
filedNameTmp = null;
keySet.clear();
keySet = null;
// int size = rows.size();
rows.clear();// 释放
rows = null;
// logger.debug(size + " rows be cleared.");
//
// logger.debug("多行记录转换为Dto集合-共耗时:" + ConnUtils2.ms2Hms(System.currentTimeMillis() - time_begin));
return dtoList;
}
/**
* 多行记录转换为Dto集合
*
* @param rows
* 多行记录
* @param baseDto
* baseDto实例
* @return Dto集合
*/
public static <T> List<T> rowMap2DtoList(
List<Map<String, Object>> rows,
T baseDto) {
List<T> dtoList = new ArrayList<T>();
if (CollectionUtils.isEmpty(rows)) {
return dtoList;
}
Map<String, String> filedsMap = new HashMap<String, String>();
T dto = null;
Constructor<T> constructor = null;
Class<?> conargs[] = {};
Class<T> clazz = (Class<T>) baseDto.getClass();
Set<String> keySet = null;
String filedName = null;
Object filedValue = null;
BeanMap beanMap = new BeanMap(baseDto);
@SuppressWarnings("unchecked")
Set<AbstractMapEntry> entrySet = beanMap.entrySet();
String filedNameTmp = null;
for (AbstractMapEntry entry : entrySet) {
filedNameTmp = entry.getKey().toString();
filedsMap.put(filedNameTmp.toUpperCase(), filedNameTmp);
}
for (Map<String, Object> map : rows) {
try {
constructor = clazz.getConstructor(conargs);
dto = (T) constructor.newInstance();
} catch (SecurityException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (NoSuchMethodException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (IllegalArgumentException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (InstantiationException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (IllegalAccessException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (InvocationTargetException e1) {
logger.error("JdbcTemplateRowMapper", e1);
}
dtoList.add(dto);
keySet = map.keySet();
for (String key : keySet) {
try {
if (keySet.contains(key)) {
filedName = filedsMap.get(key.toUpperCase());
filedValue = map.get(key);
} else {
continue;
}
if (StringUtils.isNotBlank(filedName) && null != filedValue) {
JdbcTemplateRowMapper.setProperty(dto, filedName, filedValue);
}
} catch (IllegalAccessException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (InvocationTargetException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (Exception e1) {
logger.error("JdbcTemplateRowMapper", e1);
}
}
}
filedsMap.clear();
filedsMap = null;
dto = null;
constructor = null;
conargs = null;
clazz = null;
filedName = null;
filedValue = null;
beanMap.clear();
beanMap = null;
// entrySet.clear();//UnsupportedOperationException
entrySet = null;
filedNameTmp = null;
keySet.clear();
keySet = null;
// int size = rows.size();
rows.clear();// 释放
rows = null;
// logger.debug(size + " rows be cleared.");
//
// logger.debug("多行记录转换为Dto集合-共耗时:" + ConnUtils2.ms2Hms(System.currentTimeMillis() - time_begin));
return dtoList;
}
public static void main(
String[] args) throws Throwable {
testReflect();
}
public static <T extends BaseDto> void setProperty(
T dto,
String filedName,
Object filedValue) throws Exception {
// byte(字节) 8 -128 -127 0
// short(短整型) 16 -32768 -32768 0
// int(整型) 32 -2147483648 -2147483648 0
// long(长整型) 64 -9233372036854477808 -9233372036854477808 0
// float(浮点型) 32 -3.40292347E+38 -3.40292347E+38 0.0f
// double(双精度) 64 -1.79769313486231570E+308 -1.79769313486231570E+308 0.0d
// char(字符型) 16 ‘ \u0000 - u\ffff ’ ‘\u0000 ’
// boolean(布尔型) 1 true/false false
//
Class<? extends BaseDto> dtoCls = dto.getClass();
Class<? extends Object> filedClass = filedValue.getClass();// jdbc返回的值类型
if (java.sql.Timestamp.class.isAssignableFrom(filedClass)) {
filedValue = new Date(((java.sql.Timestamp) filedValue).getTime());
filedClass = Date.class;
}
Field dtoField = null;
try {
dtoField = dtoCls.getDeclaredField(filedName);
} catch (NoSuchFieldException e) {
Field[] fields = dtoCls.getFields();
for (Field field : fields) {
if (field.getName().equals(filedName)) {
dtoField = field;// 获取超类中定义的Filed
break;
}
}
}
Class<?> dtoFiledType = dtoField.getType();
if (filedClass.isAssignableFrom(dtoFiledType)) {// 类型相同
;// noting to do.
} else {// 类型不相同
if (java.math.BigDecimal.class.isAssignableFrom(filedClass)) {// jdbc返回BigDecimal类型
if (short.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).shortValue();
filedClass = short.class;
} else if (int.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).intValue();
filedClass = int.class;
} else if (long.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).longValue();
filedClass = long.class;
} else if (float.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).floatValue();
filedClass = float.class;
} else if (double.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).doubleValue();
filedClass = double.class;
} else if (Short.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).shortValue();
filedClass = Short.class;
} else if (Integer.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).intValue();
filedClass = Integer.class;
} else if (Long.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).longValue();
filedClass = Long.class;
} else if (Float.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).floatValue();
filedClass = Float.class;
} else if (Double.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).doubleValue();
filedClass = Double.class;
} else if (String.class.isAssignableFrom(dtoFiledType)) {
filedValue = filedValue.toString();
filedClass = String.class;
} else {
throw new Exception("未定义的数据类型。");
}
}
}
// logger.debug("filedClass.getName()" + ":" + filedClass.getName());
Method method = dtoCls.getMethod(SET + StringUtils.capitalize(filedName), filedClass);
method.invoke(dto, filedValue);
dtoCls = null;
}
public static <T> void setProperty(
T dto,
String filedName,
Object filedValue) throws Exception {
// byte(字节) 8 -128 -127 0
// short(短整型) 16 -32768 -32768 0
// int(整型) 32 -2147483648 -2147483648 0
// long(长整型) 64 -9233372036854477808 -9233372036854477808 0
// float(浮点型) 32 -3.40292347E+38 -3.40292347E+38 0.0f
// double(双精度) 64 -1.79769313486231570E+308 -1.79769313486231570E+308 0.0d
// char(字符型) 16 ‘ \u0000 - u\ffff ’ ‘\u0000 ’
// boolean(布尔型) 1 true/false false
//
Class<?> dtoCls = dto.getClass();
Class<? extends Object> filedClass = filedValue.getClass();// jdbc返回的值类型
if (java.sql.Timestamp.class.isAssignableFrom(filedClass)) {
filedValue = new Date(((java.sql.Timestamp) filedValue).getTime());
filedClass = Date.class;
}
Field dtoField = null;
try {
dtoField = dtoCls.getDeclaredField(filedName);
} catch (NoSuchFieldException e) {
Field[] fields = dtoCls.getFields();
for (Field field : fields) {
if (field.getName().equals(filedName)) {
dtoField = field;// 获取超类中定义的Filed
break;
}
}
}
Class<?> dtoFiledType = dtoField.getType();
if (filedClass.isAssignableFrom(dtoFiledType)) {// 类型相同
;// noting to do.
} else {// 类型不相同
if (java.math.BigDecimal.class.isAssignableFrom(filedClass)) {// jdbc返回BigDecimal类型
if (short.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).shortValue();
filedClass = short.class;
} else if (int.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).intValue();
filedClass = int.class;
} else if (long.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).longValue();
filedClass = long.class;
} else if (float.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).floatValue();
filedClass = float.class;
} else if (double.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).doubleValue();
filedClass = double.class;
} else if (Short.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).shortValue();
filedClass = Short.class;
} else if (Integer.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).intValue();
filedClass = Integer.class;
} else if (Long.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).longValue();
filedClass = Long.class;
} else if (Float.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).floatValue();
filedClass = Float.class;
} else if (Double.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).doubleValue();
filedClass = Double.class;
} else if (String.class.isAssignableFrom(dtoFiledType)) {
filedValue = filedValue.toString();
filedClass = String.class;
} else {
throw new Exception("未定义的数据类型。");
}
}
}
// logger.debug("filedClass.getName()" + ":" + filedClass.getName());
Method method = dtoCls.getMethod(SET + StringUtils.capitalize(filedName), filedClass);
method.invoke(dto, filedValue);
dtoCls = null;
}
public static void testReflect() throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
PScoreOrigDto dto = new PScoreOrigDto();
Class<? extends PScoreOrigDto> cls = dto.getClass();
Field[] fields = cls.getDeclaredFields();// 自身定义的所有字段。不包括继承自父类
// Method[] declaredMethods = cls.getDeclaredMethods();
String setter = "set";
String filedName = null;
Object filedValue = null;
Method method = null;
for (Field field : fields) {
filedName = field.getName();
filedValue = "1111";
Class<?> fieldType = field.getType();
// logger.debug("filedName" + ":" + filedName);
if (BaseDto.class.isAssignableFrom(fieldType) || fieldType.isArray() || List.class.isAssignableFrom(fieldType) || Set.class.isAssignableFrom(fieldType)) {
;// 排除BaseDto类型的字段
} else {
method = cls.getMethod(setter + StringUtils.capitalize(filedName), fieldType);
logger.debug("filedName" + ":" + filedName + "\t\t" + "fieldType" + ":" + fieldType);
if (BigDecimal.class.isAssignableFrom(fieldType)) {
filedValue = new BigDecimal(filedValue.toString());
} else if (Integer.class.isAssignableFrom(fieldType)) {
filedValue = new Integer(filedValue.toString());
} else if (Long.class.isAssignableFrom(fieldType)) {
filedValue = new Long(filedValue.toString());
} else if (Double.class.isAssignableFrom(fieldType)) {
filedValue = new Double(filedValue.toString());
} else if (Float.class.isAssignableFrom(fieldType)) {
filedValue = new Float(filedValue.toString());
} else if (Short.class.isAssignableFrom(fieldType)) {
filedValue = new Short(filedValue.toString());
} else if (Byte.class.isAssignableFrom(fieldType)) {
filedValue = new Byte(filedValue.toString());
} else if (Date.class.isAssignableFrom(fieldType)) {
filedValue = DateTimeUtils.parseDate(filedValue.toString());
}
method.invoke(dto, filedValue);
}
}
// method = cls.getMethod(setter + StringUtils.capitalize("createTime"), Date.class);
// method.invoke(dto, new Date());
// method = cls.getMethod(setter + StringUtils.capitalize("createBy"), String.class);
// method.invoke(dto, "createBy");
// method = cls.getMethod(setter + StringUtils.capitalize("updateTime"), Date.class);
// method.invoke(dto, new Date());
// method = cls.getMethod(setter + StringUtils.capitalize("updateBy"), String.class);
// method.invoke(dto, "createBy");
//
// logger.debug("dto.getApplyNo()" + ":" + dto.getApplyNo());
// logger.debug("dto.getCertNo()" + ":" + dto.getCertNo());
// logger.debug("dto.getCertType()" + ":" + dto.getCertType());
// logger.debug("dto.getCreateTime()" + ":" + dto.getCreateTime());
// logger.debug("dto.getCreateBy()" + ":" + dto.getCreateBy());
// logger.debug("dto.getUpdateTime()" + ":" + dto.getUpdateTime());
// logger.debug("dto.getUpdateBy()" + ":" + dto.getUpdateBy());
}
public static void main2(
String[] args) {
viewRuntimeMemory();
List<PScoreOrigDto> psoList = new ArrayList<PScoreOrigDto>();
PScoreOrigDto e = null;
long time_begin = System.currentTimeMillis();
for (int i = 0; i < 5 * 2; i++) {
for (int j = 0; j < 20000; j++) {
e = new PScoreOrigDto();
e.setApplyNo("");
e.setCertNo(null);
e.setCertType(" ");
// e.setClusterSubject(new ClusterSubjectDto());
e.setClusterSubjectId("String");
e.setCredtId("String");
e.setExamineeInfo(new ExamineeInfoDto());
e.setException(BigDecimal.ZERO);
e.setExamineeName("String");
e.setFlag(BigDecimal.TEN);
e.setId("String");
e.setPosition(new PositionDto());
e.setPositionClusterName("String");
e.setPositionId("String");
e.setScore(BigDecimal.TEN);
e.setSignature("String");
e.setSubjectId("String");
psoList.add(e);
}
logger.debug("根据导入日志ID删除导入的记录:" + i);
}
logger.debug("根据导入日志ID删除导入的记录-共耗时:" + ConnUtils2.ms2Hms(System.currentTimeMillis() - time_begin));
List<BaseDto> dtoList = new ArrayList<BaseDto>();
Map<String, String> filedsMap = new HashMap<String, String>();
PScoreOrigDto dto = null;
Constructor<PScoreOrigDto> constructor = null;
Class<?> conargs[] = {};
PScoreOrigDto template = new PScoreOrigDto();
Class<PScoreOrigDto> clazz = (Class<PScoreOrigDto>) template.getClass();
BeanMap beanMap = new BeanMap(template);
Set<String> keySet = beanMap.keySet();
String filedName = null;
Object filedValue = null;
@SuppressWarnings("unchecked")
Set<AbstractMapEntry> entrySet = beanMap.entrySet();
String filedNameTmp = null;
for (AbstractMapEntry entry : entrySet) {
filedNameTmp = entry.getKey().toString();
filedsMap.put(filedNameTmp.toUpperCase(), filedNameTmp);
}
time_begin = System.currentTimeMillis();
for (PScoreOrigDto pso : psoList) {
try {
constructor = clazz.getConstructor(conargs);
dto = constructor.newInstance();
} catch (SecurityException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (NoSuchMethodException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (IllegalArgumentException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (InstantiationException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (IllegalAccessException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (InvocationTargetException e1) {
logger.error("JdbcTemplateRowMapper", e1);
}
dtoList.add(dto);
for (String key : keySet) {
try {
filedName = key;
filedValue = BeanUtils.getProperty(pso, key);
if (StringUtils.isNotBlank(filedName) && null != filedValue) {
if ("position".equals(key) || "examineeInfo".equals(key) || "clusterSubject".equals(key)) {
} else {
BeanUtils.setProperty(dto, filedName, filedValue);
}
}
} catch (IllegalAccessException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (InvocationTargetException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (NoSuchMethodException e1) {
logger.error("JdbcTemplateRowMapper", e1);
}
}
}
// logger.debug("转换(" + psoList.size() + ")个实体-共耗时:" + ConnUtils2.ms2Hms(System.currentTimeMillis() - time_begin));
filedsMap.clear();
filedsMap = null;
dto = null;
constructor = null;
conargs = null;
clazz = null;
filedName = null;
filedValue = null;
beanMap.clear();
beanMap = null;
// entrySet.clear();//UnsupportedOperationException
entrySet = null;
filedNameTmp = null;
// keySet.clear();//UnsupportedOperationException
keySet = null;
}
/**
* 查看运行时内存使用情况
*/
public static void viewRuntimeMemory() {
long maxMemory = java.lang.Runtime.getRuntime().maxMemory();// 这个方法返回的是java虚拟机(这个进程)能构从操作系统那里挖到的最大的内存,以字节为单位
long totalMemory = java.lang.Runtime.getRuntime().totalMemory();// 这个方法返回的是java虚拟机现在已经从操作系统那里挖过来的内存大小,也就是java虚拟机这个进程当时所占用的所有内存。
long freeMemory = java.lang.Runtime.getRuntime().freeMemory();// 从操作系统那里挖过来而又没有用上的内存,实际上就是 freeMemory()
logger.debug("从操作系统那里挖到的最大的内存:" + maxMemory / 1024 / 1024 + "M");
logger.debug("虚拟机这个进程当时所占用的所有内存:" + totalMemory / 1024 / 1024 + "M");
logger.debug("从操作系统那里挖过来而又没有用上的内存:" + freeMemory / 1024 / 1024 + "M");
}
}
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.keyvalue.AbstractMapEntry;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.achievo.core.common.ErrorCodes;
import com.achievo.core.exception.ServiceException;
import com.achievo.ems.web.dto.BaseDto;
import com.achievo.ems.web.dto.exam.ExamineeInfoDto;
import com.achievo.ems.web.dto.exam.PositionDto;
import com.achievo.ems.web.dto.score.PScoreOrigDto;
import com.achievo.ems.web.service.score.ConnUtils2;
/**
* JdbcTemplate帮助类-行记录转换成Dto
*
* @author vernon.chen
*
*/
public class JdbcTemplateRowMapper {
private static Logger logger = LoggerFactory.getLogger(JdbcTemplateRowMapper.class);
private final static String SET = "set";
public static <T extends BaseDto> List<T> resultSet2DtoList(
ResultSet rs,
T baseDto) {
try {
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount();
List<KeyValuePair2<Integer, String>> kvColList = new ArrayList<KeyValuePair2<Integer, String>>();
for (int column = 1; column <= columnCount; column++) {
int columnType = md.getColumnType(column);// 列类型
// String columnName = md.getColumnName(column);//field的原始名字
String columnLabel = md.getColumnLabel(column);// field的别名
KeyValuePair2<Integer, String> kvCol = new KeyValuePair2<Integer, String>(columnType, columnLabel);
kvColList.add(kvCol);
}
List<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();
while (rs.next()) {
HashMap<String, Object> e = new HashMap<String, Object>();
for (KeyValuePair2<Integer, String> kvCol : kvColList) {
String colAlias = kvCol.getValue();// 列别名
Object object = rs.getObject(colAlias);// 列值
e.put(colAlias, object);
}
rows.add(e);
}
return JdbcTemplateRowMapper.rowMap2DtoList(rows, baseDto);
} catch (Exception e) {
logger.error(e.getMessage(), e);
throw new ServiceException(ErrorCodes.ERRORCODES_SYSTEM_ERROR.getStrVlue(), e);
}
}
/**
* 多行记录转换为Dto集合
*
* @param rows
* 多行记录
* @param baseDto
* baseDto实例
* @return Dto集合
*/
public static <T extends BaseDto> List<T> rowMap2DtoList(
List<Map<String, Object>> rows,
T baseDto) {
List<T> dtoList = new ArrayList<T>();
if (CollectionUtils.isEmpty(rows)) {
return dtoList;
}
Map<String, String> filedsMap = new HashMap<String, String>();
T dto = null;
Constructor<T> constructor = null;
Class<?> conargs[] = {};
Class<T> clazz = (Class<T>) baseDto.getClass();
Set<String> keySet = null;
String filedName = null;
Object filedValue = null;
BeanMap beanMap = new BeanMap(baseDto);
@SuppressWarnings("unchecked")
Set<AbstractMapEntry> entrySet = beanMap.entrySet();
String filedNameTmp = null;
for (AbstractMapEntry entry : entrySet) {
filedNameTmp = entry.getKey().toString();
filedsMap.put(filedNameTmp.toUpperCase(), filedNameTmp);
}
for (Map<String, Object> map : rows) {
try {
constructor = clazz.getConstructor(conargs);
dto = (T) constructor.newInstance();
} catch (SecurityException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (NoSuchMethodException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (IllegalArgumentException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (InstantiationException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (IllegalAccessException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (InvocationTargetException e1) {
logger.error("JdbcTemplateRowMapper", e1);
}
dtoList.add(dto);
keySet = map.keySet();
for (String key : keySet) {
try {
if (keySet.contains(key)) {
filedName = filedsMap.get(key.toUpperCase());
filedValue = map.get(key);
} else {
continue;
}
if (StringUtils.isNotBlank(filedName) && null != filedValue) {
JdbcTemplateRowMapper.setProperty(dto, filedName, filedValue);
}
} catch (IllegalAccessException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (InvocationTargetException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (Exception e1) {
logger.error("JdbcTemplateRowMapper", e1);
}
}
}
filedsMap.clear();
filedsMap = null;
dto = null;
constructor = null;
conargs = null;
clazz = null;
filedName = null;
filedValue = null;
beanMap.clear();
beanMap = null;
// entrySet.clear();//UnsupportedOperationException
entrySet = null;
filedNameTmp = null;
keySet.clear();
keySet = null;
// int size = rows.size();
rows.clear();// 释放
rows = null;
// logger.debug(size + " rows be cleared.");
//
// logger.debug("多行记录转换为Dto集合-共耗时:" + ConnUtils2.ms2Hms(System.currentTimeMillis() - time_begin));
return dtoList;
}
/**
* 多行记录转换为Dto集合
*
* @param rows
* 多行记录
* @param baseDto
* baseDto实例
* @return Dto集合
*/
public static <T> List<T> rowMap2DtoList(
List<Map<String, Object>> rows,
T baseDto) {
List<T> dtoList = new ArrayList<T>();
if (CollectionUtils.isEmpty(rows)) {
return dtoList;
}
Map<String, String> filedsMap = new HashMap<String, String>();
T dto = null;
Constructor<T> constructor = null;
Class<?> conargs[] = {};
Class<T> clazz = (Class<T>) baseDto.getClass();
Set<String> keySet = null;
String filedName = null;
Object filedValue = null;
BeanMap beanMap = new BeanMap(baseDto);
@SuppressWarnings("unchecked")
Set<AbstractMapEntry> entrySet = beanMap.entrySet();
String filedNameTmp = null;
for (AbstractMapEntry entry : entrySet) {
filedNameTmp = entry.getKey().toString();
filedsMap.put(filedNameTmp.toUpperCase(), filedNameTmp);
}
for (Map<String, Object> map : rows) {
try {
constructor = clazz.getConstructor(conargs);
dto = (T) constructor.newInstance();
} catch (SecurityException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (NoSuchMethodException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (IllegalArgumentException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (InstantiationException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (IllegalAccessException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (InvocationTargetException e1) {
logger.error("JdbcTemplateRowMapper", e1);
}
dtoList.add(dto);
keySet = map.keySet();
for (String key : keySet) {
try {
if (keySet.contains(key)) {
filedName = filedsMap.get(key.toUpperCase());
filedValue = map.get(key);
} else {
continue;
}
if (StringUtils.isNotBlank(filedName) && null != filedValue) {
JdbcTemplateRowMapper.setProperty(dto, filedName, filedValue);
}
} catch (IllegalAccessException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (InvocationTargetException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (Exception e1) {
logger.error("JdbcTemplateRowMapper", e1);
}
}
}
filedsMap.clear();
filedsMap = null;
dto = null;
constructor = null;
conargs = null;
clazz = null;
filedName = null;
filedValue = null;
beanMap.clear();
beanMap = null;
// entrySet.clear();//UnsupportedOperationException
entrySet = null;
filedNameTmp = null;
keySet.clear();
keySet = null;
// int size = rows.size();
rows.clear();// 释放
rows = null;
// logger.debug(size + " rows be cleared.");
//
// logger.debug("多行记录转换为Dto集合-共耗时:" + ConnUtils2.ms2Hms(System.currentTimeMillis() - time_begin));
return dtoList;
}
public static void main(
String[] args) throws Throwable {
testReflect();
}
public static <T extends BaseDto> void setProperty(
T dto,
String filedName,
Object filedValue) throws Exception {
// byte(字节) 8 -128 -127 0
// short(短整型) 16 -32768 -32768 0
// int(整型) 32 -2147483648 -2147483648 0
// long(长整型) 64 -9233372036854477808 -9233372036854477808 0
// float(浮点型) 32 -3.40292347E+38 -3.40292347E+38 0.0f
// double(双精度) 64 -1.79769313486231570E+308 -1.79769313486231570E+308 0.0d
// char(字符型) 16 ‘ \u0000 - u\ffff ’ ‘\u0000 ’
// boolean(布尔型) 1 true/false false
//
Class<? extends BaseDto> dtoCls = dto.getClass();
Class<? extends Object> filedClass = filedValue.getClass();// jdbc返回的值类型
if (java.sql.Timestamp.class.isAssignableFrom(filedClass)) {
filedValue = new Date(((java.sql.Timestamp) filedValue).getTime());
filedClass = Date.class;
}
Field dtoField = null;
try {
dtoField = dtoCls.getDeclaredField(filedName);
} catch (NoSuchFieldException e) {
Field[] fields = dtoCls.getFields();
for (Field field : fields) {
if (field.getName().equals(filedName)) {
dtoField = field;// 获取超类中定义的Filed
break;
}
}
}
Class<?> dtoFiledType = dtoField.getType();
if (filedClass.isAssignableFrom(dtoFiledType)) {// 类型相同
;// noting to do.
} else {// 类型不相同
if (java.math.BigDecimal.class.isAssignableFrom(filedClass)) {// jdbc返回BigDecimal类型
if (short.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).shortValue();
filedClass = short.class;
} else if (int.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).intValue();
filedClass = int.class;
} else if (long.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).longValue();
filedClass = long.class;
} else if (float.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).floatValue();
filedClass = float.class;
} else if (double.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).doubleValue();
filedClass = double.class;
} else if (Short.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).shortValue();
filedClass = Short.class;
} else if (Integer.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).intValue();
filedClass = Integer.class;
} else if (Long.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).longValue();
filedClass = Long.class;
} else if (Float.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).floatValue();
filedClass = Float.class;
} else if (Double.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).doubleValue();
filedClass = Double.class;
} else if (String.class.isAssignableFrom(dtoFiledType)) {
filedValue = filedValue.toString();
filedClass = String.class;
} else {
throw new Exception("未定义的数据类型。");
}
}
}
// logger.debug("filedClass.getName()" + ":" + filedClass.getName());
Method method = dtoCls.getMethod(SET + StringUtils.capitalize(filedName), filedClass);
method.invoke(dto, filedValue);
dtoCls = null;
}
public static <T> void setProperty(
T dto,
String filedName,
Object filedValue) throws Exception {
// byte(字节) 8 -128 -127 0
// short(短整型) 16 -32768 -32768 0
// int(整型) 32 -2147483648 -2147483648 0
// long(长整型) 64 -9233372036854477808 -9233372036854477808 0
// float(浮点型) 32 -3.40292347E+38 -3.40292347E+38 0.0f
// double(双精度) 64 -1.79769313486231570E+308 -1.79769313486231570E+308 0.0d
// char(字符型) 16 ‘ \u0000 - u\ffff ’ ‘\u0000 ’
// boolean(布尔型) 1 true/false false
//
Class<?> dtoCls = dto.getClass();
Class<? extends Object> filedClass = filedValue.getClass();// jdbc返回的值类型
if (java.sql.Timestamp.class.isAssignableFrom(filedClass)) {
filedValue = new Date(((java.sql.Timestamp) filedValue).getTime());
filedClass = Date.class;
}
Field dtoField = null;
try {
dtoField = dtoCls.getDeclaredField(filedName);
} catch (NoSuchFieldException e) {
Field[] fields = dtoCls.getFields();
for (Field field : fields) {
if (field.getName().equals(filedName)) {
dtoField = field;// 获取超类中定义的Filed
break;
}
}
}
Class<?> dtoFiledType = dtoField.getType();
if (filedClass.isAssignableFrom(dtoFiledType)) {// 类型相同
;// noting to do.
} else {// 类型不相同
if (java.math.BigDecimal.class.isAssignableFrom(filedClass)) {// jdbc返回BigDecimal类型
if (short.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).shortValue();
filedClass = short.class;
} else if (int.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).intValue();
filedClass = int.class;
} else if (long.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).longValue();
filedClass = long.class;
} else if (float.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).floatValue();
filedClass = float.class;
} else if (double.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).doubleValue();
filedClass = double.class;
} else if (Short.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).shortValue();
filedClass = Short.class;
} else if (Integer.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).intValue();
filedClass = Integer.class;
} else if (Long.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).longValue();
filedClass = Long.class;
} else if (Float.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).floatValue();
filedClass = Float.class;
} else if (Double.class.isAssignableFrom(dtoFiledType)) {
filedValue = ((java.math.BigDecimal) filedValue).doubleValue();
filedClass = Double.class;
} else if (String.class.isAssignableFrom(dtoFiledType)) {
filedValue = filedValue.toString();
filedClass = String.class;
} else {
throw new Exception("未定义的数据类型。");
}
}
}
// logger.debug("filedClass.getName()" + ":" + filedClass.getName());
Method method = dtoCls.getMethod(SET + StringUtils.capitalize(filedName), filedClass);
method.invoke(dto, filedValue);
dtoCls = null;
}
public static void testReflect() throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
PScoreOrigDto dto = new PScoreOrigDto();
Class<? extends PScoreOrigDto> cls = dto.getClass();
Field[] fields = cls.getDeclaredFields();// 自身定义的所有字段。不包括继承自父类
// Method[] declaredMethods = cls.getDeclaredMethods();
String setter = "set";
String filedName = null;
Object filedValue = null;
Method method = null;
for (Field field : fields) {
filedName = field.getName();
filedValue = "1111";
Class<?> fieldType = field.getType();
// logger.debug("filedName" + ":" + filedName);
if (BaseDto.class.isAssignableFrom(fieldType) || fieldType.isArray() || List.class.isAssignableFrom(fieldType) || Set.class.isAssignableFrom(fieldType)) {
;// 排除BaseDto类型的字段
} else {
method = cls.getMethod(setter + StringUtils.capitalize(filedName), fieldType);
logger.debug("filedName" + ":" + filedName + "\t\t" + "fieldType" + ":" + fieldType);
if (BigDecimal.class.isAssignableFrom(fieldType)) {
filedValue = new BigDecimal(filedValue.toString());
} else if (Integer.class.isAssignableFrom(fieldType)) {
filedValue = new Integer(filedValue.toString());
} else if (Long.class.isAssignableFrom(fieldType)) {
filedValue = new Long(filedValue.toString());
} else if (Double.class.isAssignableFrom(fieldType)) {
filedValue = new Double(filedValue.toString());
} else if (Float.class.isAssignableFrom(fieldType)) {
filedValue = new Float(filedValue.toString());
} else if (Short.class.isAssignableFrom(fieldType)) {
filedValue = new Short(filedValue.toString());
} else if (Byte.class.isAssignableFrom(fieldType)) {
filedValue = new Byte(filedValue.toString());
} else if (Date.class.isAssignableFrom(fieldType)) {
filedValue = DateTimeUtils.parseDate(filedValue.toString());
}
method.invoke(dto, filedValue);
}
}
// method = cls.getMethod(setter + StringUtils.capitalize("createTime"), Date.class);
// method.invoke(dto, new Date());
// method = cls.getMethod(setter + StringUtils.capitalize("createBy"), String.class);
// method.invoke(dto, "createBy");
// method = cls.getMethod(setter + StringUtils.capitalize("updateTime"), Date.class);
// method.invoke(dto, new Date());
// method = cls.getMethod(setter + StringUtils.capitalize("updateBy"), String.class);
// method.invoke(dto, "createBy");
//
// logger.debug("dto.getApplyNo()" + ":" + dto.getApplyNo());
// logger.debug("dto.getCertNo()" + ":" + dto.getCertNo());
// logger.debug("dto.getCertType()" + ":" + dto.getCertType());
// logger.debug("dto.getCreateTime()" + ":" + dto.getCreateTime());
// logger.debug("dto.getCreateBy()" + ":" + dto.getCreateBy());
// logger.debug("dto.getUpdateTime()" + ":" + dto.getUpdateTime());
// logger.debug("dto.getUpdateBy()" + ":" + dto.getUpdateBy());
}
public static void main2(
String[] args) {
viewRuntimeMemory();
List<PScoreOrigDto> psoList = new ArrayList<PScoreOrigDto>();
PScoreOrigDto e = null;
long time_begin = System.currentTimeMillis();
for (int i = 0; i < 5 * 2; i++) {
for (int j = 0; j < 20000; j++) {
e = new PScoreOrigDto();
e.setApplyNo("");
e.setCertNo(null);
e.setCertType(" ");
// e.setClusterSubject(new ClusterSubjectDto());
e.setClusterSubjectId("String");
e.setCredtId("String");
e.setExamineeInfo(new ExamineeInfoDto());
e.setException(BigDecimal.ZERO);
e.setExamineeName("String");
e.setFlag(BigDecimal.TEN);
e.setId("String");
e.setPosition(new PositionDto());
e.setPositionClusterName("String");
e.setPositionId("String");
e.setScore(BigDecimal.TEN);
e.setSignature("String");
e.setSubjectId("String");
psoList.add(e);
}
logger.debug("根据导入日志ID删除导入的记录:" + i);
}
logger.debug("根据导入日志ID删除导入的记录-共耗时:" + ConnUtils2.ms2Hms(System.currentTimeMillis() - time_begin));
List<BaseDto> dtoList = new ArrayList<BaseDto>();
Map<String, String> filedsMap = new HashMap<String, String>();
PScoreOrigDto dto = null;
Constructor<PScoreOrigDto> constructor = null;
Class<?> conargs[] = {};
PScoreOrigDto template = new PScoreOrigDto();
Class<PScoreOrigDto> clazz = (Class<PScoreOrigDto>) template.getClass();
BeanMap beanMap = new BeanMap(template);
Set<String> keySet = beanMap.keySet();
String filedName = null;
Object filedValue = null;
@SuppressWarnings("unchecked")
Set<AbstractMapEntry> entrySet = beanMap.entrySet();
String filedNameTmp = null;
for (AbstractMapEntry entry : entrySet) {
filedNameTmp = entry.getKey().toString();
filedsMap.put(filedNameTmp.toUpperCase(), filedNameTmp);
}
time_begin = System.currentTimeMillis();
for (PScoreOrigDto pso : psoList) {
try {
constructor = clazz.getConstructor(conargs);
dto = constructor.newInstance();
} catch (SecurityException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (NoSuchMethodException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (IllegalArgumentException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (InstantiationException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (IllegalAccessException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (InvocationTargetException e1) {
logger.error("JdbcTemplateRowMapper", e1);
}
dtoList.add(dto);
for (String key : keySet) {
try {
filedName = key;
filedValue = BeanUtils.getProperty(pso, key);
if (StringUtils.isNotBlank(filedName) && null != filedValue) {
if ("position".equals(key) || "examineeInfo".equals(key) || "clusterSubject".equals(key)) {
} else {
BeanUtils.setProperty(dto, filedName, filedValue);
}
}
} catch (IllegalAccessException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (InvocationTargetException e1) {
logger.error("JdbcTemplateRowMapper", e1);
} catch (NoSuchMethodException e1) {
logger.error("JdbcTemplateRowMapper", e1);
}
}
}
// logger.debug("转换(" + psoList.size() + ")个实体-共耗时:" + ConnUtils2.ms2Hms(System.currentTimeMillis() - time_begin));
filedsMap.clear();
filedsMap = null;
dto = null;
constructor = null;
conargs = null;
clazz = null;
filedName = null;
filedValue = null;
beanMap.clear();
beanMap = null;
// entrySet.clear();//UnsupportedOperationException
entrySet = null;
filedNameTmp = null;
// keySet.clear();//UnsupportedOperationException
keySet = null;
}
/**
* 查看运行时内存使用情况
*/
public static void viewRuntimeMemory() {
long maxMemory = java.lang.Runtime.getRuntime().maxMemory();// 这个方法返回的是java虚拟机(这个进程)能构从操作系统那里挖到的最大的内存,以字节为单位
long totalMemory = java.lang.Runtime.getRuntime().totalMemory();// 这个方法返回的是java虚拟机现在已经从操作系统那里挖过来的内存大小,也就是java虚拟机这个进程当时所占用的所有内存。
long freeMemory = java.lang.Runtime.getRuntime().freeMemory();// 从操作系统那里挖过来而又没有用上的内存,实际上就是 freeMemory()
logger.debug("从操作系统那里挖到的最大的内存:" + maxMemory / 1024 / 1024 + "M");
logger.debug("虚拟机这个进程当时所占用的所有内存:" + totalMemory / 1024 / 1024 + "M");
logger.debug("从操作系统那里挖过来而又没有用上的内存:" + freeMemory / 1024 / 1024 + "M");
}
}
发表评论
-
eclipse java代码格式化 javaformatter20150123.zip
2015-01-23 11:30 707eclipse java代码格式化 javaformatter ... -
List to Map
2015-01-21 17:22 478/** * <p>将List变为Map</p ... -
读取access工具类-ReadAccessUtil2
2015-01-06 13:52 546import com.healthmarketscience. ... -
ParameterizedType获取java泛型参数类型
2014-11-14 15:15 697ParameterizedType获取java泛型参数类型 ... -
分页帮助类-oracle-java
2014-10-09 17:46 456/** * 分页帮助类 * * @author ver ... -
java自动实例化List AutoArrayList
2014-07-21 15:51 735import java.util.ArrayList; p ... -
单一登录过滤器
2014-06-20 17:13 394/** * 单一登录过滤器 * * @author v ... -
正则 java 查找 打印 所有匹配项
2014-06-18 11:26 635package com.test.regex; import ... -
第3方包读取Access
2014-05-26 16:07 661package com.test; import java. ... -
多线程加签验签例子.zip
2014-05-26 16:08 508多线程加签验签例子.zip -
DateTimeUtils
2014-05-26 16:08 645import java.text.DateFormat; im ... -
listToMap
2014-05-26 16:09 434/** * List实用类 * * @author v ... -
JdbcTemplateRowMapper
2015-05-19 15:44 572import java.lang.reflect.Constr ... -
KeyValuePair2
2015-05-19 15:44 504/** * 键值对 * * @author verno ... -
OrderUtil -排序工具
2014-05-22 09:35 650/** * 排序工具 * * @author vern ... -
ZipUtils
2014-05-22 09:35 441import java.io.BufferedOutputSt ... -
TimerUtils
2014-05-22 09:34 475import org.slf4j.Logger; import ... -
Excel View 4 Spring
2014-05-22 09:29 337Excel View 4 Spring @RequestMa ... -
DOMUtils xml操作实用类
2014-04-07 21:41 801/* * Copyright 2001-2004 The A ... -
生成验证码 数学+字母
2014-04-01 10:08 437public static String getCharAnd ...
相关推荐
"更新记录.docx"文件可能是项目开发过程中对SpringJdbcTemplate封装工具类的修改日志,记录了每次更新的功能改进、bug修复或性能优化等内容,这对于版本管理和问题排查非常有帮助。 通过上述分析,我们可以看到...
标题中的“一个简单易用可对象操作基于SpringJdbcTemplate的JDBC帮助包”指的是一个专为简化Java开发中数据库操作而设计的工具包。这个工具包利用了Spring的JdbcTemplate,它是一个轻量级的ORM(对象关系映射)框架...
3. **RowMapper**:这是将结果集转换为Java对象的接口,用户可以根据实际需求实现该接口,将每一行数据映射为一个Java对象。 4. **SqlProvider**:用于动态生成SQL,可以配合SpEL(Spring Expression Language)...
JdbcTemplate基本使用 JDBC已经能够满足大部分用户最基本的需求,但是在使用JDBC时,必须自己来管理数据库资源如:获取PreparedStatement,设置SQL语句参数,关闭连接等步骤。 JdbcTemplate是Spring对JDBC的...
jdbcTemplate-runtime-update 使用Spring Boot在运行时更改DataSources和JdbcTemplate 稍后,我将文件编辑为一个小型项目,并编写有用且详细的说明
标题 "jdbcTemplate-spring对jdbc的支持" 涉及到的是Spring框架中的一个核心组件——JdbcTemplate,它是Spring对Java数据库连接(JDBC)的一种封装,用于简化数据库操作。JdbcTemplate提供了一种模板方法模式,使得...
这样的图示对于理解JdbcTemplate的工作原理以及如何在项目中实际应用它是很有帮助的。 标签"spring 尚硅谷 JdbcTemplate"说明这个文档可能是由"尚硅谷"发布,"尚硅谷"是一家专注于IT教育和培训的机构,他们提供的...
Spring 框架 JdbcTemplate 类中查询方法介绍 JdbcTemplate 是 Spring 框架中 org.springframework.jdbc.core 包提供的 JDBC 模板类,它是核心类,其他模板类都是基于它封装完成的。JdbcTemplate 类主要提供四类方法...
`UserRowMapper`是自定义的类,用于将结果集的每一行转换为`User`对象。 **3. 自动处理异常和资源关闭** JdbcTemplate会自动处理JDBC相关的异常,比如SQLException,并转换为Spring的DataAccessException。此外,...
本资源是一个完整的通过Servlet-Service-Dao-JdbcTemplate访问MySQL数据库的JavaWeb Project,可以直接导入到Eclipse中进行调试运行,注意默认编译器是JDK1.8。
- **自动化程度**:JdbcTemplate 在自动映射和类型转换上略逊于 MyBatis,MyBatis 可以直接将结果映射到 POJO。 - **学习曲线**:JdbcTemplate 更简单,易于上手;MyBatis 需要学习更多的配置和映射规则。 ### 6. ...
Spring的JdbcTemplate是Spring框架中用于简化数据库操作的工具类,它是基于JDBC但又抽象出了一层,避免了直接与数据库驱动API交互,从而提高了代码的可读性和可维护性。本文将深入探讨Spring JdbcTemplate的常用方法...
本文将深入探讨`JdbcTemplate`通用泛型Dao实现的相关知识点,帮助开发者更好地理解和应用这一技术。 首先,让我们了解什么是`JdbcTemplate`。它是Spring框架的一部分,用于处理SQL操作。`JdbcTemplate`提供了一组...
在SSHnote_Spring_14_JDBCTemplate这个压缩包中,可能包含了关于这些知识点的详细教程、示例代码和练习,帮助读者深入理解和掌握Spring JdbcTemplate的使用。学习这些内容,开发者能够更加高效地在Spring环境中进行...
JdbcTemplate是Spring提供的一个SQL模板类,它降低了与数据库交互的复杂性。通过使用JdbcTemplate,开发者可以避免大量的手动事务管理、结果集转换等繁琐工作,使代码更加简洁且易于测试。JdbcTemplate支持SQL语句的...
这些工具类可以帮助优化数据库操作,比如通过连接池提高数据库连接的效率,或者提供更安全、可维护的SQL构建方式,避免硬编码SQL字符串。 这个JdbcTemplate扩展可能添加了一些额外的功能,比如: 1. **事务管理**...
这篇博客文章的标题"打印JdbcTemplate执行sql"主要涉及如何在使用`JdbcTemplate`时,追踪并打印出执行的SQL语句,这对于调试和性能分析非常有帮助。接下来,我们将深入探讨`JdbcTemplate`的工作原理以及如何实现SQL...
与传统的JDBC相比,JdbcTemplate提供了一些高级特性,比如异常转换、SQL参数设置以及结果集提取等。 **1.2 JdbcTemplate特点** - **简化异常处理**:将JDBC中抛出的运行时异常转换为Spring的`DataAccessException`...
`query()`方法的第三个参数是RowMapper,它负责将数据库行转换为User对象。 ### 更新(Update) 更新数据时,我们会使用`update()`方法执行SQL UPDATE语句。假设我们想更新用户的密码: ```java String updateSql...