java-development-manual

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Java开发手册(嵩山版)

Java Development Manual (Songshan Edition)

概述

Overview

本手册基于阿里巴巴Java开发手册(嵩山版),将规约分为7个维度。规约按约束力强弱分为:
级别含义说明
【强制】必须遵守违反可能导致严重问题
【推荐】建议遵守提升代码质量和可维护性
【参考】可选择性采纳根据实际情况判断
This manual is based on the Alibaba Java Development Manual (Songshan Edition) and organizes its standards into seven areas. Standards are categorized by the strength of their requirements:
LevelMeaningDescription
MandatoryMust be followedViolations may cause serious problems
RecommendedRecommendedImproves code quality and maintainability
ReferenceOptionally adoptedChoose based on the actual situation

章节导航

Chapter Navigation

根据需求选择对应章节的详细规约:
章节适用场景详细文档
编程规约命名、格式、OOP、并发、集合处理coding-convention.md
异常日志错误码、异常处理、日志规范exception-log.md
单元测试测试用例、覆盖率、Mockunit-test.md
安全规约SQL注入、XSS、CSRF、脱敏security.md
MySQL数据库建表、索引、SQL、ORMmysql.md
工程结构分层架构、依赖管理、服务器project-structure.md
设计规约UML、设计模式、设计原则design.md
Select the detailed standards in the corresponding chapter according to your needs:
ChapterApplicable ScenariosDetailed Documentation
Coding ConventionsNaming, formatting, OOP, concurrency, collection handlingcoding-convention.md
Exceptions and LoggingError codes, exception handling, logging standardsexception-log.md
Unit TestingTest cases, coverage, Mockunit-test.md
Security StandardsSQL injection, XSS, CSRF, data maskingsecurity.md
MySQL DatabaseTable creation, indexes, SQL, ORMmysql.md
Project StructureLayered architecture, dependency management, serversproject-structure.md
Design ConventionsUML, design patterns, design principlesdesign.md

快速参考

Quick Reference

命名规范速查

Naming Convention Quick Reference

java
// 类名:UpperCamelCase
public class UserService { }
public class UserDO { }      // DO/DTO/VO例外

// 方法名/变量:lowerCamelCase
private String userName;
public void getUserById() { }

// 常量:全大写+下划线
public static final int MAX_RETRY_COUNT = 3;

// 包名:全小写
package com.company.project.service;
java
// 类名:UpperCamelCase
public class UserService { }
public class UserDO { }      // DO/DTO/VO例外

// 方法名/变量:lowerCamelCase
private String userName;
public void getUserById() { }

// 常量:全大写+下划线
public static final int MAX_RETRY_COUNT = 3;

// 包名:全小写
package com.company.project.service;

禁止事项速查

Prohibited Practices Quick Reference

禁止原因
拼音命名可读性差
魔法值难以维护
SELECT *
性能和可维护性
Executors创建线程池可能OOM
字符串拼接SQL注入风险
finally中return丢失try返回值
foreach中removeConcurrentModificationException
ProhibitedReason
Pinyin-based namingPoor readability
Magic valuesDifficult to maintain
SELECT *
Performance and maintainability issues
Creating thread pools with ExecutorsMay cause OOM
String concatenation for SQLInjection risk
return in finallyLoses the try return value
remove within foreachConcurrentModificationException

必须事项速查

Required Practices Quick Reference

必须原因
覆写方法加@Override避免签名错误
表必备三字段id, create_time, update_time
敏感数据脱敏隐私保护
参数校验安全防护
ThreadLocal回收避免内存泄漏
日志用占位符性能优化
RequiredReason
Add
@Override
when overriding methods
Prevents signature errors
Required table fieldsid, create_time, update_time
Mask sensitive dataPrivacy protection
Validate parametersSecurity protection
Clean up ThreadLocalPrevents memory leaks
Use placeholders in log messagesPerformance optimization

异常处理速查

Exception Handling Quick Reference

