先说一下需求:最近做一个项目其中需要将前台传来的数组存到数据库,但是这个表里有15个字段,集合是不固定的,然后要把这个集合的数值赋给这个类的相应属性,然后存到数据库中。集合长度应小于等于这个类属性的个数。下面是代码
实体类
package com.test.flect; public class TbFilterLevel { private Long id; private Long itemId; private String value1; private String value2; private String value3; private String value4; private String value5; private String value6; private String value7; private String value8; private String value9; private String value10; private String value11; private String value12; private String value13; private String value14; private String value15; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Long getItemId() { return itemId; } public void setItemId(Long itemId) { this.itemId = itemId; } public String getValue1() { return value1; } public void setValue1(String value1) { this.value1 = value1 == null ? null : value1.trim(); } public String getValue2() { return value2; } public void setValue2(String value2) { this.value2 = value2 == null ? null : value2.trim(); } public String getValue3() { return value3; } public void setValue3(String value3) { this.value3 = value3 == null ? null : value3.trim(); } public String getValue4() { return value4; } public void setValue4(String value4) { this.value4 = value4 == null ? null : value4.trim(); } public String getValue5() { return value5; } public void setValue5(String value5) { this.value5 = value5 == null ? null : value5.trim(); } public String getValue6() { return value6; } public void setValue6(String value6) { this.value6 = value6 == null ? null : value6.trim(); } public String getValue7() { return value7; } public void setValue7(String value7) { this.value7 = value7 == null ? null : value7.trim(); } public String getValue8() { return value8; } public void setValue8(String value8) { this.value8 = value8 == null ? null : value8.trim(); } public String getValue9() { return value9; } public void setValue9(String value9) { this.value9 = value9 == null ? null : value9.trim(); } public String getValue10() { return value10; } public void setValue10(String value10) { this.value10 = value10 == null ? null : value10.trim(); } public String getValue11() { return value11; } public void setValue11(String value11) { this.value11 = value11 == null ? null : value11.trim(); } public String getValue12() { return value12; } public void setValue12(String value12) { this.value12 = value12 == null ? null : value12.trim(); } public String getValue13() { return value13; } public void setValue13(String value13) { this.value13 = value13 == null ? null : value13.trim(); } public void setValue14(String value14) { this.value14 = value14 == null ? null : value14.trim(); } public String getValue14() { return value14; } public String getValue15() { return value15; } public void setValue15(String value15) { this.value15 = value15 == null ? null : value15.trim(); } }
封装的方法:
private void testReflect(Object model,List<?> list) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException{ // 获取实体类的所有属性,返回Field数组 Field[] field = model.getClass().getDeclaredFields(); // 获取属性的名字 String[] modelName = new String[field.length]; String[] modelType = new String[field.length]; for (int i = 0; i < field.length; i++){ // 获取属性的名字 String name = field[i].getName(); modelName[i] = name; // 获取属性类型 String type = field[i].getGenericType().toString(); modelType[i] = type; //关键。。。可访问私有变量 field[i].setAccessible(true); // 将属性的首字母大写 name = name.replaceFirst(name.substring(0, 1), name.substring(0, 1) .toUpperCase()); if (type.equals("class java.lang.String")){ //我只需要String类型的赋值,所以只加了String类型的约束,其他的可以自己加,吧String换成别的类即可 // 如果type是类类型,则前面包含"class ",后面跟类名 Method m = model.getClass().getMethod("get" + name); // 调用getter方法获取属性值 String value = (String) m.invoke(model); String str=null; if (value == null){ m = model.getClass().getMethod("set"+name,String.class); if(list.size()>i-2&&list.size()<field.length-2){//我的String类型的属性是15个,所以这个集合长度这么约束的,这样不会有多余的值,其他长度请自己处理 m.invoke(model, list.get(i-2)); }else if(i-2==list.size()){ m = model.getClass().getMethod("set"+name,String.class); m.invoke(model, str); }else{ break; } } } } }
如有疑问请联系我,我们一起讨论学习一下。