Token导航 LogoToken导航TokenDH.com
前端设计需要联网github未标认证来源可访问许可证需确认审计通过

best-java-structure最佳 Java structure

Agent Skill

用于辅助 Java 项目开发、面向对象设计、Spring 生态、Maven 或 Gradle 依赖和后端工程实践。它适合让 Agent 分析类结构、设计接口、整理服务分层、生成测试或检查常见代码坏味道。使用时需要结合项目已有架构、包结构和依赖版本,不应只按通用教程改代码;涉及数据库、事务、并发或框架配置时,应先确认运行环境和回归测试范围。

总安装

558

周安装

23

GitHub Stars

1

下载量

182
CodexClaudeCursorGemini CLI

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:best-java-structure(最佳 Java structure)
来源仓库:https://github.com/chasepassion/skills
仓库路径:skills/best-java-structure
安装命令:
npx skills add https://github.com/chasepassion/skills --skill best-java-structure
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。该命令会通过 npx skills 从第三方来源获取 Skill;本站只展示命令,不托管安装包,也不自动执行。

skills.shnpx skills
npx skills add https://github.com/chasepassion/skills --skill best-java-structure

简介

基于 Spring Boot 与 MyBatis-Plus 的五层架构设计指南,涵盖分层规范与 DTO 映射。

  • 适合 Java Web 项目开发、遗留系统重构或团队架构标准化场景。
  • 提供 Controller/Service/Dao 层的职责划分与事务管理建议。
  • 需根据实际业务复杂度调整分层粒度,避免过度设计。
  • best-java-structure 属于前端设计类 Skill,可作为该场景下的辅助能力补充。

SKILL.md

Java Layered Architecture Design Pattern (Spring Boot + MyBatis-Plus)

A practical guide to implementing a classic five-layer architecture with Spring Boot and MyBatis-Plus.

When to Apply

Use this skill in the following scenarios:

  • Creating a new Java Web project
  • Refactoring an existing project architecture
  • Designing a multi-module Java system
  • Checking architectural compliance during code reviews
  • Defining architectural standards for team development
  • Database migration or technology stack migration

Assumptions

  • Spring Boot 3.x
  • MyBatis-Plus for data access
  • DTO/VO separation at API boundaries

Quick Reference

PriorityCategoryImpact AreaPrefix
1Separation of ConcernsArchitectural clarity-principle-
2Unidirectional DependencyDependency management-dependency-
3Interface AbstractionReplaceability-abstraction-
4Data TransferAPI consistency-data-
5Exception HandlingError handling-exception-
6Cross-Layer InvocationArchitectural compliance-crosslayer-
7Reverse DependencyArchitectural compliance-reverse-
8Entity ExposureSecurity-entity-exposure-

Core Principles

1. Separation of Concerns (SoC)

Each layer should solve one category of problems and must not take on responsibilities belonging to other layers.

// Correct: clear responsibilities per layer
// Presentation layer: HTTP handling only
@RestController
public class ProjectController {
    private final ProjectService projectService;
    public ProjectController(ProjectService projectService) {
        this.projectService = projectService;
    }
    @GetMapping("/{id}")
    public ApiReturn<ProjectVo> getProject(@PathVariable Long id) {
        Project project = projectService.getProject(id);
        ProjectVo vo = new ProjectVo();
        BeanUtils.copyProperties(project, vo);
        return ApiReturn.of(vo);
    }
}
// Business logic layer: business rules only
@Service
public class ProjectService {
    private final ProjectMapper projectMapper;
    public ProjectService(ProjectMapper projectMapper) {
        this.projectMapper = projectMapper;
    }
    public Project getProject(Long id) {
        Project project = projectMapper.selectById(id);
        if (project == null) {
            throw new BusinessException(404, "Project does not exist");
        }
        return project;
    }
}
// Data access layer: data interfaces only
public interface ProjectMapper extends BaseMapper<Project> {
}

2. Unidirectional Dependency

Upper layers may call lower layers, but lower layers must not depend on upper layers.

// Correct: depend on a lower layer abstraction
@Service
public class ProjectService {
    private final ProjectMapper projectMapper;
    public ProjectService(ProjectMapper projectMapper) {
        this.projectMapper = projectMapper;
    }
}
// Incorrect: cross-layer invocation
@RestController
public class ProjectController {
    private final ProjectMapper projectMapper;
    public ProjectController(ProjectMapper projectMapper) {
        this.projectMapper = projectMapper;
    }
}

3. Interface Abstraction

Layers should interact via interfaces, not concrete implementations.

// Correct: mapper interface managed by MyBatis-Plus
public interface ProjectMapper extends BaseMapper<Project> {
}
// Incorrect: depending on a non-existent implementation class
public class ProjectService {
    private ProjectMapperImpl projectMapperImpl;
}

