import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.lang.reflect.Field;
import java.util.*;
/**
* Created by 黄永光
* User: Administrator
* Date: 2011-8-11
* Time: 10:30:15
* <p/>
* 将实体工具类,Spring查询对象封装实体工具.
*/
public class EntityTool {
static Log LOGGER = LogFactory.getLog(EntityTool.class);
/**
* 将map转成实体,map中的key必须与实体中的属性名相同才可以转换成功
*
* @param entityMap 实体map,属性键值对,其中key为实体的属性名,value为属性的值
* @param entityType 实体类
* @param <T>
* @return
*/
public static <T> T converToEntity(Map<String, Object> entityMap, Class<T> entityType) {
T result = null;
try {
result = entityType.newInstance();
Map<String, Field> fieldsMap = getFieldMap(entityType);
Field field = null;
for (Map.Entry<String, Object> entry : entityMap.entrySet()) {
field = fieldsMap.get(entry.getKey().toLowerCase());
if (field == null) {
LOGGER.error("键值" + entry.getKey().toLowerCase() + "在" + entityType.getName() + "中找不到对应的属性!");
throw new Exception();
}
field.setAccessible(true);
Object value = entry.getValue();
if (value == null) {
continue;
}
if (isBasicType(field.getType())) {
// System.out.println(field.getName()+"="+value);
System.out.println(field.getType());
if (field.getType().equals(boolean.class) || field.getType().equals(Boolean.class)) {
if (value.equals(new Boolean(true) || value.equals(true)) || value.equals(new Boolean(true) || value.equals(true))) {
field.set(result, value);
} else {
if (value.toString().equals("1")) {
field.set(result, true);
} else {
field.set(result, false);
}
}
} else {
field.set(result, value);
}
} else {
/*
设置复合类型属性,只设id如果存在
*/
Class fildType = field.getType();
Field idField = null;
try {
idField = getDeclaredField("id", fildType, true);
} catch (NoSuchFieldException e) {
continue;
}
if (idField != null) {
idField.setAccessible(true);
Object compositeFieldValue = fildType.newInstance();
idField.set(compositeFieldValue, value);
field.set(result, compositeFieldValue);
} else {
LOGGER.warn("复合属性" + entry.getKey().toLowerCase() + "没有id属性,没有赋值!");
}
}
}
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return result;
}
/**
* 得到某类的属性
* @param fieldName 属性名
* @param clazz 类
* @param withSuperClass 是否从父类中取属性
* @return
*/
private static Field getDeclaredField(String fieldName, Class clazz, boolean withSuperClass) throws NoSuchFieldException {
Field field = null;
try {
field = clazz.getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
if (field == null && withSuperClass) {
Class superClazz = clazz.getSuperclass();
if (superClazz != null) {
field = getDeclaredField(fieldName, superClazz, withSuperClass);
}
} else {
throw e;
}
}
return field;
}
/**
* 将多个map转换成实体集合
*
* @param datas
* @param entityType
* @param <E>
* @return
*/
public static <E> List<E> convertToEntities(List<Map<String, Object>> datas, Class<E> entityType) {
List<E> result = new ArrayList<E>();
E data = null;
for (Map<String, Object> map : datas) {
data = converToEntity(map, entityType);
result.add(data);
data = null;
}
return result;
}
/**
* 判断是不是基本类型
*
* @param type
* @return
*/
public static boolean isBasicType(Class type) {
if (type.isPrimitive()) {
return true;
}
if (type.getPackage().getName().equals("java.lang") || type.getPackage().getName().equals(("java.util"))) {
return true;
} else if (type.equals(java.sql.Date.class)) {
return true;
} else {
return false;
}
}
/**
* 得到某个类的属性map key为属性名,value为属性
*
* @param entityClass
* @return
*/
public static Map<String, Field> getFieldMap(Class entityClass) {
Field[] fields = entityClass.getDeclaredFields();
Map<String, Field> fieldsMap = new HashMap<String, Field>();
for (Field field : fields) {
fieldsMap.put(field.getName().toLowerCase(), field);
}
Class superclass = entityClass.getSuperclass();
if (superclass != null) {
fieldsMap.putAll(getFieldMap(superclass));
}
return fieldsMap;
}
public static void main(String[] args) {
/*Map<String,Field> fields= getFieldMap(User.class);
for(Map.Entry<String,Field> entry:fields.entrySet()){
System.out.println(entry.getKey()+":"+entry.getValue());
}*/
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", null);
map.put("userId", null);
map.put("employee", null);
map.put("password", null);
map.put("userStatus", null);
map.put("historyPwdList", null);
map.put("statusChangeTime", null);
map.put("updateTimeStamp", null);
map.put("passwordUpdateTime", null);
map.put("createTimeStamp", null);
map.put("removed", null);
User user = converToEntity(map, User.class);
System.out.println(user.getId());
}
}
分享到:
相关推荐
Spring JdbcTemplate查询实例
strut2+spring+springjdbctemplate做的简易登录系统
一个非常简单基于注解的Spring JdbcTemplate,供初学者参考用。
Spring JDBCTemplate连接池jar包
Druid数据库连接池的SpringJDBCTemplate所需的jar包,Druid数据库连接池的SpringJDBCTemplate所需的jar包,Druid数据库连接池的SpringJDBCTemplate所需的jar包,Druid数据库连接池的SpringJDBCTemplate所需的jar包,...
Spring JdbcTemplate调用Oracle存储过程输出游标结果集实现增删改查
模仿spring jdbcTemplate的粗略实现,只有很小的参考价值,如果是java初学者可以使用这个封装好的工具进行数据库操作,只需要在db.properties里配置好driver,url等信息
使用Spring的JdbcTemplate实现分页功能
spring-jdbcTemplate实例工程
Spring JdbcTemplate的操作,包括接口,BaseDao,log4J配置文件,主要为oracle数据库操作,很多特殊方法只对oracle有效
SpringJdbcTemplate封装工具包,包括规范model格式接口,封装SpringJdbcTemplate,实现分页,自适应多种数据库
JdbcTemplate api 下载 Spring
1.Spring4前 spring-jdbc包是独立的,4以后spring-jdbc 就已经没有了
NULL 博文链接:https://humlzy.iteye.com/blog/2382685
spring JdbcTemplate query方法使用示例,欢迎下载借鉴
使用Spring的JdbcTemplate调用Oracle的存储过程
spring jdbctemplate组件的简单实例。可以直接运行该实例来学习spring的jdbctemplate。处于初学者,或开发互联网性能要求较高的比较有料。
在查询结果为空或查询结果大于 1 行时,JdbcTemplate 会抛出异常。这些异常包括: * org.springframework.dao.EmptyResultDataAccessException:查询结果为空 * org.springframework.dao....
Spring-JdbcTemplate
Spring jdbcTemplate 调用 Oracle 存储过程...我们创建了一个简单的存储过程,用于查询员工信息,并使用 Spring jdbcTemplate 调用存储过程来获取结果集。这种方法可以帮助我们简化数据库操作,并提高应用程序的性能。