001/** 002 * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package com.mybatisflex.core.keygen; 017 018import com.mybatisflex.core.keygen.impl.FlexIDKeyGenerator; 019import com.mybatisflex.core.keygen.impl.UUIDKeyGenerator; 020 021import java.util.HashMap; 022import java.util.Map; 023 024public class KeyGeneratorFactory { 025 026 private static final Map<String, IKeyGenerator> KEY_GENERATOR_MAP = new HashMap<>(); 027 028 static { 029 /** 内置了 uuid 的生成器,因此主键配置的时候可以直接配置为 @Id(keyType = KeyType.Generator, value = "uuid") 030 * {@link com.mybatisflex.annotation.Id} 031 */ 032 register("uuid", new UUIDKeyGenerator()); 033 register("flexId", new FlexIDKeyGenerator()); 034 } 035 036 037 /** 038 * 获取 主键生成器 039 * 040 * @param name 041 * @return 042 */ 043 public static IKeyGenerator getKeyGenerator(String name) { 044 return KEY_GENERATOR_MAP.get(name.trim()); 045 } 046 047 048 /** 049 * 注册一个主键生成器 050 * 051 * @param key 052 * @param keyGenerator 053 */ 054 public static void register(String key, IKeyGenerator keyGenerator) { 055 KEY_GENERATOR_MAP.put(key.trim(), keyGenerator); 056 } 057 058}