Architecture Layers

Classic Five-Layer Architecture

Presentation Layer → Business Logic Layer → Data Access Layer → Persistence Layer → Database Layer
LayerSpring StackCommon NamesCore Responsibilities
Presentation Layer@ControllerController, APIHandle HTTP requests and responses
Business Logic Layer@ServiceServiceImplement business rules and workflows
Data Access Layer@MapperMapper, DAODefine DB access interfaces
Persistence LayerMyBatis XML, EntityMapper XML, EntitySQL mapping and entity definitions
Database LayerMySQLDatabaseStore and retrieve data

Layer Definitions

Presentation Layer

Responsibilities:

  • Handle HTTP requests and responses
  • Receive and validate parameters
  • Routing and dispatching
  • Data format transformation (DTO ↔ Entity ↔ VO)
  • Exception handling and HTTP status mapping Template:
@RestController
@RequestMapping("/api/projects")
public class ProjectController {
    private final ProjectService projectService;
    public ProjectController(ProjectService projectService) {
        this.projectService = projectService;
    }
    @GetMapping("/{id}")
    public ApiReturn<ProjectVo> getProject(@PathVariable Long id) {
        Project project = projectService.getProject(id);
        ProjectVo vo = new ProjectVo();
        BeanUtils.copyProperties(project, vo);
        return ApiReturn.of(vo);
    }
    @PostMapping
    public ApiReturn<ProjectVo> createProject(@Valid @RequestBody ProjectCreateRequest request) {
        Project project = new Project();
        BeanUtils.copyProperties(request, project);
        Project created = projectService.createProject(project);
        ProjectVo vo = new ProjectVo();
        BeanUtils.copyProperties(created, vo);
        return ApiReturn.of(vo);
    }
}

Design notes:

  • Keep it thin: HTTP-related logic only
  • Use DTO/VO for data transfer
  • Centralize validation with Bean Validation and global exception handling
  • No business logic
  • No direct database access

Business Logic Layer

Responsibilities:

  • Implement business rules and workflows
  • Data validation and business checks
  • Authorization and access control
  • Transaction management (boundary here)
  • Coordinate multiple services and mappers
  • Call the data access layer Template:
@Service
public class ProjectService {
    private final ProjectMapper projectMapper;
    public ProjectService(ProjectMapper projectMapper) {
        this.projectMapper = projectMapper;
    }
    public Project getProject(Long id) {
        if (id == null || id <= 0) {
            throw new BusinessException(400, "Invalid project ID");
        }
        Project project = projectMapper.selectById(id);
        if (project == null) {
            throw new BusinessException(404, "Project does not exist");
        }
        return project;
    }
    @Transactional
    public Project createProject(Project project) {
        if (project.getCustomerId() == null) {
            throw new BusinessException(400, "Customer ID must not be null");
        }
        project.setCreatedTime(LocalDateTime.now());
        project.setStatus(ProjectStatus.CREATED);
        projectMapper.insert(project);
        return project;
    }
}

Design notes:

  • Contains core business logic
  • Use @Transactional for transactions
  • Perform business validations
  • Orchestrate multiple services and mappers
  • No HTTP concerns
  • No direct DB connection handling
  • Service should not return DTO (DTO is an input model). Return Entity/Domain or VO consistently.

Data Access Layer (MyBatis-Plus)

Responsibilities:

  • Define DB access interfaces
  • Provide CRUD abstractions
  • Encapsulate persistence details
  • Provide aggregate query interfaces when needed Template:
public interface ProjectMapper extends BaseMapper<Project> {
}

Design notes:

  • Interfaces only; no concrete implementations
  • Method names clearly express intent
  • No SQL statements here (use XML for complex SQL)
  • No business logic
  • No transaction management
  • Must not depend on the Service layer

Persistence Layer

Responsibilities:

  • SQL mapping and result mapping
  • Entity definitions
  • Database-specific SQL fragments (when needed) Template (MyBatis-Plus XML):
<mapper namespace="com.example.mapper.ProjectMapper">
    <select id="selectActive" resultType="com.example.entity.Project">
        SELECT * FROM project WHERE status = 'CREATED'
    </select>
</mapper>

Design notes:

  • Focus on SQL authoring and optimization
  • Result mapping and type conversion
  • Use SQL fragments for reusability
  • No business logic
  • No business validations
  • Connection management and transaction boundaries are handled by Spring and DataSource

Database Layer

Responsibilities:

  • Persistent storage
  • Data integrity constraints
  • Indexes and performance optimization
  • Transaction support Template (MySQL):
