• 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;
       }

  • 相关阅读:
    直拍反手拉球引拍位置及发力技巧
    话说多球 --  乒在民间
    直板横打不稳定,总是出界的可能原因 -- 乒在民间
    【hihocoder 1424】 Asa's Chess Problem(有源汇上下界网络流)
    【HDU 6036】Division Game (NTT+数学)
    【hdu 4658】Integer Partition (无序分拆数、五边形数定理)
    【hdu 5628】Clarke and math (Dirichlet卷积)
    【hdu6188】Duizi and Shunzi(贪心)
    【hdu6186】CS Course(前缀后缀异或)
    【hdu6185】Covering(骨牌覆盖)
  • 原文地址:https://www.cnblogs.com/xiaofengyuan/p/5756288.html
Copyright © 2020-2023  润新知