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