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