CREATE TABLE project (
    id BIGINT PRIMARY KEY AUTO_INCREMENT,
    created_time DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    customer_id BIGINT NOT NULL,
    status VARCHAR(50) DEFAULT 'CREATED'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

Data Transfer Patterns

Using DTO, VO, and Entity

Frontend → Controller → Service → Mapper → Database
           DTO        Entity    Entity
           VO         Entity

Conversion Example

@PostMapping("/projects")
public ApiReturn<ProjectVo> createProject(@Valid @RequestBody ProjectCreateRequest request) {
    Project project = new Project();
    BeanUtils.copyProperties(request, project);
    Project created = projectService.createProject(project);
    ProjectVo vo = new ProjectVo();
    BeanUtils.copyProperties(created, vo);
    return ApiReturn.of(vo);
}

Exception Handling

Centralized Exception Handling

public class BusinessException extends RuntimeException {
    private final int code;
    public BusinessException(int code, String message) {
        super(message);
        this.code = code;
    }
    public int getCode() {
        return code;
    }
}

@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(BusinessException.class)
    public ApiReturn<Void> handleBusinessException(BusinessException e) {
        return ApiReturn.error(e.getCode(), e.getMessage());
    }
    @ExceptionHandler(Exception.class)
    public ApiReturn<Void> handleException(Exception e) {
        return ApiReturn.error(500, "Internal server error");
    }
}

Transaction Guidelines

  • Transaction boundaries belong to Service methods.
  • Use @Transactional(readOnly = true) for read-only queries.
  • Rollback on RuntimeException by default; document checked-exception rollbacks if needed.
  • Avoid transactions in Controller, Mapper, or XML.

Common Pitfalls

Pitfall 1: Cross-Layer Calls

// Incorrect: Controller directly calls Mapper
@RestController
public class ProjectController {
    private final ProjectMapper projectMapper;
    public ProjectController(ProjectMapper projectMapper) {
        this.projectMapper = projectMapper;
    }
    public List<Project> getProjects() {
        return projectMapper.selectList(null);
    }
}

Pitfall 2: Reverse Dependency

// Incorrect: Service depends on Controller
@Service
public class ProjectService {
    private ProjectController projectController;
}

Pitfall 3: Business Logic in SQL

<!-- Incorrect: business logic embedded in SQL -->
<select id="getById" resultMap="BaseResultMap">
    SELECT * FROM project
    WHERE id = #{id}
      AND is_deleted = 0
      AND has_permission(#{currentUserId}, id) = 1
</select>

Pitfall 4: Returning Entity Directly from Controller

// Incorrect: Controller returns Entity directly
@GetMapping("/projects/{id}")
public Project getProject(@PathVariable Long id) {
    return projectService.getProject(id);
}

Project Structure Template (Recommended)

src/main/java/com/xdyai/backend/
  controller/
    internal/
    request/
  service/
    common/
    context/
    integration/
  mapper/
  entity/
    enumeration/

src/main/resources/
  application.yml
  application-dev.yml
  mapper/
  db/
  validation/
  integration/

Best Practices Summary

Presentation Layer

  • Keep it thin: HTTP logic only
  • Use DTO/VO for data transfer
  • Centralized exception handling
  • No business logic
  • No direct Mapper access

Business Logic Layer

  • Contains core business logic
  • Use @Transactional to manage transactions
  • Orchestrate multiple services
  • No HTTP concerns
  • No direct database connection handling

Data Access Layer

  • Interfaces only
  • Clear method naming
  • CRUD via MyBatis-Plus
  • No SQL statements here
  • No business logic

Persistence Layer

  • Focus on SQL authoring
  • Use SQL fragments
  • Use dynamic SQL for complex queries
  • No business logic
  • No business validations

Cross-Layer Communication

  • Controller ← DTO
  • Service ← Entity
  • Mapper ← Entity
  • Controller ← VO
  • Avoid cross-layer calls
  • Avoid reverse dependencies

适合场景

01

用户想查找某类 Agent Skill 时

02

需要根据任务场景推荐可安装能力包时

03

需要对比不同来源的安装命令和来源信息时

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

保留来源站点、仓库和原始说明,方便继续核验

能力 4

展示第三方安全扫描或审计结果

安装后应在对应宿主中按原始 README 的触发条件使用;具体调用方式请以来源页面和 README 为准。

平台分布

Codex

35.45%
按下载量换算65

Claude

28.88%
按下载量换算53

Cursor

19.45%
按下载量换算35

Gemini CLI

9.54%
按下载量换算17

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

需要联网

该 Skill 可能需要联网访问来源站点、仓库或外部 API;具体网络访问范围需要结合源码和 README 复核。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。当前只有一个来源,正式发布前建议补源仓库或其他目录站核验。

来源信息

继续浏览同类 Skills