分页生成sitemap.xml

背景:需要查询某个表然后生成sitemap.xml文件,部署服务器的时候发觉无法生成,检查发现是由于读取数据库的时候一下子查询的结果集太大,大概有50w条,导致响应很久都没有结果,需要进行分页查询,改造的思路如下:
DepartmentKeyword vo = new DepartmentKeyword();
vo.setStatus(10);
int total=bxkcDepartmentKeywordService.countKeywordList(vo);
int pageNo = total/pageSize+1;//总页数

首先去查询总记录数,执行sql相当于:select count(*) from xxxx;pageNo表示分页的总数,pageSize表示每次查询的条数,+1表示最后一页

		for (int i = 1; i <= pageNo; i++) {
			StringBuffer buffer = new StringBuffer();
			list = bxkcDepartmentKeywordService.getKeywordList(vo,i,pageSize);
			File file=  createFile(i, relativelyPath,indexPath, fos, pw,buffer);
			for (int j = 0; j < list.size(); j++) {
				buffer.append("<url>\r\n");
				buffer.append("<loc>http://www.xxx.com/public/"+list.get(j).getPinyin()+"</loc>\r\n");
				buffer.append("<lastmod>"+currentTime+"</lastmod>\r\n");
				buffer.append("<changefreq>daily</changefreq>\r\n");	
				buffer.append("<priority>0.7</priority>\r\n");	
				buffer.append("</url>\r\n");
			}
			buffer.append("</urlset>\r\n");
			fos = new FileOutputStream(file);
			pw = new PrintWriter(fos);
			pw.write(buffer.toString().toCharArray());
			pw.flush();
			pw.close();
		}

生成文件方法

public File createFile(Integer count,String relativelyPath,String indexPath,FileOutputStream fos,PrintWriter pw,StringBuffer buff) throws Exception {
	File file=new File(indexPath);
	if(!file.exists()){
		file.mkdir();//创建文件夹
	}
	String path= indexPath + "/public"+count+".xml";
	File fileName = new File(path);
	if (!fileName.exists()) {
		fileName.createNewFile();
	}
	buff.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
	buff.append("<urlset>\r\n");
	return fileName;
}	
也就是每查找一页生成一个xml文件,效果如下

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值