java
// 正确的异常处理
try {
    // 业务逻辑
} catch (SpecificException e) {
    logger.error("操作失败, 参数: {}", params, e);
    throw new BusinessException("用户友好提示", e);
} finally {
    // 资源关闭(JDK7+ try-with-resources)
}
java
// 正确的异常处理
try {
    // 业务逻辑
} catch (SpecificException e) {
    logger.error("操作失败, 参数: {}", params, e);
    throw new BusinessException("用户友好提示", e);
} finally {
    // 资源关闭(JDK7+ try-with-resources)
}

数据库速查

Database Quick Reference

sql
-- 建表必备
CREATE TABLE example (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT,
    `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 索引命名
-- 主键: pk_字段名
-- 唯一: uk_字段名
-- 普通: idx_字段名
sql
-- 建表必备
CREATE TABLE example (
    `id` bigint unsigned NOT NULL AUTO_INCREMENT,
    `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 索引命名
-- 主键: pk_字段名
-- 唯一: uk_字段名
-- 普通: idx_字段名

并发处理速查

Concurrency Handling Quick Reference

java
// 线程池创建
ThreadPoolExecutor executor = new ThreadPoolExecutor(
    corePoolSize,
    maximumPoolSize,
    keepAliveTime,
    TimeUnit.SECONDS,
    new LinkedBlockingQueue<>(queueCapacity),
    new ThreadFactory() {
        private AtomicInteger counter = new AtomicInteger(1);
        public Thread newThread(Runnable r) {
            return new Thread(r, "worker-" + counter.getAndIncrement());
        }
    },
    new ThreadPoolExecutor.CallerRunsPolicy()
);

// ThreadLocal使用
try {
    threadLocal.set(value);
    // 业务逻辑
} finally {
    threadLocal.remove();  // 必须回收
}
java
// 线程池创建
ThreadPoolExecutor executor = new ThreadPoolExecutor(
    corePoolSize,
    maximumPoolSize,
    keepAliveTime,
    TimeUnit.SECONDS,
    new LinkedBlockingQueue<>(queueCapacity),
    new ThreadFactory() {
        private AtomicInteger counter = new AtomicInteger(1);
        public Thread newThread(Runnable r) {
            return new Thread(r, "worker-" + counter.getAndIncrement());
        }
    },
    new ThreadPoolExecutor.CallerRunsPolicy()
);

// ThreadLocal使用
try {
    threadLocal.set(value);
    // 业务逻辑
} finally {
    threadLocal.remove();  // 必须回收
}

使用指南

Usage Guide

代码审查场景

Code Review Scenarios

  1. 命名检查 → 查看 coding-convention.md 的"命名风格"章节
  2. 并发问题 → 查看 coding-convention.md 的"并发处理"章节
  3. 异常处理 → 查看 exception-log.md
  4. 安全问题 → 查看 security.md
  1. Naming checks → Check the "Naming Style" section of coding-convention.md
  2. Concurrency issues → Check the "Concurrency Handling" section of coding-convention.md
  3. Exception handling → Check exception-log.md
  4. Security issues → Check security.md

新项目搭建场景

New Project Setup Scenarios

  1. 架构设计 → 查看 design.md
  2. 分层结构 → 查看 project-structure.md
  3. 数据库设计 → 查看 mysql.md
  4. 单元测试 → 查看 unit-test.md
  1. Architecture design → Check design.md
  2. Layered structure → Check project-structure.md
  3. Database design → Check mysql.md
  4. Unit testing → Check unit-test.md

问题排查场景

Troubleshooting Scenarios

  1. NPE问题 → 查看 exception-log.md 的"NPE防护"
  2. 性能问题 → 查看 mysql.md 的"索引规约"
  3. 并发问题 → 查看 coding-convention.md 的"并发处理"
  1. NPE issues → Check the "NPE Protection" section of exception-log.md
  2. Performance issues → Check the "Index Standards" section of mysql.md
  3. Concurrency issues → Check the "Concurrency Handling" section of coding-convention.md