private Sql makeSQL(T t, String[] fields, Object[] values) throws SqlCreateException { Sql sql = new Sql(); Class<?> clazz = t.getClass(); String table = ORMConfigBeanUtil.getTable(clazz, false); ReflectUtil ru = new ReflectUtil(t); String[] columns = ORMConfigBeanUtil.getColumns(clazz, fields); String idColumn = ORMConfigBeanUtil.getIdColumn(clazz); String idField = ORMConfigBeanUtil.getIdField(clazz); Method idGetter = ru.getGetter(idField); if (idGetter == null) throw new SqlCreateException("can not find id getter."); Object idValue = null; try { idValue = idGetter.invoke(t); } catch (Exception e) { throw new SqlCreateException(idGetter + " invoke exception " + e.toString(), e); } StringBuilder sb = new StringBuilder(); for (int i = 0; i < columns.length; ++i) { String column = columns[i]; if (sb.length() > 0) sb.append(", "); // sb.append(column).append(" = '").append(values[i]).append("'"); sb.append(column).append(" = ?"); sql.args.add(values[i]); } // String condition = new StringBuilder().append(idColumn).append(" = // ").append("'").append(idValue).append("'").toString(); String condition = new StringBuilder().append(idColumn).append(" = ? ").toString(); sql.args.add(idValue); sql.sql = String.format("UPDATE %s SET %s WHERE %s ;", table, sb.toString(), condition); return sql; }
private String makeSQL(T t, String[] fields, String[] values) throws SqlCreateException { Class<?> clazz = t.getClass(); String table = ORMConfigBeanUtil.getTable(clazz); ReflectUtil ru = new ReflectUtil(t); String[] columns = ORMConfigBeanUtil.getColumns(clazz, fields); String idColumn = ORMConfigBeanUtil.getIdColumn(clazz); String idField = ORMConfigBeanUtil.getIdField(clazz); Method idGetter = ru.getGetter(idField); if (idGetter == null) throw new SqlCreateException("can not find id getter."); StringBuilder condition = new StringBuilder(); try { condition.append(idColumn).append(" = '").append(idGetter.invoke(t)).append("'"); } catch (Exception e) { throw new SqlCreateException(idGetter + " invoke exception " + e.toString()); } StringBuilder sb = new StringBuilder(); for (int i = 0; i < columns.length; ++i) { String column = columns[i]; if (sb.length() > 0) sb.append(", "); sb.append(column).append(" = '"); sb.append(values[i]).append("'"); } return String.format("UPDATE %s SET %s WHERE %s ;", table, sb.toString(), condition); }
private Sql makeSQL(T t, String condition) { Sql sql = new Sql(); Class<?> clazz = t.getClass(); String table = ORMConfigBeanUtil.getTable(clazz, false); sql.sql = String.format("UPDATE %s %s ;", table, condition); return sql; }
public DAO setClass(Class<?> clazz) { this.clazz = clazz; if (Map.class.isAssignableFrom(clazz)) { if (this.map != null) { selectAllColumn = ORMConfigBeanUtil.getSelectAllColumn(map); table = (String) map.get("table"); } } else { this.table = ORMConfigBeanUtil.getTable(clazz); selectAllColumn = ORMConfigBeanUtil.getSelectAllColumn(clazz); } if (selectAllColumn == null || selectAllColumn.trim().length() == 0) selectAllColumn = "*"; return this; }
public void init(Object t, List<Field> fields) throws DAOException { this.t = t; this.fields = fields; this.ru = new ReflectUtil(this.t); this.table = ORMConfigBeanUtil.getTable(this.t.getClass(), true); // 主类的ID属性名 this.idField = ORMConfigBeanUtil.getIdField(this.t.getClass()); this.idSetter = ru.getSetter(idField); if (this.idSetter == null) throw new DAOException("can not get idSetter.", null); this.idGetter = ru.getGetter(idField); if (this.idGetter == null) throw new DAOException("can not get idGetter.", null); this.idColumn = ORMConfigBeanUtil.getIdColumn(this.t.getClass()); try { Object _idVal = idGetter.invoke(this.t); this.idVal = _idVal == null ? null : String.valueOf(_idVal); } catch (Exception e) { throw new DAOException(idGetter + " invoke exception ", e); } }
private void init(Class<?> clazz, String dsName) { if (dsName == null) dsName = DAOConfigConstant.MYDBINFO; this.clazz = clazz; this.dsName = dsName; this.ds = DataSourceWrapCache.get(dsName); dbType = DBInfoConfigBeanCache.get(dsName).getDataBaseType(); if (Map.class.isAssignableFrom(clazz)) { if (this.map != null) { selectAllColumn = ORMConfigBeanUtil.getSelectAllColumn(map); table = (String) map.get("table"); } } else { this.table = ORMConfigBeanUtil.getTable(clazz); selectAllColumn = ORMConfigBeanUtil.getSelectAllColumn(clazz); } if (selectAllColumn == null || selectAllColumn.trim().length() == 0) selectAllColumn = "*"; this.buffer.put("orderField", ORMConfigBeanUtil.getIdField(clazz)); this.buffer.put("orderType", OrderType.DESC_ORDER); }
/** * 多对多级联查询 1.当主对象没有包含任何一个关联对象时,默认查询所有与之关联的对象 2.当主对象中包含了关联对象时(含有其id值),则只查询这些关联的对象 * * @param <T> * @throws InvocationTargetException * @throws IllegalAccessException * @throws IllegalArgumentException */ public void select() throws DAOException { if (this.fields == null || this.fields.size() == 0) return; // select %s from {tarTable} where {referencedColumn} in (select {to} from {relTable} where // {from} = {idVal}) String format = "SELECT %s FROM %s WHERE %s IN (SELECT %s FROM %s WHERE %s = ?) "; if (idVal == null || "0".equals(idVal) || "".equals(idVal)) return; for (Field f : fields) { Method tarGetter = ru.getGetter(f.getName()); if (tarGetter == null) continue; ManyToMany ann = tarGetter.getAnnotation(ManyToMany.class); if (ann == null) { ann = f.getAnnotation(ManyToMany.class); if (ann == null) continue; } JoinTable join = tarGetter.getAnnotation(JoinTable.class); if (join == null) { join = f.getAnnotation(JoinTable.class); if (join == null) continue; } JoinColumn[] froms = join.joinColumns(); if (froms == null || froms.length == 0) continue; JoinColumn[] tos = join.inverseJoinColumns(); if (tos == null || tos.length == 0) continue; // 多对多关系目标Class Class<?> tarClass = ann.targetEntity(); if (void.class.isAssignableFrom(tarClass)) { tarClass = ClassUtil.getGenericType(f); } String tarTable = ORMConfigBeanUtil.getTable(tarClass, true); // 目标类对应的数据库表Id字段 String referencedColumn = tos[0].referencedColumnName(); if (referencedColumn == null || referencedColumn.trim().length() == 0) referencedColumn = ORMConfigBeanUtil.getIdColumn(tarClass); // 目标类在第三方关系表中的字段名 String to = tos[0].name(); // 第三方关系表 String relTable = join.name(); // 主类在第三方关系表中的字段名 String from = froms[0].name(); try { List<?> tarList = null; tarList = (List<?>) tarGetter.invoke(t); if (tarList != null && tarList.size() > 0) { for (int i = 0; i < tarList.size(); i++) { Object tarObj = tarList.get(i); ReflectUtil tarRu = new ReflectUtil(tarObj); String tarIdField = ORMConfigBeanUtil.getIdField(tarClass); Method tarIdGetter = tarRu.getGetter(tarIdField); Object tarIdValObj = tarIdGetter.invoke(tarObj); if (tarIdValObj == null) continue; String tarIdVal = String.valueOf(tarIdValObj); // 查询 select %s from {tarTable} where {tarIdColumn} = {tarIdVal} tarObj = DAOFactory.getSelectDAO(dsName) .selectOne(tarClass, new String[] {tarIdField}, new String[] {tarIdVal}); } } else { String sql = String.format( format, ORMConfigBeanUtil.getSelectAllColumn(tarClass), tarTable, referencedColumn, to, relTable, from); // 从数据库中取出与当前主对象id关联的所有目标对象, tarList = DAOFactory.getSelectDAO(dsName).selectBySQL(tarClass, sql, idVal); } // 并注入到当前主对象的属性中 Method tarSetter = ru.getSetter(f.getName()); tarSetter.invoke(t, tarList); } catch (Exception e) { e.printStackTrace(); throw new DAOException("", e); } } }
private Sql makeSQL(T t) throws SqlCreateException { Sql sql = new Sql(); Class<?> clazz = t.getClass(); String table; String[] columns; String[] fields; Object[] values = null; String idColumn; String idField; Object idValue = null; HashMap<String, Object> map = null; if (Map.class.isAssignableFrom(clazz)) { map = (HashMap) t; table = (String) map.get("table"); idColumn = (String) map.get("idColumn"); idField = idColumn; idValue = map.get("idValue"); columns = (String[]) map.get("columns"); fields = columns; values = (Object[]) map.get("values"); } else { table = ORMConfigBeanUtil.getTable(clazz, false); columns = ORMConfigBeanUtil.getColumns(clazz); fields = ORMConfigBeanUtil.getFields(clazz); idColumn = ORMConfigBeanUtil.getIdColumn(clazz); idField = ORMConfigBeanUtil.getIdField(clazz); } StringBuilder valuesSb = new StringBuilder(); ReflectUtil ru = new ReflectUtil(t); try { if (map == null) { Method idGetter = ru.getGetter(idField); if (idGetter == null) throw new SqlCreateException("can not find id getter"); idValue = idGetter.invoke(t); } for (int i = 0; i < columns.length; i++) { String column = columns[i]; String field = fields[i]; Object value = null; // id 字段不允许 if (idColumn != null && idColumn.equalsIgnoreCase(column)) continue; if (map != null && values != null) { value = values[i]; } else { Method getter = ru.getGetter(field); if (getter == null) continue; Object _value = getter.invoke(t); if (_value == null) continue; if (ClassUtil.isPojo(_value.getClass())) { Field f = ru.getField(field); OneToOne oneAnn = getter.getAnnotation(OneToOne.class); if (oneAnn == null) oneAnn = f.getAnnotation(OneToOne.class); ManyToOne manyToOneAnn = null; if (oneAnn == null) { manyToOneAnn = getter.getAnnotation(ManyToOne.class); if (manyToOneAnn == null) manyToOneAnn = f.getAnnotation(ManyToOne.class); } if (oneAnn != null || manyToOneAnn != null) { JoinColumn joinColAnn = getter.getAnnotation(JoinColumn.class); if (joinColAnn == null) joinColAnn = f.getAnnotation(JoinColumn.class); if (joinColAnn != null && joinColAnn.referencedColumnName().trim().length() > 0) { String refCol = joinColAnn.referencedColumnName(); String refField = ORMConfigBeanUtil.getField(_value.getClass(), refCol); ReflectUtil tarRu = new ReflectUtil(_value); Method tarFKGetter = tarRu.getGetter(refField); value = tarFKGetter.invoke(_value); } else { ReflectUtil tarRu = new ReflectUtil(_value); String tarFKField = ORMConfigBeanUtil.getIdField(_value.getClass()); if (tarFKField != null) { Method tarFKGetter = tarRu.getGetter(tarFKField); value = tarFKGetter.invoke(_value); } } } if (value == null) continue; } else value = _value; } if (valuesSb.length() > 0) valuesSb.append(","); // valuesSb.append(column).append(" = '").append(value).append("'"); valuesSb.append(column).append(" = ? "); sql.args.add(value); } } catch (Exception e) { throw new SqlCreateException("" + e.toString(), e); } // String condition = new StringBuilder().append(idColumn).append(" = // ").append("'").append(idValue).append("'").toString(); String condition = new StringBuilder().append(idColumn).append(" = ? ").toString(); sql.args.add(idValue); sql.sql = String.format("UPDATE %s SET %s WHERE %s ;", table, valuesSb, condition); return sql; }
private Sql makeSQL(T t, String[] fields) throws SqlCreateException { Sql sql = new Sql(); Class<?> clazz = t.getClass(); // if fields is empty if (fields == null || fields.length == 0) { fields = ORMConfigBeanUtil.getFields(clazz); } String table = ORMConfigBeanUtil.getTable(clazz, false); StringBuilder values = new StringBuilder(); ReflectUtil ru = new ReflectUtil(t); String[] columns = ORMConfigBeanUtil.getColumns(clazz, fields); String idColumn = ORMConfigBeanUtil.getIdColumn(clazz); String idField = ORMConfigBeanUtil.getIdField(clazz); Method idGetter = ru.getGetter(idField); if (idGetter == null) throw new SqlCreateException("can not find id getter."); Object idValue = null; try { idValue = idGetter.invoke(t); } catch (Exception e) { throw new SqlCreateException(idGetter + " invoke exception " + e.toString(), e); } for (int i = 0; i < fields.length; i++) { String field = fields[i]; String column = columns[i]; Method getter = ru.getGetter(field); if (getter == null) continue; try { Object _value = getter.invoke(t); if (_value == null) continue; Object value = null; if (ClassUtil.isPojo(_value.getClass())) { Field f = ru.getField(field); OneToOne oneAnn = getter.getAnnotation(OneToOne.class); if (oneAnn == null) oneAnn = f.getAnnotation(OneToOne.class); ManyToOne manyToOneAnn = null; if (oneAnn == null) { manyToOneAnn = getter.getAnnotation(ManyToOne.class); if (manyToOneAnn == null) manyToOneAnn = f.getAnnotation(ManyToOne.class); } if (oneAnn != null || manyToOneAnn != null) { JoinColumn joinColAnn = getter.getAnnotation(JoinColumn.class); if (joinColAnn == null) joinColAnn = f.getAnnotation(JoinColumn.class); if (joinColAnn != null && joinColAnn.referencedColumnName().trim().length() > 0) { String refCol = joinColAnn.referencedColumnName(); String refField = ORMConfigBeanUtil.getField(_value.getClass(), refCol); ReflectUtil tarRu = new ReflectUtil(_value); Method tarFKGetter = tarRu.getGetter(refField); value = tarFKGetter.invoke(_value); } else { ReflectUtil tarRu = new ReflectUtil(_value); String tarFKField = ORMConfigBeanUtil.getIdField(_value.getClass()); if (tarFKField != null) { Method tarFKGetter = tarRu.getGetter(tarFKField); value = tarFKGetter.invoke(_value); } } } if (value == null) continue; } else { value = _value; } if (values.length() > 0) values.append(", "); // values.append(column).append(" = '").append(value).append("'"); values.append(column).append(" = ? "); sql.args.add(value); } catch (Exception e) { throw new SqlCreateException(idGetter + " invoke exception " + e.toString(), e); } } // String condition = new StringBuilder().append(idColumn).append(" = // ").append("'").append(idValue).append("'").toString(); String condition = new StringBuilder().append(idColumn).append(" = ? ").toString(); sql.args.add(idValue); sql.sql = String.format("UPDATE %s SET %s WHERE %s ;", table, values, condition); return sql; }
private String makeSQL(T t, String condition) { Class<?> clazz = t.getClass(); String table = ORMConfigBeanUtil.getTable(clazz); return String.format("UPDATE %s %s ;", table, condition); }
private String makeSQL(T t, String[] fields) throws SqlCreateException { Class<?> clazz = t.getClass(); String table = ORMConfigBeanUtil.getTable(clazz); StringBuilder condition = new StringBuilder(); StringBuilder values = new StringBuilder(); ReflectUtil ru = new ReflectUtil(t); String[] columns = ORMConfigBeanUtil.getColumns(clazz, fields); String idColumn = ORMConfigBeanUtil.getIdColumn(clazz); String idField = ORMConfigBeanUtil.getIdField(clazz); Method idGetter = ru.getGetter(idField); if (idGetter == null) throw new SqlCreateException("can not find id getter."); try { condition.append(idColumn).append(" = '").append(idGetter.invoke(t)).append("'"); } catch (Exception e) { throw new SqlCreateException(idGetter + " invoke exception " + e.toString()); } for (int i = 0; i < fields.length; i++) { String field = fields[i]; String column = columns[i]; Method getter = ru.getGetter(field); if (getter == null) continue; try { Object _value = getter.invoke(t); if (_value == null) continue; Object value = null; if (ClassUtil.isPojo(_value.getClass())) { Field f = ru.getField(field); OneToOne oneAnn = getter.getAnnotation(OneToOne.class); if (oneAnn == null) oneAnn = f.getAnnotation(OneToOne.class); ManyToOne manyToOneAnn = null; if (oneAnn == null) { manyToOneAnn = getter.getAnnotation(ManyToOne.class); if (manyToOneAnn == null) manyToOneAnn = f.getAnnotation(ManyToOne.class); } if (oneAnn != null || manyToOneAnn != null) { ReflectUtil tarRu = new ReflectUtil(_value); String tarFKField = ORMConfigBeanUtil.getIdField(_value.getClass()); Method tarFKGetter = tarRu.getGetter(tarFKField); value = tarFKGetter.invoke(_value); } } if (value == null) value = _value; if (values.length() > 0) values.append(", "); values.append(column).append(" = '").append(value).append("'"); } catch (Exception e) { throw new SqlCreateException(idGetter + " invoke exception " + e.toString()); } } return String.format("UPDATE %s SET %s WHERE %s ;", table, values, condition); }
/** 多对一级联查询 1.获取当前idVal,然后作为条件查询出其外键值,接着通过其外键值查出主对象数据,注入到当前 */ public void select() { if (this.fields == null || this.fields.size() == 0) return; if (idVal == null || "0".equals(idVal) || "".equals(idVal)) { return; } // else if (DAOFactory.getSelectDAO(dsName).selectOne(t, this.idField) == null) { // // 检查一下当前对象的ID是否存在于数据库 // return; // } for (Field f : fields) { Method tarGetter = ru.getGetter(f.getName()); if (tarGetter == null) continue; String fk = null; OneToOne ann = tarGetter.getAnnotation(OneToOne.class); if (ann == null) ann = f.getAnnotation(OneToOne.class); if (ann == null) { ManyToOne moAn = tarGetter.getAnnotation(ManyToOne.class); if (moAn == null) moAn = f.getAnnotation(ManyToOne.class); if (moAn == null) continue; } String refCol = null; JoinColumn joinCol = f.getAnnotation(JoinColumn.class); if (joinCol == null) joinCol = tarGetter.getAnnotation(JoinColumn.class); if (joinCol == null) fk = f.getName() + "_id"; else { fk = joinCol.name(); refCol = joinCol.referencedColumnName(); } Class<?> tarClass = f.getType(); if (refCol == null || refCol.trim().length() == 0) refCol = ORMConfigBeanUtil.getIdColumn(tarClass); String refField = ORMConfigBeanUtil.getField(tarClass, refCol); try { Object _tarObj = tarGetter.invoke(t); Object tarObj = null; boolean flag = false; if (_tarObj != null) { Method refFieldGetter = new ReflectUtil(_tarObj).getGetter(refField); if (refFieldGetter != null && refFieldGetter.invoke(_tarObj) != null) tarObj = DAOFactory.getSelectDAO(dsName).selectOne(_tarObj, refField); else flag = true; } else flag = true; if (flag) { // select * from {tarTable} where {referencedColumn} = (select {fk} from {table} where // {idColumn} = {idVal}) String format = "select %s from %s where %s = (select %s from %s where %s = %s )"; String tarTable = ORMConfigBeanUtil.getTable(tarClass, true); String sql = String.format( format, ORMConfigBeanUtil.getSelectAllColumn(tarClass), tarTable, refCol, fk, table, idColumn, idVal); List<?> tarList = DAOFactory.getSelectDAO(dsName).selectBySQL(tarClass, sql); if (tarList == null || tarList.size() == 0) continue; tarObj = tarList.get(0); } if (tarObj == null) continue; Method tarSetter = ru.getSetter(f.getName()); tarSetter.invoke(t, tarObj); } catch (Exception e) { throw new DAOException("", e); } } }