Esempio n. 1
0
  /**
   * 对象封装
   *
   * @param cursor
   * @param clazz
   * @return
   */
  private <T> T cursorToBean(Cursor cursor, Class<T> clazz) {
    EntityInfo entity = EntityInfo.build(clazz);
    Set<String> keys = entity.getColumns().keySet();
    T obj = null;
    try {
      obj = clazz.newInstance();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    } catch (InstantiationException e) {
      e.printStackTrace();
    }

    for (Iterator<String> iterator = keys.iterator(); iterator.hasNext(); ) {
      String key = iterator.next();
      String column = entity.getColumns().get(key);
      Field field = BeanUtil.getDeclaredField(clazz, key);
      if (field.getType().equals(Integer.class) || field.getType().equals(int.class)) {
        BeanUtil.setProperty(obj, key, cursor.getInt(cursor.getColumnIndex(column)));
      } else if (field.getType().equals(Long.class) || field.getType().equals(long.class)) {
        BeanUtil.setProperty(obj, key, cursor.getLong(cursor.getColumnIndex(column)));
      } else if (field.getType().equals(Double.class) || field.getType().equals(double.class)) {
        BeanUtil.setProperty(obj, key, cursor.getDouble(cursor.getColumnIndex(column)));
      } else if (field.getType().equals(Float.class) || field.getType().equals(float.class)) {
        BeanUtil.setProperty(obj, key, cursor.getFloat(cursor.getColumnIndex(column)));
      } else if (field.getType().equals(String.class)) {
        BeanUtil.setProperty(obj, key, cursor.getString(cursor.getColumnIndex(column)));
      } else if (field.getType().equals(Date.class)) {
        try {
          BeanUtil.setProperty(obj, key, new Date(cursor.getLong(cursor.getColumnIndex(column))));
        } catch (Exception e) {
        }
      } else if (field.getType().equals(Boolean.class) || field.getType().equals(boolean.class)) {
        BeanUtil.setProperty(
            obj, key, cursor.getInt(cursor.getColumnIndex(column)) == 0 ? false : true);
      }
    }

    return obj;
  }
Esempio n. 2
0
 private static String getCreatTableSQL(Class<?> clazz) {
   EntityInfo info = EntityInfo.build(clazz);
   StringBuffer sql = new StringBuffer();
   sql.append("CREATE TABLE IF NOT EXISTS ");
   sql.append(info.getTable());
   sql.append(" ( ");
   Map<String, String> propertys = info.getColumns();
   Set<String> keys = propertys.keySet();
   for (Iterator<String> iterator = keys.iterator(); iterator.hasNext(); ) {
     String key = iterator.next();
     sql.append(propertys.get(key));
     Class<?> dataType = BeanUtil.getDeclaredField(clazz, key).getType();
     if (dataType == int.class
         || dataType == Integer.class
         || dataType == long.class
         || dataType == Long.class) {
       sql.append(" INTEGER");
     } else if (dataType == float.class
         || dataType == Float.class
         || dataType == double.class
         || dataType == Double.class) {
       sql.append(" REAL");
     } else if (dataType == boolean.class || dataType == Boolean.class) {
       sql.append(" NUMERIC");
     }
     if (key.equals(info.pk)) {
       sql.append(" PRIMARY KEY");
       if (info.pkAuto) {
         sql.append(" AUTOINCREMENT");
       }
     }
     sql.append(",");
   }
   sql.deleteCharAt(sql.length() - 1);
   sql.append(" )");
   return sql.toString();
 }