`
liuguly
  • 浏览: 9775 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

Bean自动赋值

 
阅读更多
package test;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import entity.HelpTopicId;

public class TestMap
{

	public static void restoreProp(Object bean, Map<String, Object> map)
	{
		BeanInfo beanInfo;
		try
		{
			beanInfo = Introspector.getBeanInfo(bean.getClass());
			PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
			for (PropertyDescriptor propertyDescriptor : descriptors)
			{
				// 属性名字
				String fieldName = propertyDescriptor.getName().toUpperCase();
				String fieldType = propertyDescriptor.getPropertyType().getName();
				if (map.containsKey(fieldName))
				{
					Method setter = propertyDescriptor.getWriteMethod();
					if("java.lang.Short".equals(fieldType))
					{
						setter.invoke(bean, new Object[] { (Short.parseShort(map.get(fieldName)+"")) });
						continue;
					}
					if("java.lang.Integer".equals(fieldType))
					{
						setter.invoke(bean, new Object[] { (Integer.parseInt(map.get(fieldName)+"")) });
						continue;
					}
					setter.invoke(bean, new Object[] { map.get(fieldName) });
				}
			}
		} catch (IntrospectionException e)
		{
			e.printStackTrace();
		} catch (IllegalArgumentException e)
		{
			e.printStackTrace();
		} catch (IllegalAccessException e)
		{
			e.printStackTrace();
		} catch (InvocationTargetException e)
		{
			e.printStackTrace();
		}
	}

	public static List<Map<String, Object>> readCSV(File file)
	{
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try
		{
			BufferedReader br = new BufferedReader(new InputStreamReader(
					new FileInputStream(file)));
			String msg = "";
			int i = 0;
			while ((msg = br.readLine()) != null)
			{
				String[] line = msg.split(",");
				if (line == null || "".equals(line[0]))
				{
					break;
				}
				Map<String, Object> map = new HashMap<String, Object>();
				if (i == 0)
				{
					for (int j = 0; j < line.length; j++)
					{
						line[j] = line[j].replaceAll("\"", "").replaceAll("_", "").toUpperCase();
						if(j==0)
						{
							line[j]=line[j].substring(line[j].indexOf("H"), line[j].length());
						}
						map.put(j + "", line[j]);
					}
					list.add(map);
					i++;
					continue;
				}
				for (int k = 0; k < line.length; k++)
				{
					line[k] = line[k].replaceAll("\"", "");
					map.put(list.get(0).get(k + "")+"", line[k]);
				}
				list.add(map);
			}
		} catch (FileNotFoundException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return list;
	}

	public static HashMap<String, Object> backupProp(Object bean)
	{
		HashMap<String, Object> result = new HashMap<String, Object>();
		try
		{
			BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
			PropertyDescriptor[] descriptors = beanInfo
					.getPropertyDescriptors();
			for (PropertyDescriptor propertyDescriptor : descriptors)
			{
				String fieldName = propertyDescriptor.getName();
				Method getter = propertyDescriptor.getReadMethod();
				Object fieldValue = getter.invoke(bean, new Object[] {});
				System.out.println("bean 的属性名字:" + fieldName + " 属性值:"
						+ fieldValue);
				if (!fieldName.equalsIgnoreCase("class"))
				{
					result.put(fieldName, fieldValue);
				}
			}
		} catch (IntrospectionException e)
		{
			e.printStackTrace();
		} catch (IllegalArgumentException e)
		{
			e.printStackTrace();
		} catch (IllegalAccessException e)
		{
			e.printStackTrace();
		} catch (InvocationTargetException e)
		{
			e.printStackTrace();
		}
		return result;
	}

	public static void main(String[] args)
	{
		// BookAuthor author = new BookAuthor();
		// author.setName("lx");
		//
		// Book book = new Book();
		// book.setBookName("java");
		// book.setPrice(22);
		// book.setAuthor(author);
		//
		// backupProp(book);
		List<Map<String, Object>> list = readCSV(new File("F:\\Documents and Settings\\Administrator\\桌面\\help_topic.csv"));
		for(int i=1;i<list.size();i++)
		{
			    HelpTopicId bean = new HelpTopicId();
		       restoreProp(bean, list.get(i));
		       System.out.println(bean.toString());
		}
	}
}

 

分享到:
评论

相关推荐

    Bean之间赋值工具

    自制工具,方便在两个Bean之间赋值,而且自动追加注释。 .netframework2下动作。

    Struts中FormBean的日期类型字段赋值自动转化

    NULL 博文链接:https://zywang.iteye.com/blog/413003

    【Spring】总结Spring整合Mybatis的底层原理实现步骤

    (鲁班学院公开课笔记) Dao:数据访问层,提供让Service层...2、从Spring容器中拿到的对象,会给对象里面的包含@AutoWired注解的属性进行自动注入赋值。而手动new出来的对象不会自动注入赋值。 Spring容器中的Bean

    iBatis SQL Maps开发指南.pdf

    例子3:用预赋值的结果对象查询成对象(select) 例子4:查询成对象List(select) 例子5:自动提交 例子6:用结果集边界查询成对象List(select) 例子7:用RowHandler执行查询(select) 例子8:查询成Paginated ...

    客户关系管理系统框架搭建(二)

    -- 1 配置注解的自动扫描--&gt; &lt;!--3 配置本地化代理工程bean,这是spring整合hibernate的入口 --&gt; &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3....

    Spring攻略(第二版 中文高清版).part2

    1.12 用@Autowired和@Resource自动装配Bean 41 1.12.1 问题 41 1.12.2 解决方案 41 1.12.3 工作原理 41 1.13 继承Bean配置 47 1.13.1 问题 47 1.13.2 解决方案 47 1.13.3 工作原理 48 1.14 从...

    Spring攻略(第二版 中文高清版).part1

    1.12 用@Autowired和@Resource自动装配Bean 41 1.12.1 问题 41 1.12.2 解决方案 41 1.12.3 工作原理 41 1.13 继承Bean配置 47 1.13.1 问题 47 1.13.2 解决方案 47 1.13.3 工作原理 48 1.14 从...

    Java核心技术II(第8版)

    4.5.5 获取自动生成键 4.6 可滚动和可更新的结果集 4.6.1 可滚动的结果集 4.6.2 可更新的结果集 4.7 行集 4.7.1 被缓存的行集 4.8 元数据 4.9 事务 4.9.1 保存点 4.9.2 批量更新 4.9.3 高级SQL类型 4.10 Web与企业...

    乐优商城.xmind

    GET和POST请求传的参数会自动转换赋值到@RequestParam 所注解的变量上 defaultValue 默认值 required 默认值为true , 当为false时 这个注解可以不传这个参数 null || .size()==0 ResponseEntity...

    轻量级java web MVC框架

    一个非常简单的MVC框架,实现了...有查询模板QueryTemplate,命名查询NamedQueryTemplate及bean作为参数的BeanQueryTemplate等。支持返回javabean类型,javabean列表, MAP类型,map列表类型,统计结果queryForInt等。

    freemarker总结

    FreeMarker对空值的处理非常严格,FreeMarker的变量必须有值,没有被赋值的变量就会抛出异常,因为FreeMarker未赋值 的变量强制出错可以杜绝很多潜在的错误,如缺失潜在的变量命名,或者其他变量错误.这里所说的空值,...

    Think in Java(中文版)chm格式

    3.1.4 自动递增和递减 3.1.5 关系运算符 3.1.6 逻辑运算符 3.1.7 按位运算符 3.1.8 移位运算符 3.1.9 三元if-else运算符 3.1.10 逗号运算符 3.1.11 字串运算符+ 3.1.12 运算符常规操作规则 3.1.13 造型...

    JAVA_Thinking in Java

    3.1.4 自动递增和递减 3.1.5 关系运算符 3.1.6 逻辑运算符 3.1.7 按位运算符 3.1.8 移位运算符 3.1.9 三元if-else运算符 3.1.10 逗号运算符 3.1.11 字串运算符+ 3.1.12 运算符常规操作规则 3.1.13 造型运算符 3.1.14...

    Java初学者入门教学

    3.1.4 自动递增和递减 3.1.5 关系运算符 3.1.6 逻辑运算符 3.1.7 按位运算符 3.1.8 移位运算符 3.1.9 三元if-else运算符 3.1.10 逗号运算符 3.1.11 字串运算符+ 3.1.12 运算符常规操作规则 3.1.13 造型运算符 3.1.14...

    ThinkInJava

    3.1.4 自动递增和递减 3.1.5 关系运算符 3.1.6 逻辑运算符 3.1.7 按位运算符 3.1.8 移位运算符 3.1.9 三元if-else运算符 3.1.10 逗号运算符 3.1.11 字串运算符+ 3.1.12 运算符常规操作规则 3.1.13 造型运算符 3.1.14...

    java 编程入门思考

    3.1.4 自动递增和递减 3.1.5 关系运算符 3.1.6 逻辑运算符 3.1.7 按位运算符 3.1.8 移位运算符 3.1.9 三元if-else运算符 3.1.10 逗号运算符 3.1.11 字串运算符+ 3.1.12 运算符常规操作规则 3.1.13 造型运算符 3.1.14...

    thinkinjava

    3.1.4 自动递增和递减 3.1.5 关系运算符 3.1.6 逻辑运算符 3.1.7 按位运算符 3.1.8 移位运算符 3.1.9 三元if-else运算符 3.1.10 逗号运算符 3.1.11 字串运算符+ 3.1.12 运算符常规操作规则 3.1.13 造型运算符 3.1.14...

    Thinking in Java简体中文(全)

    3.1.4 自动递增和递减 3.1.5 关系运算符 3.1.6 逻辑运算符 3.1.7 按位运算符 3.1.8 移位运算符 3.1.9 三元if-else运算符 3.1.10 逗号运算符 3.1.11 字串运算符+ 3.1.12 运算符常规操作规则 3.1.13 造型运算符 3.1.14...

    java联想(中文)

    3.1.4 自动递增和递减 3.1.5 关系运算符 3.1.6 逻辑运算符 3.1.7 按位运算符 3.1.8 移位运算符 3.1.9 三元if-else运算符 3.1.10 逗号运算符 3.1.11 字串运算符+ 3.1.12 运算符常规操作规则 3.1.13 造型运算符 3.1.14...

    Thinking in Java(中文版 由yyc,spirit整理).chm

    3.1.4 自动递增和递减 3.1.5 关系运算符 3.1.6 逻辑运算符 3.1.7 按位运算符 3.1.8 移位运算符 3.1.9 三元if-else运算符 3.1.10 逗号运算符 3.1.11 字串运算符+ 3.1.12 运算符常规操作规则 3.1.13 造型运算符 3.1.14...

Global site tag (gtag.js) - Google Analytics