1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
public <T, ID> T retrieveByPK(BaseEntity baseEntity, ID pk) throws Exception { return retrieveByPK(baseEntity, pk, null); }
public <T, ID> T retrieveByPK(BaseEntity baseEntity, ID pk, String[] selectedFields) throws Exception { if (pk == null) { throw new IllegalArgumentException("pk is null"); } T result = null;
Map<String, Object> unionKeyMap = new HashMap<>(); unionKeyMap.put(BeanProcessorHelper.getPkColumn(baseEntity.getClass(), baseEntity.getFullName()), pk); result = retrieveByUnionKeyMap(baseEntity, unionKeyMap, selectedFields);
return result; }
public <T> T retrieveByUnionKeyMap(BaseEntity baseEntity, Map<String, Object> unionKeyMap) throws Exception { return retrieveByUnionKeyMap(baseEntity, unionKeyMap, null); }
public <T> T retrieveByUnionKeyMap(BaseEntity baseEntity, Map<String, Object> unionKeyMap, String[] selectedFields) throws Exception { if (unionKeyMap == null) { throw new IllegalArgumentException("unionKeyMap is null"); } T result = null; boolean ormAttributeSign = BeanDataOperateHelper.initOrmAttributeSign(baseEntity); Map<String, Object> resultMap = null; SQLParameter param = new SQLParameter();
List<String> conditionList = new ArrayList<>(); for(Map.Entry<String, Object> entry : unionKeyMap.entrySet()){ conditionList.add(entry.getKey()); param.addParam(entry.getValue()); }
Class clz = baseEntity.getClass(); String fullName = baseEntity.getFullName(); Map<String, ORMColumnInfo> types = findColumnInfo(clz, fullName); Entity entity = MetaEntityHelper.getMetaEntity(fullName); List<Property> elasticAttributes = entity.getElasticAttributes();
for (Property elasticProperty : elasticAttributes) { types.remove(elasticProperty.columnName()); } StringBuffer columnsForQuery = new StringBuffer(); for (String columnName : types.keySet()) { columnsForQuery.append(columnName).append(","); } columnsForQuery.delete(columnsForQuery.length() - 1, columnsForQuery.length());
List<OneToOneType> parallelTypes = BizMetaHelper.getOneToOneField(baseEntity, null, null); List<OneToOneType> characteristicTypes = BeanProcessorHelper.getCharacteristicOneToOneField(fullName); StringBuffer condition = new StringBuffer(); for(String s : conditionList){ condition.append(s + "=? and "); } condition.delete(condition.length() - 5, condition.length());
if (CollectionUtils.isEmpty(parallelTypes) && CollectionUtils.isEmpty(characteristicTypes)) { String pkName = BeanProcessorHelper.getPkColumn(clz, fullName); boolean hasPKField = false; StringBuffer mainSQL = new StringBuffer(); String tableName = BeanProcessorHelper.getTableName(clz, fullName); if (selectedFields == null) { mainSQL.append("SELECT ").append(columnsForQuery).append(" FROM ").append(tableName); } else { selectedFields = transPropertyNameToFieldNames(clz, fullName, selectedFields); String[] columns = getValidNames(fullName, clz, types, baseEntity, selectedFields); mainSQL.append("SELECT "); for (int i = 0; i < columns.length; i++) { if (columns[i] != null) { mainSQL.append(columns[i]).append(","); if (columns[i].equalsIgnoreCase(pkName)) { hasPKField = true; } } } if (!hasPKField) { mainSQL.append(pkName).append(","); } mainSQL.setLength(mainSQL.length() - 1); mainSQL.append(" FROM ").append(tableName); } mainSQL.append(" WHERE ").append(condition); BaseProcessor processor = new BeanListProcessor(baseEntity); List<T> results = getJdbcSession().executeQuery(mainSQL.toString(), param, processor); if (results.size() >= 1) { result = results.get(0); } } else { if (logger.isDebugEnabled()) { logger.debug("query for oneToOne .. {} ", clz.getClasses()); } Field[] fields = baseEntity.getClass().getDeclaredFields(); String[] propNames = new String[fields.length]; for (int i = 0; i < fields.length; i++) { propNames[i] = fields[i].getName(); } String pkName = BeanProcessorHelper.getPkColumn(clz, fullName); boolean hasPKField = false; StringBuffer mainSQL = new StringBuffer(); String tableName = BeanProcessorHelper.getTableName(clz, fullName); StringBuilder mapSQL = new StringBuilder(); mapSQL.append("SELECT * FROM ").append(tableName).append(" WHERE ").append(condition); List<Map> mapList = getJdbcSession().executeQuery(mapSQL.toString(), param, new MapListProcessor()); if (mapList.size() >= 1) { resultMap = mapList.get(0); } else { logger.debug("查询结果为空!"); return null; } if (selectedFields == null) { mainSQL.append("SELECT ").append(columnsForQuery).append(" FROM ").append(tableName).append(" WHERE ").append(condition); List<T> results = getJdbcSession().executeQuery(mainSQL.toString(), param, new BeanListProcessor(baseEntity)); if (results.size() >= 1) { result = results.get(0); } for (OneToOneType oneToOneType : characteristicTypes) { String childSQL = buildSQLWithoutSelectFields(oneToOneType, clz, fullName, resultMap); if (StringUtils.isNotEmpty(childSQL)) { characteristicEntityHandler(baseEntity, childSQL, fullName, result, ormAttributeSign); } } for (OneToOneType oneToOneType : parallelTypes) { String childTable = BeanProcessorHelper.getTableName(oneToOneType.getTargetEntity(), oneToOneType.getTargetFullName()); String childSQL = buildSQLWithoutSelectFields(oneToOneType, clz, fullName, resultMap); if (StringUtils.isNotEmpty(childSQL)) { parallelEntityHandler(baseEntity, childSQL, childTable, result, ormAttributeSign); } }
} else { String[] filterFileds = transPropertyNameToFieldNames(clz, fullName, selectedFields); Set<String> filerdFieldSet = new HashSet<>(); filerdFieldSet.addAll(Arrays.asList(filterFileds)); StringBuilder columnBuilder = new StringBuilder(); for (int i = 0; i < filterFileds.length; i++) { if (filterFileds[i] != null) { columnBuilder.append(filterFileds[i]).append(","); if (filterFileds[i].equalsIgnoreCase(pkName)) { hasPKField = true; } } } columnBuilder.append("pubts").append(","); columnBuilder.append("dr"); if (!hasPKField) { columnBuilder.append(",").append(pkName); } StringBuilder sql = new StringBuilder(); sql.append("SELECT ").append(columnBuilder).append(" FROM ").append(tableName).append(" WHERE ").append(condition); BaseProcessor processor = new BeanListProcessor(baseEntity); List<T> results = getJdbcSession().executeQuery(sql.toString(), param, processor); if (results.size() >= 1) { result = results.get(0); }
for (OneToOneType oneToOneType : characteristicTypes) { Class childClz = oneToOneType.getTargetEntity(); String childTable = BeanProcessorHelper.getTableName(childClz, oneToOneType.getTargetFullName()); String childSQL = buildSQLWithSelectedFields(oneToOneType, clz, fullName, selectedFields, resultMap, childTable); if (StringUtils.isNotEmpty(childSQL)) { characteristicEntityHandler(baseEntity, childSQL, fullName, result, ormAttributeSign); } } for (OneToOneType oneToOneType : parallelTypes) { Class childClz = oneToOneType.getTargetEntity(); String childTable = BeanProcessorHelper.getTableName(childClz, oneToOneType.getTargetFullName()); String childSQL = buildSQLWithSelectedFields(oneToOneType, clz, fullName, selectedFields, resultMap, childTable); if (StringUtils.isNotEmpty(childSQL)) { parallelEntityHandler(baseEntity, childSQL, childTable, result, ormAttributeSign); } } }
}
return result; }
|