package com.springboot.eurekazuul.utils;
import java.beans.SimpleBeanInfo;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import org.springframework.stereotype.Component;
/**
* 将map转换为bean对象
* @author Administrator
*
*/
public class MapToBeanUtil {
@SuppressWarnings("unchecked")
public static Object mapToObject(Class clazz, Map<String,Object> map){
if(null == map){
return null;
}
Field[] fields = clazz.getDeclaredFields(); //取到类下所有的属性,也就是变量名
Field field;
Object o = null;
try {
o = clazz.newInstance();
} catch (InstantiationException e1) {
e1.printStackTrace();
} catch (IllegalAccessException e1) {
e1.printStackTrace();
}
for(int i=0; i<fields.length; i++){
field = fields[i];
String fieldName = field.getName();
//把属性的第一个字母处理成大写
String stringLetter=fieldName.substring(0, 1).toUpperCase();
//取得set方法名,比如setBbzt
String setterName="set"+stringLetter+fieldName.substring(1);
//真正取得set方法。
Method setMethod;
Class fieldClass = field.getType();
try {
if(isHaveSuchMethod(clazz, setterName)){
if(fieldClass == String.class){
setMethod = clazz.getMethod(setterName, fieldClass);
setMethod.invoke(o, String.valueOf(map.get(fieldName)));//为其赋值
}else if(fieldClass == Integer.class || fieldClass == int.class){
setMethod = clazz.getMethod(setterName, fieldClass);
setMethod.invoke(o, Integer.parseInt(String.valueOf(map.get(fieldName))));//为其赋值
}else if(fieldClass == Boolean.class || fieldClass == boolean.class){
setMethod = clazz.getMethod(setterName, fieldClass);
setMethod.invoke(o, Boolean.getBoolean(String.valueOf(map.get(fieldName))));//为其赋值
}else if(fieldClass == Short.class || fieldClass == short.class){
setMethod = clazz.getMethod(setterName, fieldClass);
setMethod.invoke(o, Short.parseShort(String.valueOf(map.get(fieldName))));//为其赋值
}else if(fieldClass == Long.class || fieldClass == long.class){
setMethod = clazz.getMethod(setterName, fieldClass);
setMethod.invoke(o, Long.parseLong(String.valueOf(map.get(fieldName))));//为其赋值
}else if(fieldClass == Double.class || fieldClass == double.class){
setMethod = clazz.getMethod(setterName, fieldClass);
setMethod.invoke(o, Double.parseDouble(String.valueOf(map.get(fieldName))));//为其赋值
}else if(fieldClass == Float.class || fieldClass == float.class){
setMethod = clazz.getMethod(setterName, fieldClass);
setMethod.invoke(o, Float.parseFloat(String.valueOf(map.get(fieldName))));//为其赋值
}else if(fieldClass == BigInteger.class ){
setMethod = clazz.getMethod(setterName, fieldClass);
setMethod.invoke(o, BigInteger.valueOf(Long.parseLong(String.valueOf(map.get(fieldName)))));//为其赋值
}else if(fieldClass == BigDecimal.class){
setMethod = clazz.getMethod(setterName, fieldClass);
setMethod.invoke(o, BigDecimal.valueOf(Long.parseLong(String.valueOf(map.get(fieldName)))));//为其赋值
}else if(fieldClass == Date.class){
setMethod = clazz.getMethod(setterName, fieldClass);
if(map.get(fieldName).getClass() == java.sql.Date.class){
setMethod.invoke(o, new Date(((java.sql.Date)map.get(fieldName)).getTime()));//为其赋值
}else if(map.get(fieldName).getClass() == java.sql.Time.class){
setMethod.invoke(o, new Date(((java.sql.Time)map.get(fieldName)).getTime()));//为其赋值
}else if(map.get(fieldName).getClass() == java.sql.Timestamp.class){
setMethod.invoke(o, new Date(((java.sql.Timestamp)map.get(fieldName)).getTime()));//为其赋值
}
}
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
return o;
}
/**
* 将Map对象通过反射机制转换成Bean对象
*
* @param map 存放数据的map对象
* @param clazz 待转换的class
* @return 转换后的Bean对象
* @throws Exception 异常
*/
public static Object mapToBean(Map<String, Object> map, Class<?> clazz) throws Exception {
Object obj = clazz.newInstance();
if(map != null && map.size() > 0) {
for(Map.Entry<String, Object> entry : map.entrySet()) {
String propertyName = entry.getKey(); //属性名
Object value = entry.getValue();
String setMethodName = "set"
+ propertyName.substring(0, 1).toUpperCase()
+ propertyName.substring(1);
Field field = getClassField(clazz, propertyName);
if(field==null)
continue;
Class<?> fieldTypeClass = field.getType();
value = convertValType(value, fieldTypeClass);
try{
clazz.getMethod(setMethodName, field.getType()).invoke(obj, value);
}catch(NoSuchMethodException e){
e.printStackTrace();
}
}
}
return obj;
}
/**
* 获取指定字段名称查找在class中的对应的Field对象(包括查找父类)
*
* @param clazz 指定的class
* @param fieldName 字段名称
* @return Field对象
*/
private static Field getClassField(Class<?> clazz, String fieldName) {
if( Object.class.getName().equals(clazz.getName())) {
return null;
}
Field []declaredFields = clazz.getDeclaredFields();
for (Field field : declaredFields) {
if (field.getName().equals(fieldName)) {
return field;
}
}
Class<?> superClass = clazz.getSuperclass();
if(superClass != null) {// 简单的递归一下
return getClassField(superClass, fieldName);
}
return null;
}
/**
* 将Object类型的值,转换成bean对象属性里对应的类型值
*
* @param value Object对象值
* @param fieldTypeClass 属性的类型
没有合适的资源?快使用搜索试试~ 我知道了~
springcloud-shiro
共1239个文件
xml:690个
jar:437个
class:38个
4星 · 超过85%的资源 需积分: 38 109 下载量 43 浏览量
2019-02-21
14:53:02
上传
评论 2
收藏 130.73MB RAR 举报
温馨提示
基於springboot的shiro項目,使用了zuul等springcloud項目
资源详情
资源评论
资源推荐
收起资源包目录
springcloud-shiro (1239个子文件)
MyFilter.class 4KB
UserInfoController.class 4KB
ShiroConfig.class 4KB
UserInfoController.class 4KB
MyShiroRealm.class 4KB
MyShiroRealm.class 4KB
ShiroConfig.class 4KB
UUIDUtil.class 3KB
UserInfo.class 3KB
UserInfo.class 3KB
UserInfo.class 3KB
BusinessMap.class 3KB
Test.class 3KB
SysPermission.class 2KB
SysPermission.class 2KB
SysPermission.class 2KB
RedisController.class 2KB
ResponseData.class 2KB
SysRole.class 2KB
SysRole.class 2KB
SysRole.class 2KB
EurekaSecurity2Application.class 2KB
EurekaSecurity2Application.class 2KB
EurekaZuulApplication.class 1KB
EurekaServerApplication.class 1KB
EurekaServerApplication.class 1KB
UserInfoServiceImpl.class 1KB
ResponseController.class 1KB
User.class 954B
UserInfoDao.class 785B
ResponseController.class 697B
EurekaSecurity2ApplicationTests.class 683B
EurekaServerApplicationTests.class 668B
EurekaZuulApplicationTests.class 658B
EurekaUserApplicationTests.class 658B
UserService.class 616B
UserService.class 611B
UserInfoService.class 328B
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
.gitignore 268B
.gitignore 268B
.gitignore 268B
.gitignore 268B
403.html 145B
403.html 145B
403.html 145B
eureka-security2.iml 21KB
eureka-user.iml 20KB
eureka-zuul.iml 19KB
eureka-server.iml 19KB
hibernate-core-5.3.7.Final.jar 6.65MB
bcprov-jdk15on-1.60.jar 4MB
bcprov-jdk15on-1.60.jar 4MB
bcprov-jdk15on-1.56.jar 3.29MB
byte-buddy-1.9.5.jar 3.09MB
druid-1.1.9.jar 2.59MB
guava-16.0.jar 2.12MB
guava-16.0.jar 2.12MB
guava-15.0.jar 2.07MB
mysql-connector-java-8.0.13.jar 2.03MB
aspectjweaver-1.9.2.jar 1.96MB
aspectjweaver-1.9.2.jar 1.96MB
aspectjweaver-1.8.13.jar 1.84MB
freemarker-2.3.28.jar 1.45MB
reactor-core-3.2.3.RELEASE.jar 1.38MB
reactor-core-3.2.3.RELEASE.jar 1.38MB
spring-data-redis-2.1.3.RELEASE.jar 1.37MB
spring-data-redis-2.1.3.RELEASE.jar 1.37MB
spring-web-5.1.3.RELEASE.jar 1.31MB
spring-web-5.1.3.RELEASE.jar 1.31MB
jackson-databind-2.9.7.jar 1.29MB
jackson-databind-2.9.7.jar 1.29MB
jackson-databind-2.9.6.jar 1.29MB
ehcache-core-2.6.11.jar 1.29MB
lettuce-core-5.1.3.RELEASE.jar 1.28MB
lettuce-core-5.1.3.RELEASE.jar 1.28MB
spring-core-5.1.3.RELEASE.jar 1.23MB
spring-core-5.1.3.RELEASE.jar 1.23MB
spring-boot-autoconfigure-2.1.1.RELEASE.jar 1.21MB
spring-boot-autoconfigure-2.1.1.RELEASE.jar 1.21MB
spring-web-5.0.7.RELEASE.jar 1.2MB
spring-core-5.0.7.RELEASE.jar 1.17MB
spring-data-commons-2.1.3.RELEASE.jar 1.11MB
spring-data-commons-2.1.3.RELEASE.jar 1.11MB
spring-boot-autoconfigure-2.0.3.RELEASE.jar 1.11MB
rxjava-1.3.8.jar 1.11MB
rxjava-1.3.8.jar 1.11MB
rxjava-1.3.8.jar 1.11MB
hibernate-validator-6.0.13.Final.jar 1.1MB
hibernate-validator-6.0.13.Final.jar 1.1MB
hibernate-validator-6.0.10.Final.jar 1.08MB
spring-context-5.1.3.RELEASE.jar 1.05MB
spring-context-5.1.3.RELEASE.jar 1.05MB
spring-context-5.0.7.RELEASE.jar 1.04MB
commons-math-2.2.jar 965KB
commons-math-2.2.jar 965KB
commons-math-2.2.jar 965KB
共 1239 条
- 1
- 2
- 3
- 4
- 5
- 6
- 13
Mr.DreamerZ
- 粉丝: 17
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论5