出现的问题
  1. 使用springboot自动注入,没有引用接口,而是直接引用的类,导致在类名修改的时候,要寻找到引用类名的地方,在修改类名

  2. 不同数据库表的pojo类复用,导致后期在数据库表的数据相差较大时,还需再次定义对应的pojo类,此时大量的逻辑都是针对之前的pojo类操作,导致代码改动较大

  3. 在编写dao层时,对于方法的通用性考虑不足,不符合开闭原则,例如在传参时,固定传参某个对象

    1
    List<PatrolArea> queryFirstAreaByAreaId(PatrolDevice patrolDevice)

    造成如果此时有另一对象同样含有areaid,那么需要再写一个方法去适应这种变化,

    改进方案

    1. 将传参类型变为基础类型

      1
      List<PatrolArea> queryFirstAreaByAreaId(String id)
    2. 或者将传参改为接口,只要实现接口,都可以作为参数被传入,如果实现类中没有查询条件,也就是areaid,那么查询就会出错,此时需要在接口中实现赋值方法

      1
      2
      3
      4
      5
      class PatrolArea implements interface{
      String areaId=‘’;
      void setAreaId(String id);
      }
      List<PatrolArea> queryFirstAreaByAreaId(interface param)
获得的经验
  1. 使用hutool自定义线程池,并提交任务以及判断,任务结束时返回响应的方法

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    ExecutorService executor = ExecutorBuilder.create()
    .setCorePoolSize(3)
    .setMaxPoolSize(6)
    .setWorkQueue(new LinkedBlockingQueue<>(100))
    .build();
    try {
    Future task1=executor.submit(new InsertRecordTask());
    Future task2=executor.submit(new InsertItemTask());
    Future task3=executor.submit(new InsertImageTask());
    if(task1.get()==null && task2.get() == null && task3.get() ==null){
    return new ResponseDTO();
    }
    }catch (Exception e){
    executor.shutdown();
    return new ResponseDTO("线程执行异常");
    }
    return new ResponseDTO("线程执行任务失败");

    java提交线程池的两种方式:executesubmit

    1. execute用于提交无返回值的任务,也就是某个class继承了Runnable接口并且重写了run方法的任务。

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      static class ThreadTask implements Runnable{
      @Override
      public void run() {
      System.out.println("正在执行多线程任务......");
      }
      }
      executor.submit(new Runnable() {
      @Override
      public void run() {

      }
      })
    2. submit用于提交有返回值的任务,也就是继承了Callable接口并且重写了call方法的任务,最好指定返回类型。

      1
      2
      3
      4
      5
      6
      7
      static class CallableTask implements Callable{
      @Override
      public String call() throws Exception {
      return "正在有返回值的多线程任务......";
      }
      }

  2. 使用hutool文件工具,接受上传的图片,并保存到本地用ngnix映射的目录方法

  3. 在对项目运行较重要的逻辑部分,要习惯抛出异常,最后由路由捕捉,统一返回到前端,避免大量的判断逻辑

  4. 解析前端较复杂的数据时,可针对数据格式指定对应的dto类,使用注解自动解析(@resquestBody注解只能解析一次)

    1
    2
    3
    4
    5
    6
    7
    8
    public class PatrolRectifiedDTO {

    public List<PatrolImage> rectifiedImages;

    public PatrolRecord rectifiedResult;


    }