mybatis利用index循环


	
		${keyColumnName} = #{columnValues[${j}]}
	

找到循环里index用法真的很不容易。。。不过这么循环出来之后拿到的数据不太对,不知道为什么columnValues3的数据会把前两条覆盖掉。

数据是这样的:

List keyColumnNames = Arrays.asList("column02", "column03");
List columnValues1 = Arrays.asList("3000083896", "TC201607151449");
List columnValues2 = Arrays.asList("2003088729", "TC201607190433");
List columnValues3 = Arrays.asList("2003088860", "TC201607190431");
List> columnValuesList = Arrays.asList(columnValues1, columnValues2, columnValues3);

想要拼出来这么一条SQL

and ( 
    (column02 = '3000083896' and column03 = 'TC201607151449' )  or 
    (column02 = '2003088729' and column03 = 'TC201607190433' )  or 
    (column02 = '2003088860' and column03 = 'TC201607190431' ) 
 )

但是现在通过mybatis得到的结果是这样:

and ( 
    ( column02 = '2003088860' and column03 = 'TC201607190431' ) or
    ( column02 = '2003088860' and column03 = 'TC201607190431' ) or
    ( column02 = '2003088860' and column03 = 'TC201607190431' ) 
) 

嗯,没错,前两组数据columnValues数据被覆盖掉了,目前还没找到原因,找到解决办法后续再更新、、、

哦对,为了实验那段sql逻辑没问题,还特意写了一段对应的java代码,事实证明逻辑是正确的,不知道mybatis什么毛病。。。

附上这段可供参考的java代码:

List> columnValues = Arrays.asList(
	Arrays.asList("3000083896", "TC201607151449"),
	Arrays.asList("2003088729", "TC201607190433"),
	Arrays.asList("2003088860", "TC201607190431"));
List columnNames = Arrays.asList("column02", "column03");
System.out.print(" and (");
int j = 0;
for (List columnValue : columnValues) {
	if(j>0){
		System.out.print(" or");
	}
	System.out.print(" (");
	int i =0;
	for(String columnName : columnNames){
		if (i>0){
			System.out.print(" and ");
		}
		System.out.print(columnName+" = '"+columnValue.get(i)+"'");
		i++;
	}
	System.out.print(" ) ");
	j++;
}
System.out.print(" )");

2018年5月23日更新:

找到解决办法了~~~,循环改成下面这样就好使了~~泪奔~~o(>_<)o ~~


	
		${keyColumnNames[j]} = #{columnValue}
	

你可能感兴趣的:(Mybatis)