可能有点不完善。不过经测试,能满足大部分需求。
直接上干货!
?
class="java" name="code">public class DataHelper {
public static void putDataIntoEntity(Map map, Object entity) throws SecurityException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
if (entity != null && map != null&& map.size()>0) {
//获取传入实体类的属性Filed数组
Field[] field_arr = Class.forName(entity.getClass().getCanonicalName()).getDeclaredFields();
//遍历数组
for (Field field : field_arr) {
//获取属性名称
String fieldName = field.getName();
//判断map中是否存在对应的属性名称(注:这个方法要想使用就必须保证map中的key与实体类的属性名称一致)
if (map.containsKey(fieldName)) {
//调用本类中的帮助方法来获取当前属性名对应的方法名(“set”为getMethodName方法的第二个参数)
String methodName