天天看点

对List使用操作并进行拆分分页操作

在项目中往往会要求对多表数据进行查询操作,而两表中的数据只有编号相关联,但查询条件中要根据其中一张表中的电话号码来查询信息,这样我们必须要先根据电话号码来查询编号,再通过编号参数来执行查询操作。但往往一个电话号码又可能对应多条信息(即多个编号),这样操作起来就比较麻烦!常规方法就是循环操作,这样就大大降低了效率!

下面来看看我的操作:

1.首先取出电话号码信息,保存在一个list中(注意此List中只包含编号字段)

List objslist =null;

if(form.getPhoneNumber()!=null && form.getPhoneNumber().length()>0)

{

phoneNumber =form.getPhoneNumber();

[color=red]objslist =vehicleDs.queryVehicleNumber(phoneNumber);[/color] }

2.查询另一张表中的所有数据,然后去比较有相同的编号的数据,保存在list中

list = consumptionRecordDs.querySQLResults(vehicleNumber, mainmeternumber,form.getCodeId(),startTime,endTime, convertString2Timestamp(tingcbeginTime), convertString2Timestamp(tingcendTime), convertString2Timestamp(likbeginTime), convertString2Timestamp(likendTime),form.getConsumptionMoneybegin(),form.getConsumptionMoneyend());

if(list!=null && list.size()>0)

{

UrpcsConsumptionRecord crecord =null;

for(int j=0;j<list.size();j++)

{

crecord =(UrpcsConsumptionRecord)list.get(j);

String veNumber =crecord.getVehicleNumber();

if([color=red]objslist.contains(veNumber))[/color] {

dList.add(crecord);

}else{

continue;

}

}

}

[color=red]注意:红色部分文字,用list与string作比较,取出具有相同编号的对象[/color],关于List.contains()的使用,可以查看java API

这样我就查询出了具有同一手机号码的所有相关数据,但是现在要对这个list进行分页,又出现了问题!

3.对list进行分页操作

count =dList.size(); //取出总记录数

if(count/form.getPageSize()==0 && count%form.getPageSize()>0)

{

fromIndex=0;

toIndex =count;

}else

{

if(count/form.getPageSize()>=1 && count % form.getPageSize()>=0)

{

fromIndex =form.getPageSize() * (form.getPageNo()-1);

if(form.getPageSize() * form.getPageNo() >=count)

{

toIndex =count;

}else{

toIndex =form.getPageSize() * form.getPageNo();

}

}

}

[color=red]dataList =dList.subList(fromIndex, toIndex);[/color]

注意:红色字体部分就是对整个list按照每页记录数进行拆分。

JAVA API中有对List.subList(formIndex,toIndex)的解释,大家可以自寻查找!

继续阅读