|
19 | 19 | import org.apache.ibatis.builder.annotation.ProviderContext;
|
20 | 20 | import org.apache.ibatis.jdbc.SQL;
|
21 | 21 |
|
| 22 | +import java.lang.reflect.Field; |
| 23 | +import java.lang.reflect.Method; |
| 24 | +import java.lang.reflect.ParameterizedType; |
| 25 | +import java.lang.reflect.Type; |
| 26 | +import java.util.LinkedHashMap; |
22 | 27 | import java.util.List;
|
23 | 28 | import java.util.Map;
|
24 | 29 |
|
@@ -213,4 +218,107 @@ public String buildSelectByIdAndNameMultipleParamAndProviderContext(final Intege
|
213 | 218 | }}.toString();
|
214 | 219 | }
|
215 | 220 |
|
| 221 | + private Class getEntityClass(ProviderContext providerContext){ |
| 222 | + Method mapperMethod = providerContext.getMapperMethod(); |
| 223 | + Class<?> declaringClass = mapperMethod.getDeclaringClass(); |
| 224 | + Class<?> mapperClass = providerContext.getMapperType(); |
| 225 | + |
| 226 | + Type[] types = mapperClass.getGenericInterfaces(); |
| 227 | + for (Type type : types) { |
| 228 | + if (type instanceof ParameterizedType) { |
| 229 | + ParameterizedType t = (ParameterizedType) type; |
| 230 | + if (t.getRawType() == declaringClass || mapperClass.isAssignableFrom((Class<?>) t.getRawType())) { |
| 231 | + Class<?> returnType = (Class<?>) t.getActualTypeArguments()[0]; |
| 232 | + return returnType; |
| 233 | + } |
| 234 | + } |
| 235 | + } |
| 236 | + throw new RuntimeException("The interface [" |
| 237 | + + mapperClass.getCanonicalName() + "] must specify a generic type."); |
| 238 | + } |
| 239 | + |
| 240 | + private Map<String, String> getColumnMap(ProviderContext context){ |
| 241 | + Class entityClass = getEntityClass(context); |
| 242 | + Field[] fields = entityClass.getDeclaredFields(); |
| 243 | + Map<String, String> columnMap = new LinkedHashMap<String, String>(); |
| 244 | + for (Field field : fields) { |
| 245 | + BaseMapper.Column column = field.getAnnotation(BaseMapper.Column.class); |
| 246 | + if(column != null){ |
| 247 | + String columnName = column.value(); |
| 248 | + if(columnName == null || columnName.length() == 0){ |
| 249 | + columnName = field.getName(); |
| 250 | + } |
| 251 | + columnMap.put(columnName, field.getName()); |
| 252 | + } |
| 253 | + } |
| 254 | + if(columnMap.size() == 0){ |
| 255 | + throw new RuntimeException("There is no field in the class [" |
| 256 | + + entityClass.getCanonicalName() + "] that specifies the @BaseMapper.Column annotation."); |
| 257 | + } |
| 258 | + return columnMap; |
| 259 | + } |
| 260 | + |
| 261 | + public String buildInsertSelective(ProviderContext context) { |
| 262 | + final String tableName = context.getMapperType().getAnnotation(BaseMapper.Meta.class).tableName(); |
| 263 | + Map<String, String> columnMap = getColumnMap(context); |
| 264 | + StringBuffer sqlBuffer = new StringBuffer(); |
| 265 | + sqlBuffer.append("<script>"); |
| 266 | + sqlBuffer.append("insert into "); |
| 267 | + sqlBuffer.append(tableName); |
| 268 | + sqlBuffer.append("<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">"); |
| 269 | + for (Map.Entry<String, String> entry : columnMap.entrySet()) { |
| 270 | + sqlBuffer.append("<if test=\"").append(entry.getValue()).append(" != null\">"); |
| 271 | + sqlBuffer.append(entry.getKey()).append(","); |
| 272 | + sqlBuffer.append("</if>"); |
| 273 | + } |
| 274 | + sqlBuffer.append("</trim>"); |
| 275 | + sqlBuffer.append("<trim prefix=\"VALUES (\" suffix=\")\" suffixOverrides=\",\">"); |
| 276 | + for (String field : columnMap.values()) { |
| 277 | + sqlBuffer.append("<if test=\"").append(field).append(" != null\">"); |
| 278 | + sqlBuffer.append("#{").append(field).append("} ,"); |
| 279 | + sqlBuffer.append("</if>"); |
| 280 | + } |
| 281 | + sqlBuffer.append("</trim>"); |
| 282 | + sqlBuffer.append("</script>"); |
| 283 | + return sqlBuffer.toString(); |
| 284 | + } |
| 285 | + |
| 286 | + public String buildUpdateSelective(ProviderContext context) { |
| 287 | + final String tableName = context.getMapperType().getAnnotation(BaseMapper.Meta.class).tableName(); |
| 288 | + Map<String, String> columnMap = getColumnMap(context); |
| 289 | + StringBuffer sqlBuffer = new StringBuffer(); |
| 290 | + sqlBuffer.append("<script>"); |
| 291 | + sqlBuffer.append("update "); |
| 292 | + sqlBuffer.append(tableName); |
| 293 | + sqlBuffer.append("<set>"); |
| 294 | + for (Map.Entry<String, String> entry : columnMap.entrySet()) { |
| 295 | + sqlBuffer.append("<if test=\"").append(entry.getValue()).append(" != null\">"); |
| 296 | + sqlBuffer.append(entry.getKey()).append(" = #{").append(entry.getValue()).append("} ,"); |
| 297 | + sqlBuffer.append("</if>"); |
| 298 | + } |
| 299 | + sqlBuffer.append("</set>"); |
| 300 | + //For simplicity, there is no @Id annotation here, using default id directly |
| 301 | + sqlBuffer.append("where id = #{id}"); |
| 302 | + sqlBuffer.append("</script>"); |
| 303 | + return sqlBuffer.toString(); |
| 304 | + } |
| 305 | + |
| 306 | + public String buildGetByEntityQuery(ProviderContext context) { |
| 307 | + final String tableName = context.getMapperType().getAnnotation(BaseMapper.Meta.class).tableName(); |
| 308 | + Map<String, String> columnMap = getColumnMap(context); |
| 309 | + StringBuffer sqlBuffer = new StringBuffer(); |
| 310 | + sqlBuffer.append("<script>"); |
| 311 | + sqlBuffer.append("select * from "); |
| 312 | + sqlBuffer.append(tableName); |
| 313 | + sqlBuffer.append("<where>"); |
| 314 | + for (Map.Entry<String, String> entry : columnMap.entrySet()) { |
| 315 | + sqlBuffer.append("<if test=\"").append(entry.getValue()).append(" != null\">"); |
| 316 | + sqlBuffer.append("and ").append(entry.getKey()).append(" = #{").append(entry.getValue()).append("}"); |
| 317 | + sqlBuffer.append("</if>"); |
| 318 | + } |
| 319 | + sqlBuffer.append("</where>"); |
| 320 | + sqlBuffer.append("</script>"); |
| 321 | + return sqlBuffer.toString(); |
| 322 | + } |
| 323 | + |
216 | 324 | }
|
0 commit comments