工具

Intellij IDEA

  • 插件
    • Vim
      • 使用 vim 编写代码
    • Lombok
    • grep console: add color for console output
    • string manipulation
    • rainbow brackets
    • error prone
      • compile | use javac with error prone
      • avoid compile error
    • p3c: alibaba java code formatter
    • request-mapper
    • RoboPOJOGenerator
    • sonarLint
    • findbugs
    • json to kotlin class
  • 善用快捷键
    • 重新指定快捷键
    • 组合序列

Java

代码组织结构

  • 数据库的表与 Entity 一一对应

    • table
    • join column
  • DTO 转化

    • org.springframework.beans.BeanUtils.copyProperties 浅复制
    • guava
      • com.google.common.base.Convert
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private static class UserInputDTOConvert extends Converter<UserInputDTO, User> {
@Override
protected User doForward(UserInputDTO userInputDTO) {
User user = new User();
BeanUtils.copyProperties(userInputDTO, user);
return user;
}

@Override
protected UserInputDTO doBackward(User user) {
UserInputDTO userInputDTO = new UserInputDTO();
BeanUtils.copyProperties(user, userInputDTO);
return userInputDTO;
}
}

Bean 验证

  • Lombok @NotNull 等
  • hibernate jsr 303

hibernate validator

链式风格

  • @Accessors(chain = true)
  • Java 8 Lamda

静态构造方法

  • guava
    • List<String> list = Lists.newArrayList();
    • HashMap<String, String> objectObjectHashMap = Maps.newHashMap();

使用 Builder

  • @Builder

枚举

以前的枚举

  • int 枚举模式
  • String 枚举模式

枚举特点

  • 允许添加任意的方法和域,并实现任意的接口
  • 枚举所有的域都应该是 final 的
  • 有一个静态的 values 方法

通用程序设计

  • 如果其他类型更适合,则尽量避免使用字符串
  • 用接口作为类型,程序将会更加灵活

Hibernate Validator

类库

原则:尽量使用类库去实现功能,不要重复造轮子

命令执行

  • Apache Commons Exec
    1
    2
    3
    4
    5
    6
    7
    8
    String command = "scp --help";
    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(stdout);
    CommandLine cmdLine = CommandLine.parse(command);
    DefaultExecutor executor = new DefaultExecutor();
    executor.setStreamHandler(pumpStreamHandler);
    int exitValue = executor.execute(cmdLine);
    System.out.println(stdout.toString());

StringUtils

-Apache Common Lang

  • lang3
    • isEmpty
    • isBlank

IO 操作

  • Apache Common IO
  • FilenameUtils
  • Files
  • Paths: Paths.get(folder, fileName)

任务调度

Database

Database Projection

1
2
3
4
public interface Projection {
@Value("#{target.firstName} #{target.lastName}")
String getFullName();
}

提高

  1. 多看成熟框架的源码
  2. 多回头看自己的代码
  3. 勤于重构

Java

  1. Java parameter