java利用映射表名称反射创建实体类并赋属性值

  1.hibernate中首先进行初始化,将对应的表名和类名以键值对的方式存放到map中

  private Map<String, String> mappings;//全局变量

  /**
  * 根据数据库表名获取实体类
  */
  public void initMappings() {
    if (mappings == null) {
    mappings = new HashMap<String, String>();
    SessionFactory factory = this.getSessionFactory();
    Map metaMap = factory.getAllClassMetadata();
    for (String key : (Set<String>) metaMap.keySet()) {
      AbstractEntityPersister classMetadata = (AbstractEntityPersister) metaMap.get(key);
      String tableName = classMetadata.getTableName().toLowerCase();
      int index = tableName.indexOf(".");
      if (index >= 0) {
        tableName = tableName.substring(index + 1);
      }
      String className = classMetadata.getEntityMetamodel().getName();
      mappings.put(tableName, className);
      }
    }
  }

  2.调用方法,传入表名得到对应的实体类名

  public String getEntityNameByTableName(String tableName) {
    initMappings();
    return mappings.get(tableName);
  }

  3.根据实体类名创建实体类

  /**
  *listobj:要赋的属性值集合,顺序要和实体类属性顺序一致

  */

  public Object getByReflect(String tableName, List listobj)throws Exception {

      Class<?> model = Class.forName(tableName);
      Object object = new Object();

      if (model != null) {
         Field[] field = model.getDeclaredFields();
         String[] modelName = new String[field.length];
         String[] modelType = new String[field.length];

         object = model.newInstance();
         Method m = null;

         for (int i = 1; i <field.length ; i++) {
            String name = field[i].getName();
            Object value = null;
            name = name.substring(0, 1).toUpperCase() + name.substring(1); 
            String type = field[i].getGenericType().toString(); 
               if (type.equals("class java.lang.String")) {
               m = model.getDeclaredMethod("set" + name, String.class);
     
               if(listobj.get(i - 1) instanceof Double){
                  Double d=(Double) listobj.get(i-1);
                  value=String.valueOf(d);
      
               }else{
                    value =(String)listobj.get(i - 1);
               }
            }
            if (type.equals("class java.lang.Integer")) {
               m = model.getDeclaredMethod("set" + name, Integer.class);
               Double d = (Double) listobj.get(i - 1);
               value = Integer.valueOf(d.intValue());
            }
            if (type.equals("class java.lang.Short")) {
               m = model.getDeclaredMethod("set" + name, Short.class);
               value = (Short) listobj.get(i - 1);
            }
            if (type.equals("class java.lang.Float")) {
               m = model.getDeclaredMethod("set" + name, Float.class);
               value = (Float) listobj.get(i - 1);
            }
            if (type.equals("class java.lang.Double")) {
               m = model.getDeclaredMethod("set" + name, Double.class);
               value = (Double) listobj.get(i - 1);
            }
            if (type.equals("class java.lang.Boolean")) {
               m = model.getDeclaredMethod("set" + name, Boolean.class);
               value = (Boolean) listobj.get(i - 1);
            }
              if (m != null) {
               m.invoke(object, value);
          }
       }

     }
     return object;
   }

posted @ 2017-01-20 15:18  小晓峰  阅读(6139)  评论(1编辑  收藏  举报