日期:2014-05-16  浏览次数:20442 次

浅谈Android数据库CRUD操作的封装与实现(二)【Android Hibernate,永别了SQL!】

用了一整天的时间把昨天遗留下来的问题解决了。

既然是要做一个类似Hibernate的工具,最主要的就是尽量减少SQL语句的编写。我采用反向强力解析实体类的办法,实现了自动构建SQL语句的功能。

现在有三个核心类:

?

?

EntityDao是一个泛型接口,定义了所有应该实现的方法。SimpleDao是抽象类,对接口功能进行实现,真正实体的DAO只要继承SimpleDao就可以拥有所有功能。无需任何额外实现、无需任何SQL语句!

首先,看看我的BeanTools。它主要负责对实体进行强力解析,废话不多说,上代码:

?

/**
 * Bean工具类
 * @author EwinLive
 *
 */
public abstract class BeanTools {
	/**
	 * 获取第一个泛型类
	 */
	public static Class<?> getGenericClass(Class<?> clazz) {
		return getGenericClass(clazz, 0);
	}

	/**
	 * 获取泛型类
	 */
	public static Class<?> getGenericClass(Class<?> clazz, int index) throws IndexOutOfBoundsException {
		Type genType = clazz.getGenericSuperclass();
		if (!(genType instanceof ParameterizedType)) {
			return Object.class;
		}
		Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
		if (index >= params.length || index < 0) {
			throw new IndexOutOfBoundsException("Index: " + index + ", Size of Parameterized Type: " + params.length);
		}
		return (Class<?>) params[index];
	}

	/**
	 * 直接设置对象属性值, 无视private/protected修饰符, 不经过setter函数.
	 */
	public static void setFieldValue(final Object object, final String fieldName, final Object value) {
		Field field = getDeclaredField(object, fieldName);

		if (field == null) {
			throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + object + "]");
		}

		makeAccessible(field);

		try {
			field.set(object, value);
		} catch (IllegalAccessException e) {
			//logger.error("不可能抛出的异常:{}", e.getMessage());
		}
	}

	/**
	 * 强行设置Field可访问.
	 */
	protected static void makeAccessible(final Field field) {
		if (!Modifier.isPublic(field.getModifiers()) || !Modifier.isPublic(field.getDeclaringClass().getModifiers())) {
			field.setAccessible(true);
		}
	}

	/**
	 * 循环向上转型, 获取对象的DeclaredField.
	 * 
	 * 如向上转型到Object仍无法找到, 返回null.
	 */
	protected static Field getDeclaredField(final Object object, final String fieldName) {
		//Assert.notNull(object, "object不能为空");
		//Assert.hasText(fieldName, "fieldName");
		for (Class<?> superClass = object.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()) {
			try {
				return superClass.getDeclaredField(fieldName);
			} catch (NoSuchFieldException e) {
				// Field不在当前类定义,继续向上转型
			}
		}
		return null;
	}

	/**
	 * 转换字符串到相应类型.
	 * 
	 * @param value 待转换的字符串
	 * @param toType 转换目标类型
	 */
	public static Object convertStringToObject(String value, Class<?> toType) {
		if (StringTools.isNotEmpty(value)) {
			return ConvertUtils.convert(value, toType);
		} else {
			return null;
		}
	}

	/**
	 * 强行获取私有属性的值
	 */
	public static Object getPrivateProperty(Object object, String propertyName) throws IllegalAccessException, NoSuchFieldException {
		//Assert.notNull(object);
		//Assert.hasText(propertyName);
		Field field = object.getClass().getDeclaredField(propertyName);
		field.setAccessible(true);
		return field.get(object);
	}

	/**
	 * 强行设置私有属性的值
	 */
	public static void setPrivateProperty(Object object, String propertyName, Object newValue) throws IllegalAccessException, NoSuchFieldException {
		//Assert.notNull(object);
		//Assert.hasText(propertyName);
		Field field = object.getClass().getDeclaredField(propertyName);
		field.setAccessible(true);
		field.set(object, newValue);
	}
	
	/**
	 * 获取所有字段
	 * @param entityClass 实体的类型
	 * @return data 
	 * 			返回包含两个数组的HashMap,可参考以下使用方法:
	 * 			String[] fieldName = (String[]) data.get("fieldName");
	 * 			Class<?>[] fieldType = (Class<?>[]) data.get("fieldType");
	 */
	public static HashMap<Object, Object> getAllFiled(Class<?> entityClass){
		HashMap<Object, Object> data = new HashMap<Object, Object>();
		
		Field[]  fields = entityClass.getDeclaredFields();
		String[] fieldName = new