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.annotation.KeyType; 019import com.mybatisflex.core.FlexConsts; 020import com.mybatisflex.core.FlexGlobalConfig; 021import com.mybatisflex.core.exception.FlexExceptions; 022import com.mybatisflex.core.table.IdInfo; 023import com.mybatisflex.core.table.TableInfo; 024import com.mybatisflex.core.util.ConvertUtil; 025import org.apache.ibatis.executor.Executor; 026import org.apache.ibatis.executor.keygen.KeyGenerator; 027import org.apache.ibatis.mapping.MappedStatement; 028import org.apache.ibatis.reflection.invoker.Invoker; 029import org.apache.ibatis.session.Configuration; 030 031import java.sql.Statement; 032import java.util.Map; 033 034/** 035 * 通过 java 编码的方式生成主键 036 * 当主键类型配置为 KeyType#Generator 时,使用此生成器生成 037 * {@link KeyType#Generator} 038 */ 039public class CustomKeyGenerator implements KeyGenerator { 040 041 protected Configuration configuration; 042 protected IKeyGenerator keyGenerator; 043 protected TableInfo tableInfo; 044 protected IdInfo idInfo; 045 046 047 public CustomKeyGenerator(Configuration configuration, TableInfo tableInfo, IdInfo idInfo) { 048 this.configuration = configuration; 049 FlexGlobalConfig.KeyConfig globalKeyConfig = FlexGlobalConfig.getConfig(configuration).getKeyConfig(); 050 String keyValue = MybatisKeyGeneratorUtil.getKeyValue(idInfo, globalKeyConfig); 051 this.keyGenerator = KeyGeneratorFactory.getKeyGenerator(keyValue); 052 this.tableInfo = tableInfo; 053 this.idInfo = idInfo; 054 055 ensuresKeyGeneratorNotNull(); 056 } 057 058 private void ensuresKeyGeneratorNotNull() { 059 if (keyGenerator == null) { 060 throw FlexExceptions.wrap("The name of \"%s\" key generator not exist.\n" + 061 "please check annotation @Id(value=\"%s\") at field: %s#%s" 062 , idInfo.getValue(), idInfo.getValue(), tableInfo.getEntityClass().getName(), idInfo.getProperty()); 063 } 064 } 065 066 067 @Override 068 public void processBefore(Executor executor, MappedStatement ms, Statement stmt, Object parameter) { 069 Object entity = ((Map) parameter).get(FlexConsts.ENTITY); 070 Object generateId = keyGenerator.generate(entity, idInfo.getColumn()); 071 try { 072 Invoker setInvoker = tableInfo.getReflector().getSetInvoker(idInfo.getProperty()); 073 Object id = ConvertUtil.convert(generateId, setInvoker.getType()); 074 setInvoker.invoke(entity, new Object[]{id}); 075 } catch (Exception e) { 076 throw FlexExceptions.wrap(e); 077 } 078 } 079 080 081 @Override 082 public void processAfter(Executor executor, MappedStatement ms, Statement stmt, Object parameter) { 083 //do nothing 084 } 085}