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; 017 018import com.mybatisflex.annotation.InsertListener; 019import com.mybatisflex.annotation.SetListener; 020import com.mybatisflex.annotation.UpdateListener; 021import com.mybatisflex.core.dialect.DbType; 022import com.mybatisflex.annotation.KeyType; 023import org.apache.ibatis.session.Configuration; 024import org.apache.ibatis.session.SqlSessionFactory; 025 026import java.util.Map; 027import java.util.concurrent.ConcurrentHashMap; 028 029/** 030 * 全局配置文件 031 */ 032public class FlexGlobalConfig { 033 034 /** 035 * 默认使用 Mysql 数据库类型 036 */ 037 private DbType dbType = DbType.MYSQL; 038 039 /** 040 * Mybatis 配置 041 */ 042 private Configuration configuration; 043 044 /** 045 * 创建好的 sqlSessionFactory 046 */ 047 private SqlSessionFactory sqlSessionFactory; 048 049 /** 050 * 全局的 ID 生成策略配置,当 @Id 未配置 或者 配置 KeyType 为 None 时 051 * 使用当前全局配置 052 */ 053 private KeyConfig keyConfig; 054 055 /** 056 * entity 的监听器 057 */ 058 private Map<Class<?>, SetListener> entitySetListeners = new ConcurrentHashMap<>(); 059 private Map<Class<?>, UpdateListener> entityUpdateListeners = new ConcurrentHashMap<>(); 060 private Map<Class<?>, InsertListener> entityInsertListeners = new ConcurrentHashMap<>(); 061 062 063 /** 064 * 逻辑删除的相关配置 065 */ 066 private Object normalValueOfLogicDelete = FlexConsts.LOGIC_DELETE_NORMAL; 067 private Object deletedValueOfLogicDelete = FlexConsts.LOGIC_DELETE_DELETED; 068 069 070 public DbType getDbType() { 071 return dbType; 072 } 073 074 public void setDbType(DbType dbType) { 075 this.dbType = dbType; 076 } 077 078 public Configuration getConfiguration() { 079 return configuration; 080 } 081 082 public void setConfiguration(Configuration configuration) { 083 this.configuration = configuration; 084 } 085 086 public SqlSessionFactory getSqlSessionFactory() { 087 return sqlSessionFactory; 088 } 089 090 public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) { 091 this.sqlSessionFactory = sqlSessionFactory; 092 } 093 094 public KeyConfig getKeyConfig() { 095 return keyConfig; 096 } 097 098 public void setKeyConfig(KeyConfig keyConfig) { 099 this.keyConfig = keyConfig; 100 } 101 102 public Map<Class<?>, SetListener> getEntitySetListeners() { 103 return entitySetListeners; 104 } 105 106 public void setEntitySetListeners(Map<Class<?>, SetListener> entitySetListeners) { 107 this.entitySetListeners = entitySetListeners; 108 } 109 110 public Map<Class<?>, UpdateListener> getEntityUpdateListeners() { 111 return entityUpdateListeners; 112 } 113 114 public void setEntityUpdateListeners(Map<Class<?>, UpdateListener> entityUpdateListeners) { 115 this.entityUpdateListeners = entityUpdateListeners; 116 } 117 118 public Map<Class<?>, InsertListener> getEntityInsertListeners() { 119 return entityInsertListeners; 120 } 121 122 public void setEntityInsertListeners(Map<Class<?>, InsertListener> entityInsertListeners) { 123 this.entityInsertListeners = entityInsertListeners; 124 } 125 126 public void registerSetListener(SetListener listener, Class<?>... classes) { 127 for (Class<?> aClass : classes) { 128 entitySetListeners.put(aClass, listener); 129 } 130 } 131 132 public void registerUpdateListener(UpdateListener listener, Class<?>... classes) { 133 for (Class<?> aClass : classes) { 134 entityUpdateListeners.put(aClass, listener); 135 } 136 } 137 138 public void registerInsertListener(InsertListener listener, Class<?>... classes) { 139 for (Class<?> aClass : classes) { 140 entityInsertListeners.put(aClass, listener); 141 } 142 } 143 144 public SetListener getSetListener(Class<?> entityClass) { 145 return entitySetListeners.get(entityClass); 146 } 147 148 149 public UpdateListener getUpdateListener(Class<?> entityClass) { 150 return entityUpdateListeners.get(entityClass); 151 } 152 153 154 public InsertListener getInsertListener(Class<?> entityClass) { 155 return entityInsertListeners.get(entityClass); 156 } 157 158 159 public Object getNormalValueOfLogicDelete() { 160 return normalValueOfLogicDelete; 161 } 162 163 public void setNormalValueOfLogicDelete(Object normalValueOfLogicDelete) { 164 if (normalValueOfLogicDelete == null){ 165 throw new NullPointerException("normalValueOfLogicDelete can not be null."); 166 } 167 this.normalValueOfLogicDelete = normalValueOfLogicDelete; 168 } 169 170 public Object getDeletedValueOfLogicDelete() { 171 return deletedValueOfLogicDelete; 172 } 173 174 public void setDeletedValueOfLogicDelete(Object deletedValueOfLogicDelete) { 175 if (deletedValueOfLogicDelete == null){ 176 throw new NullPointerException("deletedValueOfLogicDelete can not be null."); 177 } 178 this.deletedValueOfLogicDelete = deletedValueOfLogicDelete; 179 } 180 181 public static ConcurrentHashMap<String, FlexGlobalConfig> getGlobalConfigs() { 182 return globalConfigs; 183 } 184 185 public static void setGlobalConfigs(ConcurrentHashMap<String, FlexGlobalConfig> globalConfigs) { 186 FlexGlobalConfig.globalConfigs = globalConfigs; 187 } 188 189 190 /** 191 * 对应的是 注解 {@link com.mybatisflex.annotation.Id} 的配置 192 */ 193 public static class KeyConfig { 194 private KeyType keyType; 195 private String value; 196 private boolean before = true; 197 198 public KeyType getKeyType() { 199 return keyType; 200 } 201 202 public void setKeyType(KeyType keyType) { 203 this.keyType = keyType; 204 } 205 206 public String getValue() { 207 return value; 208 } 209 210 public void setValue(String value) { 211 this.value = value; 212 } 213 214 public boolean isBefore() { 215 return before; 216 } 217 218 public void setBefore(boolean before) { 219 this.before = before; 220 } 221 } 222 223 224 /////static factory methods///// 225 private static ConcurrentHashMap<String, FlexGlobalConfig> globalConfigs = new ConcurrentHashMap<>(); 226 private static FlexGlobalConfig defaultConfig = new FlexGlobalConfig(); 227 228 public static FlexGlobalConfig getDefaultConfig() { 229 return defaultConfig; 230 } 231 232 public static FlexGlobalConfig getConfig(Configuration configuration) { 233 return getConfig(configuration.getEnvironment().getId()); 234 } 235 236 public static FlexGlobalConfig getConfig(String environmentId) { 237 return globalConfigs.get(environmentId); 238 } 239 240 public static synchronized void setConfig(String id, FlexGlobalConfig config) { 241 //first setConfig,copy the config to default 242 if (globalConfigs.isEmpty()) { 243 244 defaultConfig.setSqlSessionFactory(config.sqlSessionFactory); 245 defaultConfig.setDbType(config.dbType); 246 defaultConfig.setConfiguration(config.configuration); 247 248 if (defaultConfig.getKeyConfig() == null 249 && config.keyConfig != null) { 250 defaultConfig.setKeyConfig(config.keyConfig); 251 } 252 253 config = defaultConfig; 254 } 255 256 globalConfigs.put(id, config); 257 } 258 259 260}