Map代替VO小试

import java.util.HashMap;

/**
 * 动态VO 使用Map存储列名(key) 、值(value)
 * 
 * @author yuanhongbo
 * 
 */
public class DynamicVO extends HashMap<Object, Object> {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -7462753289250392092L;

	/**
	 * 表名
	 */
	private String tableName;

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

}

import java.util.Iterator;

public class DynamicVOUtil {
	/**生成insert语句
	 * @param vo
	 * @return
	 */
	public static String getInsertSQL(DynamicVO vo) {
		StringBuffer sql = new StringBuffer();
		sql.append("insert into ").append(vo.getTableName()).append('(');

		StringBuffer sqlValues = new StringBuffer();
		sqlValues.append(" values(");
		for (Iterator<Object> i = vo.keySet().iterator(); i.hasNext();) {
			String column = (String) i.next();
			sql.append(column).append(',');
			sqlValues.append('\'').append(vo.get(column)).append('\'')
					.append(',');
		}
		sql.append("last_update_date)");
		sqlValues.append("sysdate)");

		return sql.append(sqlValues).toString();
	}
}


public class TestDynamicVO {
	public static void main(String[] args) {
		DynamicVO vo = new DynamicVO();
		vo.setTableName("t_table_name");
		vo.put("dummy", "Y");
		String sql = DynamicVOUtil.getInsertSQL(vo);
		System.out.println(sql);
	}
}

//列的类型处理待完善...

你可能感兴趣的:(map)