java利用映射表名稱反射建立實體類並賦屬性值

來源:互聯網
上載者:User

標籤:abstract   metadata   abs   mod   short   exce   declare   this   throws   

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

java利用映射表名稱反射建立實體類並賦屬性值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.