packagecom.xsw.test;importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.OutputStreamWriter;importjava.util.List;importjavax.annotation.Resource;importorg.apache.log4j.Logger;importorg.junit.Ignore;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.springframework.test.context.ContextConfiguration;importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;importcom.alibaba.fastjson.JSON;importcom.xsw.model.BatchTemp;importcom.xsw.model.TeamsDTO;importcom.xsw.service.BatchService;importcom.xsw.service.TeamService;importcom.xsw.util.DateUtils;
@RunWith(SpringJUnit4ClassRunner.class) //表示继承了SpringJUnit4ClassRunner类
@ContextConfiguration(locations = { "classpath:spring.xml" ,"classpath:spring-mybatis.xml"})public classMybatisTest {private static Logger logger = Logger.getLogger(MybatisTest.class);
@ResourceprivateTeamService teamService;
@ResourceprivateBatchService batchService;
@Test//@Ignore
public void testTeam() throwsIOException{
TeamsDTO cre= teamService.getTeamById(1L);long startTime =System.currentTimeMillis();
logger.info("===========================开始执行读取写入:"+startTime);
File file= new File("F:\\"+DateUtils.date2SStr()+".txt");
BufferedWriter out= new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
List list =teamService.selectAllList();for (int i = 0; i < list.size(); i++) {//logger.info(i+"|"+JSON.toJSON(list.get(i)));
out.write(JSON.toJSONString(list.get(i))+"\r\n");//将json字符串逐行写入text
}
out.flush();//把缓存区内容压入文件
out.close();//最后记得关闭文件
long endTime =System.currentTimeMillis();
logger.info("===========================结束读取写入:"+endTime+"\r\n===========================共耗时:"+(endTime - startTime)/1000+"s");//logger.info(JSON.toJSON(cre));
startTime =System.currentTimeMillis();
logger.info("===========================开始读取:"+startTime);/*读入TXT文件*/InputStreamReader reader= new InputStreamReader( new FileInputStream(file),"UTF-8"); //建立一个输入流对象reader
BufferedReader br = new BufferedReader(reader); //建立一个对象,它把文件内容转成计算机能读懂的语言
String line = "";
line=br.readLine();while (line != null) {
line= br.readLine(); //一次读入一行数据//System.out.println(line);
batchService.insertSelective(JSON.parseObject(line, BatchTemp.class));
}
endTime=System.currentTimeMillis();
logger.info("===========================读取完成:"+endTime+"\r\n===========================共耗时:"+(endTime - startTime)/1000+"s");
}//@Test
public voidtestInsetBatch(){
System.out.println(1111);
}
}