@Override public Map<String, Object> saveMap(Map<String, Object> object, Class clazz) throws Exception { StringBuilder sql = new StringBuilder(); EntityInfo entityInfo = ClassUtils.getEntityInfoByClazz(clazz); sql.append("INSERT INTO "); sql.append(entityInfo.getTableName()); sql.append("("); List<String> columns = new ArrayList<String>(); List<Object> values = new ArrayList<Object>(); Map<String, String> ptcMap = ClassUtils.propToColumnMap.get(entityInfo.getClazzName()); for (Map.Entry<String, Object> entry : object.entrySet()) { columns.add(ptcMap.get(entry.getKey())); values.add(entry.getValue()); } sql.append(StringUtils.join(columns, ",")); sql.append(") VALUES("); String[] params = new String[values.size()]; Arrays.fill(params, "?"); sql.append(StringUtils.join(params, ",")); sql.append(")"); if (entityInfo.getStrategy().equals(GenerationType.IDENTITY)) { Long id = addReutrnId(sql.toString(), values); if (id != null) { object.put(entityInfo.getPkName(), id); } } else { add(sql.toString(), values); } return object; }
@Override public int update(Object object) throws Exception { EntityInfo entityInfo = ClassUtils.getEntityInfoByObject(object); Map<String, Object> valueMap = ClassUtils.describe(object); SqlHelper sqlHelper = getUpdateSql(valueMap, entityInfo); return update(sqlHelper); }
@Override public int updateMap(Map<String, Object> object, Class clazz) throws Exception { EntityInfo entityInfo = ClassUtils.getEntityInfoByClazz(clazz); if (entityInfo == null) { throw new DaoException("无效的实体类class"); } SqlHelper sqlHelper = getUpdateSql(object, entityInfo); return update(sqlHelper); }
@Override public int del(Object id, Class clazz) throws Exception { EntityInfo entityInfo = ClassUtils.getEntityInfoByClazz(clazz); if (entityInfo == null) { throw new DaoException("无效的实体类class"); } SqlHelper sqlHelper = SqlHelper.getDeleteHelper(clazz); sqlHelper.append(" WHERE ").append(entityInfo.getPkClumnName()).append("=?"); sqlHelper.setParameters(id); return del(sqlHelper, clazz); }
@Override public Map<String, Object> findMapById(Object id, Class clazz) throws Exception { EntityInfo entityInfo = ClassUtils.getEntityInfoByClazz(clazz); if (entityInfo == null) { throw new DaoException("无效的实体类class"); } SqlHelper sqlHelper = SqlHelper.getSelectHelper(clazz); sqlHelper.append(" AND ").append(entityInfo.getPkClumnName()).append("=?"); sqlHelper.setParameters(id); return searchMapForOne(sqlHelper); }
@Override public void batchDel(List<Object> ids, Class clazz) throws Exception { EntityInfo entityInfo = ClassUtils.getEntityInfoByClazz(clazz); if (entityInfo == null) { throw new DaoException("无效的实体类class"); } Object[] objects = new Object[ids.size()]; Arrays.fill(objects, "?"); SqlHelper sqlHelper = SqlHelper.getDeleteHelper(clazz); sqlHelper .append(" WHERE ") .append(entityInfo.getPkClumnName()) .append(" IN (") .append(StringUtils.join(objects, ",")) .append(")"); update(sqlHelper.getSql(), ids); }