使用字符串构建器在java中生成动态查询

时间:2015-12-09 20:56:13

标签: java sql stringbuilder

我正在尝试在java中生成动态查询,我需要生成相同的表Cos_Class_Allocation以及相同的列class_name = ? AND CLASS_ALLOCATION = ?。如何在循环中清理我的代码我注意到我声明了alloc但是没有调用它,是否有更好的方法来编写循环以及如何从我的第一个foreach循环中删除最后一个逗号并最终删除最后一个&# 34;和"来自第二个foreach循环。

我的查询应如下所示:

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 

1 个答案:

答案 0 :(得分:0)

我能够通过使用for循环而不是foreach循环来生成我的动态查询。

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));


    for (int i = 1; i <= cosAllocation.size(); i++) {
            if (i > 1) builder.append(",");
        builder.append(" Cos_Class_Allocation c").append(i);
    }

    for (int i = 1; i <= cosAllocation.size(); i++) {
            builder.append(i == 1 ? " where " : " and ");
            builder.append("c").append(i).append(".class_name = ? AND c").append(i).append(".CLASS_ALLOCATION = ?");
        if (i > 1) {
                builder.append(" and C1.TRAFFIC_PROFILE_ID = c").append(i).append(".TRAFFIC_PROFILE_ID");
        }
    }

    builder.append(" )");

    return builder.toString();
}