insert daoSupport

2129 단어 insert



public static Map po2Map(Object po) {
		Map poMap = new HashMap();
		Map map = new HashMap();
		try {
			map = BeanUtils.describe(po);
		} catch (Exception ex) {
		}
		Object[] keyArray = map.keySet().toArray();
		for (int i = 0; i < keyArray.length; i++) {
			String str = keyArray[i].toString();
			if (str != null && !str.equals("class")) {
				if (map.get(str) != null) {
					poMap.put(str, map.get(str));
				}
			}
		}

		Method[] ms =po.getClass().getMethods();
		for(Method m:ms){
			String name = m.getName();
			
			if(name.startsWith("get")){
				if(m.getAnnotation(NotDbField.class)!=null){
					poMap.remove(getFieldName(name)); 
				} 
			}

		}
		return poMap;
	}

public void insert(String table, Object po) {

		Map poMap =  ReflectionUtil.po2Map(po);

		table = this.dbRouter.getTableName( table);
		this.jdbcDaoSupport.insert(table, poMap);
	}
public void insert(String table, Map fields) {
		String sql = "";

		try {
		 
			Assert.hasText(table, " ");
			Assert.notEmpty(fields, " ");
			table = quoteCol(table);

			Object[] cols = fields.keySet().toArray();
			Object[] values = new Object[cols.length];
			for (int i = 0; i < cols.length; i++) {
				if (fields.get(cols[i]) == null) {
					values[i] = null;
				} else {
					values[i] = fields.get(cols[i]).toString();
				}
				cols[i] = quoteCol(cols[i].toString());
			}

			sql = "INSERT INTO " + table + " ("
					+ StringUtil.implode(", ", cols) + ") VALUES ("
					+ StringUtil.implodeValue(", ", values) + ")";
		
			jdbcTemplate.update(sql, values);
		} catch (Exception e) {
			e.printStackTrace();
			throw new DBRuntimeException(e, sql);
		}
	}

좋은 웹페이지 즐겨찾기