package common; import com.BWebApplication; import com.alibaba.fastjson.JSON; import com.hai.entity.SecRegion; import com.hai.service.CommonService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import javax.annotation.Resource; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @ClassName RegionTest * @Description: TODO () * @Author 胡锐 * @Date 2020/12/29 **/ @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = BWebApplication.class) @WebAppConfiguration public class RegionTest { @Resource private CommonService commonService; @Test public void addLogs(){ try { OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("exampleWrite.json"),"UTF-8"); List> jobTypeList = new ArrayList<>(); List> children1; List parentRegion = commonService.getCities(); for (SecRegion parent : parentRegion) { Map map = new HashMap<>(); map.put("value", parent.getRegionId()); map.put("label", parent.getRegionName()); // 查询二级 List chinRegion = commonService.getRegionsByParentId(parent.getRegionId()); children1 = new ArrayList<>(); for (SecRegion chin : chinRegion) { Map map1 = new HashMap<>(); map1.put("value", chin.getRegionId()); map1.put("label", chin.getRegionName()); children1.add(map1); } map.put("children", children1); jobTypeList.add(map); } osw.write(JSON.toJSONString(jobTypeList)); osw.flush();//清空缓冲区,强制输出数据 osw.close();//关闭输出流 }catch (Exception e){ e.printStackTrace(); } } }