数字字典的查询数组导出和数据导入
Posted 金石不渝
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数字字典的查询数组导出和数据导入相关的知识,希望对你有一定的参考价值。
数字字典的查询、数组导出和数据导入核心代码笔记
1、开始对mapper层的编写
public interface DictMapper extends BaseMapper<Dict>
2、对service层接口编写
public interface DictService extends IService<Dict>
//根据数据id查询子数据列表
List<Dict> findChlidData(Long id);
//导出数字字典
void exportData(HttpServletResponse response);
//导入数据字典
public void importData(MultipartFile file);
3、接口的实现代码
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService
//根据数据id查询子数据列表
@Override
@Cacheable(value = "dict",keyGenerator = "keyGenerator")
public List<Dict> findChlidData(Long id)
QueryWrapper<Dict> wrapper = new QueryWrapper<>();
wrapper.eq("parent_id",id);
List<Dict> dictList = baseMapper.selectList(wrapper);
for (Dict dict: dictList)
Long dictId = dict.getId();
boolean isChild = this.isChildren(dictId);
dict.setHasChildren(isChild);
return dictList;
//导出数据字典接口
@Override
public void exportData(HttpServletResponse response)
//设置下载信息
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String fileName = "dict";
response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");
//查询数据库
List<Dict> dictList = baseMapper.selectList(null);
//Dict -- DictEeVo 集合转化为队列
List<DictEeVo> dictVoList = new ArrayList<>();
for(Dict dict:dictList)
DictEeVo dictEeVo = new DictEeVo();
// dictEeVo.setId(dict.getId());
BeanUtils.copyProperties(dict,dictEeVo);
dictVoList.add(dictEeVo);
//调用方法进行写操作
try
EasyExcel.write(response.getOutputStream(), DictEeVo.class).sheet("dict")
.doWrite(dictVoList);
catch (IOException e)
e.printStackTrace();
//导出数字字典
@Override
@CacheEvict(value = "dict", allEntries=true)
public void importData(MultipartFile file)
try
EasyExcel.read(file.getInputStream(),DictEeVo.class,new DictListener(baseMapper)).sheet().doRead();
catch (IOException e)
e.printStackTrace();
//判断id下面是否有子节点
private boolean isChildren(Long id)
QueryWrapper<Dict> wrapper = new QueryWrapper<>();
wrapper.eq("parent_id",id);
Integer count = baseMapper.selectCount(wrapper);
// 0>0 1>0
return count>0;
Controller
@Api(description = "数据字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
@CrossOrigin
public class DictController
@Autowired
private DictService dictService;
@ApiOperation(value = "导入")
// 数字导入
@PostMapping("importData")
public Result importData(MultipartFile file)
dictService.importData(file);
return Result.ok();
//
@ApiOperation(value="导出")
@GetMapping(value = "/exportData")
public void exportData(HttpServletResponse response)
dictService.exportData(response);
//根据数据id查询子数据列表
@ApiOperation(value = "根据数据id查询子数据列表")
@GetMapping("findChildData/id")
public Result findChildData(@PathVariable Long id)
List<Dict> list = dictService.findChlidData(id);
return Result.ok(list);
以上是关于数字字典的查询数组导出和数据导入的主要内容,如果未能解决你的问题,请参考以下文章