likes
comments
collection
share

一套Java开发中的程序链路执行流程

作者站长头像
站长
· 阅读数 2

作为一个前端+Nodejs开发者,目前在业务需求中需要学习掌握基本的Java开发链路,理解程序的执行流程。这里所分享的文章主要是参考工程现有的CURD程序链路进行学习之后,自己总结和思考的内容,也作为一个沉淀和分享,当然这篇内容对于Java开发者来说可能很是入门级,有任何建议欢迎随时分享~

主要内容包括controller、executor、assembler、repository、convertor、mapper以及mybatis-xml-sql等组件的作用和相互关系。

链路概述

controller -> executor -> assembler(command => entity) -> repository -> convertor(entity => DO) -> mapper -> mybatis-xml -> convertor(DO => entity) => controller。

分层介绍

Controller层

在Spring框架中,Controller层通常是处理HTTP请求的入口。Controller负责接收客户端请求,解析请求参数,并调用合适的业务逻辑处理程序。Controller保持轻量,只关注HTTP层面的逻辑,而不涉及业务逻辑的处理。

@RestController
public class ExampleController {

    @Autowired
    private ExampleExecutor exampleExecutor;

    @PostMapping("/example")
    public ResponseEntity<?> createExample(@RequestBody ExampleCommand command) {
        ExampleEntity entity = exampleExecutor.execute(command);
        return ResponseEntity.ok(entity);
    }
}

Executor层

Executor层是业务逻辑的执行者。它接收来自Controller的指令(通常封装在Command对象中),并将其转换为领域实体(entity)或执行相应的业务逻辑。

@Component
public class ExampleExecutor {

    @Autowired
    private ExampleAssembler exampleAssembler;

    @Autowired
    private ExampleRepository exampleRepository;

    public ExampleEntity execute(ExampleCommand command) {
        ExampleEntity entity = exampleAssembler.toEntity(command);
        return exampleRepository.save(entity);
    }
}

Assembler层

Assembler层负责将Command对象转换为领域实体对象。它是数据转换组装的地方

@Component
public class ExampleAssembler {

    public ExampleEntity toEntity(ExampleCommand command) {
        // ...
        return new ExampleEntity(...);
    }
}

Repository层

Repository层是领域实体和数据映射层之间的桥梁。它抽象了数据存储的逻辑,使得Executor可以通过此层的方法进行数据操作,而无需关心底层数据库的细节。

@Repository
public interface ExampleRepository extends IRepository<ExampleEntity> {
}

Convertor层

Convertor层的作用是在不同的数据模型之间进行转换。在这里,我们经常会将领域实体(Entity)转换为数据对象(Data Object,简称DO),这些DO对象将被用于数据库交互。

@Component
public class ExampleConvertor {

    public ExampleDO toDataObject(ExampleEntity entity) {
        // ...
        return new ExampleDO(...);
    }
}

Mapper层

Mapper层包含了MyBatis的Mapper接口,它定义了与数据库交互所需的方法。Mapper接口通常会通过XML文件或注解的方式映射到SQL语句。

@Mapper
public interface ExampleMapper {
    void insert(ExampleDO exampleDO);
}

MyBatis XML

MyBatis XML文件定义了SQL语句,这些语句映射了Mapper接口的方法。XML文件中的SQL是实现数据持久化操作的核心,它描述了如何将数据从Java对象转换到数据库表中,以及如何从数据库表中检索数据。

<mapper namespace="com.example.mapper.ExampleMapper">
    <insert id="insert" parameterType="ExampleDO">
        INSERT INTO example(...) VALUES(...)
    </insert>
</mapper>

转换和返回

一旦数据被成功地存储或检索,Convertor层将再次发挥作用,将DO对象转换回领域实体。这些实体将被传递回Executor层,然后返回给Controller层,最终返回响应给客户端。

// 在Repository或Mapper操作后
ExampleEntity entity = exampleConvertor.toEntity(exampleDO);
return entity;

总结

  1. Controller:处理外部请求,将业务操作委托给应用层。
  2. Executor:执行特定的业务逻辑,通常是应用服务层的组件。
  3. Assembler:转换命令对象(Command)为领域对象(Entity)。
  4. Repository:提供集合式的接口,用于领域对象的持久化操作。
  5. Convertor:在领域对象(Entity)和数据对象(Data Object,简称DO)之间进行转换,DO通常是数据库映射的对象。
  6. Mapper:MyBatis的数据映射层,定义数据库操作的接口。
  7. MyBatis XML SQL:配置SQL语句的地方,将Mapper接口方法映射为SQL操作。
  8. Convertor:通常在查询操作后,将数据对象(DO)转换回领域对象(Entity),以便返回到业务层。
  9. Controller:最后,控制器将处理结果返回给客户端。

每层只关心特定的职责,这有助于代码的组织和维护,对于构建中大型项目来说也是至关重要的

感想

即便写过Nest或者Midway这种带有分层的Node应用框架也会对Java程序这一套链路感叹层次之多,模板之杂,但是这种分层我相信对于构建高效、可维护的应用程序是至关重要。

不过实际应用场景之中根据项目规模与预期,应该也是可以简化的,例如,在一些简单的CRUD操作中,可能不需要Executor层,Controller可以直接调用Repository;或者Assembler和Convertor的职责可能会合并,不需要那么细的粒度。