0
我想在java中生成一個動態查詢,我需要生成相同的表Cos_Class_Allocation
和同樣的列class_name = ? AND CLASS_ALLOCATION = ?
。我怎樣才能在循環中清理我的代碼我注意到我聲明瞭alloc,但沒有調用它,是否有更好的方法來編寫循環,以及如何從我的第一個foreach循環中刪除最後一個逗號,最後刪除最後一個「And 「來自第二個foreach循環。使用字符串生成器在java中生成一個動態查詢
我的查詢應該是這樣的:
SELECT * FROM TRAFFIC_PROFILE
WHERE COS_MODEL = 'cos4' AND DIRECTION = 'Egress'
AND (PE_INGRESS_FLAG = 'Y' OR PE_EGRESS_FLAG = 'Y')
and TRAFFIC_PROFILE_ID IN (
select distinct (C1.Traffic_PROFILE_ID) from Cos_Class_Allocation C1,
Cos_Class_Allocation C2, Cos_Class_Allocation C3,
Cos_Class_Allocation C4, Cos_Class_Allocation C5, Cos_Class_Allocation C6
where c1.class_name = ? AND c1.CLASS_ALLOCATION = ?
and c2.class_name = ? AND c2.CLASS_ALLOCATION = ?
and c3.class_name = ? AND c3.CLASS_ALLOCATION = ?
and c4.class_name = ? AND c4.CLASS_ALLOCATION = ?
and c5.class_name = ? AND c5.CLASS_ALLOCATION = ?
and c6.class_name = ? AND c6.CLASS_ALLOCATION = ?
and C1.TRAFFIC_PROFILE_ID = c2.TRAFFIC_PROFILE_ID
and C1.TRAFFIC_PROFILE_ID = c3.TRAFFIC_PROFILE_ID
and C1.TRAFFIC_PROFILE_ID = c4.TRAFFIC_PROFILE_ID
and C1.TRAFFIC_PROFILE_ID = c5.TRAFFIC_PROFILE_ID
and C1.TRAFFIC_PROFILE_ID = c6.TRAFFIC_PROFILE_ID) ;
代碼:
private String buildTrafficProfileByCosClassAllocationQuery(TrafficProfileExtension.CosModel cosModel, Direction direction, List<ClassOfServiceAllocation> cosAllocation, RouterType routerType){
StringBuilder builder = new StringBuilder();
builder.append(queryByDirectionRouterTypeAndCosClassAllocation);
builder.append(buildRouterQuery(routerType));
builder.append("and TRAFFIC_PROFILE_ID IN (select distinct (C1.Traffic_PROFILE_ID) from ");
// builder.append(buildCosModelQuery(cosModel));
int i = 1;
for(ClassOfServiceAllocation alloc : cosAllocation){
builder.append(" Cos_Class_Allocation c" + i++ +",");
}
builder.append(" where " );
int n = 1;
int a =1;
for(ClassOfServiceAllocation alloc : cosAllocation){
builder.append("c" + n++ + ".class_name = ? AND c" + a++ + ".CLASS_ALLOCATION = ? AND ");
}
int tp = 2;
for(ClassOfServiceAllocation alloc : cosAllocation){
builder.append("and C1.TRAFFIC_PROFILE_ID = c" + tp++ + ".TRAFFIC_PROFILE_ID ");
}
return builder.toString();
}
這是下面的輸出:(我有一個額外的AND
和額外的最後一個表comma
後)
SELECT * FROM TRAFFIC_PROFILE
WHERE COS_MODEL = ? AND DIRECTION = ?
AND (PE_INGRESS_FLAG = 'Y' OR PE_EGRESS_FLAG = 'Y')
and TRAFFIC_PROFILE_ID IN (
select distinct (C1.Traffic_PROFILE_ID)
from Cos_Class_Allocation c1, Cos_Class_Allocation c2,
Cos_Class_Allocation c3, Cos_Class_Allocation c4,
Cos_Class_Allocation c5, Cos_Class_Allocation c6,
where c1.class_name = ? AND c1.CLASS_ALLOCATION = ?
AND c2.class_name = ? AND c2.CLASS_ALLOCATION = ?
AND c3.class_name = ? AND c3.CLASS_ALLOCATION = ?
AND c4.class_name = ? AND c4.CLASS_ALLOCATION = ?
AND c5.class_name = ? AND c5.CLASS_ALLOCATION = ?
AND c6.class_name = ? AND c6.CLASS_ALLOCATION = ?
AND and C1.TRAFFIC_PROFILE_ID = c2.TRAFFIC_PROFILE_ID
and C1.TRAFFIC_PROFILE_ID = c3.TRAFFIC_PROFILE_ID
and C1.TRAFFIC_PROFILE_ID = c4.TRAFFIC_PROFILE_ID
and C1.TRAFFIC_PROFILE_ID = c5.TRAFFIC_PROFILE_ID
and C1.TRAFFIC_PROFILE_ID = c6.TRAFFIC_PROFILE_ID
請問您可以發佈代碼。 –
剛剛發佈了代碼 – Gio
上面的代碼是否生成正確的字符串?如果是這樣,你應該去CodeReview.stackexchange –