在读项目代码时看见了一种主键的方式,通过加密算法用来给定关联表的主键值。
员工和部门表主键UUID,关联表使用 (员工Id+部门Id)通过MD5算法,这样在查询关联信息的时候可以直接根据双方的Id就可以得到关联表的信息,而不需要做复杂的关联查询。
import lombok.SneakyThrows;
import org.springframework.util.DigestUtils;
import java.nio.charset.StandardCharsets;
import java.util.UUID;
public class Test {
@SneakyThrows
public static void main(String[] args) {
String userId = UUID.randomUUID().toString().replace("-","");
String deptId = UUID.randomUUID().toString().replace("-","");
System.out.println("------------第一次新增关联关系 -------------");
System.out.println("员工Id: "+userId);
System.out.println("部门Id: "+deptId);
System.out.println("关联表Id: "+DigestUtils.md5DigestAsHex((userId+deptId).getBytes(StandardCharsets.UTF_8)));
System.out.println("----------------------------------------");
System.out.println("------------第二次查询关联关系 -------------");
System.out.println("关联表Id: "+DigestUtils.md5DigestAsHex((userId+deptId).getBytes(StandardCharsets.UTF_8)));
System.out.println("----------------------------------------");
}
}