天天看点

Risc-v学习平台-项目实训第四次总结-SDU

本次工作总结

  1. 完成部分在线编译后端功能
  2. 实现时间排序最新的五个文章后端接口
  3. 实现知识图谱的后端接口实现

相关业务实现

在线编译

业务实现

主要负责接受前端传过来的想要编译的文件。

  1. 文件必须以.risc为后缀,这个前端也会检查
  2. 可以同时接受多个文件
  3. 多次接受文件必须保证文件名不重复

主要业务代码实现:

@Controller
public class FileUploadController {

    /**
    * @Description: 文件上传接口
    * @Author: lmh
    * @Date: 2022/3/17 15:14
    */

    //测试用html
    @RequestMapping("/uploadfile")
    public String uploadfile(){
        return "uploadfile";
    }

    //POST请求接口为/upload
    @PostMapping(value = "/upload",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Result upload(@RequestParam("uploadFile") MultipartFile[] multipartFiles, HttpServletRequest request){

        for (MultipartFile file:multipartFiles){
            String fileName = file.getOriginalFilename();
            String fileType = fileName.substring(fileName.lastIndexOf("."));
            if(!".riscv".equalsIgnoreCase(fileType) && !".riscv".equalsIgnoreCase(fileType)){
                return Result.failupload(500,fileName+"类型有误,请上传riscv类型文件!",fileName);
            }
        }
        //设置当前日期
        String uploaddate= new SimpleDateFormat("yyyy-MM-dd").format(new Date());

        //设置文件上传保存文件路径:这里我放到本地了
        String savepath = "D:/workspace/uploadFile";

        //创建文件夹,当文件夹不存在时,创建文件夹
        File folder = new File(savepath);
        if(!folder.isDirectory()){
            folder.mkdir();
        }

        //建立一个listmap的返回参数
        List<Map<String,Object>> listmap =new ArrayList<>();

        //建立一个循环分别接收多文件,也可以直接上传一个文件
        for(MultipartFile file:multipartFiles){
            //重命名上传的文件,为避免重复,我们使用UUID对文件分别进行命名
            String oldname=file.getOriginalFilename();

            //随机新建文件名,这里可以根据用户信息修改新的文件名
            String newname= UUID.randomUUID().toString().replace("-","")
                    +oldname.substring(oldname.lastIndexOf("."));

            //建立每一个文件上传的返回参数
            Map<String,Object> map=new HashMap<>();
            //文件保存操作
            try {
                //将文件写道指定位置
                file.transferTo(new File(folder,newname));

                //这里是返回给前端的数据中保存文件保存的位置,方便下一步“运行”指令的进行
                String filepath=savepath+"/"+newname;
                //写入返回参数
                map.put("oldname",oldname);
                map.put("newname",newname);
                map.put("filepath",filepath);
                map.put("result","成功!");
                listmap.add(map);
            }catch (IOException ex){
                //操作失败报错并写入返回参数
                ex.printStackTrace();
                map.put("oldname",oldname);
                map.put("newname",newname);
                map.put("filepath","");
                map.put("result","失败!");
            }
        }
        //将执行结果返回(老样子,使用Result形式返回)
        return Result.success(listmap);
    }
}
           

注释比较清晰

返回时间排序前五的文章

业务实现

Controller层:

这里使用的PaperVo实体类主要是为了数据传输的大小,仅将id、title、url传给前端。

/**
    * @Description: 按时间排序的前5(暂定)文章
    * @Author: lmh
    * @Date: 2022/3/26 10:05
    */
    @PostMapping("/newpapers")
//    @LogAnnotation(module = "最新文章排名",operation = "返回最新的5(暂定)条文章")
    public Result newpapers(){
        List<PaperVo> papers = paperService.listNewPapers();
        return Result.success(papers);
    }
           

Service层

/**
* @Description: 最新的5个文章
* @Author: lmh
* @Date: 2022/3/26 10:10
*/
@Override
public List<PaperVo> listNewPapers() {
    return paperMapper.listNewPapers();
}
           

数据库查询语句:

<select id="listNewPapers" resultType="com.sanelysong.riscv.vo.PaperVo">
    SELECT id,title,url
    FROM t_paper
    ORDER BY date DESC LIMIT 5
</select>
           

测试效果

使用Postman进行测试

Risc-v学习平台-项目实训第四次总结-SDU

知识图谱功能后端

实体类设计

知识图谱的主要数据包括节点Entity、关系Role,数据设计如下;

Entity:

@Data
@EqualsAndHashCode(callSuper = false)
@TableName("t_entity")
public class Entity implements Serializable {

    private static final long serialVersionUID=1L;

    private String id;

    private String name;

    private String label;


}
           

Role:

@Data
@EqualsAndHashCode(callSuper = false)
@TableName("t_role")
public class Role implements Serializable {

    private static final long serialVersionUID=1L;

    private Integer id;

    private String startId;

    private String endId;

    private String type;


}
           

RoleVo

/**
* @Description: (起点Entity,终点Entity,关系)
* @Author: lmh
* @Date: 2022/3/25 14:35
*/
@Data
public class RoleVo {
    private String source;
    private String target;
    private RelationVo relation;
}
           

前端所需数据:

包括节点list和关系list。

节点列表数据示例:

Risc-v学习平台-项目实训第四次总结-SDU

关系列表数据示例:

Risc-v学习平台-项目实训第四次总结-SDU

业务实现

Controller层

/**
     * @Description: LogAnnotation注解可以用于排查查询效率,有些瑕疵,但不影响使用
     * @Author: lmh
     * @Date: 2022/3/25 23:14
     */
    @PostMapping("/allmaps")
//    @LogAnnotation(module = "allmaps接口",operation = "返回知识图谱所需数据")
    public Result allmaps(){
        //以map结构返回给前端
        MapsVo mapsVo = new MapsVo();
        List<Entity> entityList = iEntityService.list();
        List<RoleVo> roleVos = iRoleService.findAllRolesVo();
        mapsVo.setEntitys(entityList);
        mapsVo.setRoles(roleVos);
        return Result.success(mapsVo);
    }
           

部分核心Service方法

/**
* @Description: 后端查询知识图谱所需要的数据并返回给前端
* @Author: lmh
* @Date: 2022/3/25 22:44
*/
@Override
public List<RoleVo> findAllRolesVo() {
    List<RoleVo> roleVos = new ArrayList<>();
    QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
    QueryWrapper<Entity> queryWrapper1 = new QueryWrapper<>();
    //直接全查询出来,比挨个查更有效率
    List<Role> roles = roleMapper.selectList(queryWrapper);
    List<Entity> entities = entityMapper.selectList(queryWrapper1);
    //返回一个Map,以key-value形式对list进行遍历追加
    Map<String,Entity> entity_s = new HashMap<>();
    for (int i = 0,size = entities.size();i<size;i++){
        entity_s.put(entities.get(i).getId(),entities.get(i));
    }
    for (Role r:roles) {
        //遍历追加
        Entity entity_start = entity_s.get(r.getStartId());
        Entity entity_end = entity_s.get(r.getEndId());
        RoleVo roleVo = new RoleVo();
        roleVo.setSource(entity_start.getId());
        roleVo.setTarget(entity_end.getId());
        RelationVo relationVo = new RelationVo();
        relationVo.setId(r.getId().toString());
        relationVo.setName(r.getType());
        roleVo.setRelation(relationVo);
        roleVos.add(roleVo);
    }
    return roleVos;
}
           

测试效果

使用Postman进行测试

Risc-v学习平台-项目实训第四次总结-SDU

继续阅读