TableInfoHelper.java 10.9 KB
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
/**
* Copyright (c) 2011-2020, hubin (jobob@qq.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.baomidou.mybatisplus.toolkit;

import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.entity.GlobalConfiguration;
import com.baomidou.mybatisplus.entity.TableFieldInfo;
import com.baomidou.mybatisplus.entity.TableInfo;
import com.baomidou.mybatisplus.enums.FieldStrategy;
import com.baomidou.mybatisplus.enums.IdType;
import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* <p>
* 实体类反射表辅助类
* </p>
*
* @author hubin sjy
* @Date 2016-09-09
*/
public class TableInfoHelper {

private static final Log logger = LogFactory.getLog(TableInfoHelper.class);

/**
* 缓存反射类表信息
*/
private static final Map<String, TableInfo> tableInfoCache = new ConcurrentHashMap<String, TableInfo>();
/**
* 默认表主键
*/
private static final String DEFAULT_ID_NAME = "id";

/**
* <p>
* 获取实体映射表信息
* <p>
*
* @param clazz
* 反射实体类
* @return
*/
public static TableInfo getTableInfo(Class<?> clazz) {
return tableInfoCache.get(clazz.getName());
}

/**
* <p>
* 随机获取一个TableInfo
* <p>
*
* @return
*/
public static TableInfo getRandomTableInfo() {
Collection<TableInfo> tableInfos = tableInfoCache.values();
if (CollectionUtils.isNotEmpty(tableInfos)) {
Iterator<TableInfo> iterator = tableInfos.iterator();
TableInfo tableInfo = iterator.next();
return tableInfo;
}
return null;
}

/**
* <p>
* 实体类反射获取表信息【初始化】
* <p>
*
* @param clazz
* 反射实体类
* @return
*/
public synchronized static TableInfo initTableInfo(MapperBuilderAssistant builderAssistant, Class<?> clazz) {
TableInfo ti = tableInfoCache.get(clazz.getName());
if (ti != null) {
return ti;
}
TableInfo tableInfo = new TableInfo();
GlobalConfiguration globalCache = null;
if (null != builderAssistant) {
tableInfo.setCurrentNamespace(builderAssistant.getCurrentNamespace());
tableInfo.setConfigMark(builderAssistant.getConfiguration());
globalCache = GlobalConfiguration.GlobalConfig(builderAssistant.getConfiguration());
} else {
// 兼容测试场景
globalCache = GlobalConfiguration.DEFAULT;
}
/* 表名 */
TableName table = clazz.getAnnotation(TableName.class);
String tableName = clazz.getSimpleName();
if (table != null && StringUtils.isNotEmpty(table.value())) {
tableName = table.value();
} else {
// 开启字段下划线申明
if (globalCache.isDbColumnUnderline()) {
tableName = StringUtils.camelToUnderline(tableName);
}
// 大写命名判断
if (globalCache.isCapitalMode()) {
tableName = tableName.toUpperCase();
}
}
tableInfo.setTableName(tableName);
/* 表结果集映射 */
if (table != null && StringUtils.isNotEmpty(table.resultMap())) {
tableInfo.setResultMap(table.resultMap());
}
List<TableFieldInfo> fieldList = new ArrayList<TableFieldInfo>();
List<Field> list = getAllFields(clazz);
boolean existTableId = existTableId(list);
for (Field field : list) {

/**
* 主键ID 初始化
*/
if (existTableId) {
if (initTableId(globalCache, tableInfo, field, clazz)) {
continue;
}
} else if (initFieldId(globalCache, tableInfo, field, clazz)) {
continue;
}

/**
* 字段初始化
*/
if (initTableField(globalCache, fieldList, field, clazz)) {
continue;
}

/**
* 字段, 使用 camelToUnderline 转换驼峰写法为下划线分割法, 如果已指定 TableField , 便不会执行这里
*/
fieldList.add(new TableFieldInfo(globalCache, field.getName()));
}

/* 字段列表 */
tableInfo.setFieldList(fieldList);
/*
* 未发现主键注解,跳过注入
*/
if (null == tableInfo.getKeyColumn()) {
logger.warn(String.format("Warn: Could not find @TableId in Class: %s, initTableInfo Method Fail.",
clazz.getName()));
return null;
}
/*
* 注入
*/
tableInfoCache.put(clazz.getName(), tableInfo);
return tableInfo;
}

/**
* <p>
* 判断主键注解是否存在
* </p>
*
* @param list
* 字段列表
* @return
*/
public static boolean existTableId(List<Field> list) {
boolean exist = false;
for (Field field : list) {
TableId tableId = field.getAnnotation(TableId.class);
if (tableId != null) {
exist = true;
break;
}
}
return exist;
}

/**
* <p>
* 主键属性初始化
* </p>
*
* @param tableInfo
* @param field
* @param clazz
* @return true 继续下一个属性判断,返回 continue;
*/
private static boolean initTableId(GlobalConfiguration globalConfig, TableInfo tableInfo, Field field,
Class<?> clazz) {
TableId tableId = field.getAnnotation(TableId.class);
if (tableId != null) {
if (tableInfo.getKeyColumn() == null) {
/*
* 主键策略( 注解 > 全局 > 默认 )
*/
if (IdType.INPUT != tableId.type()) {
tableInfo.setIdType(tableId.type());
} else {
tableInfo.setIdType(globalConfig.getIdType());
}
/* 字段 */
String column = field.getName();
if (StringUtils.isNotEmpty(tableId.value())) {
column = tableId.value();
tableInfo.setKeyRelated(true);
} else {
// 开启字段下划线申明
if (globalConfig.isDbColumnUnderline()) {
column = StringUtils.camelToUnderline(column);
}
// 全局大写命名
if (globalConfig.isCapitalMode()) {
column = column.toUpperCase();
}
}
tableInfo.setKeyColumn(column);
tableInfo.setKeyProperty(field.getName());
return true;
} else {
throwExceptionId(clazz);
}
}
return false;
}

/**
* <p>
* 主键属性初始化
* </p>
*
* @param tableInfo
* @param field
* @param clazz
* @return true 继续下一个属性判断,返回 continue;
*/
private static boolean initFieldId(GlobalConfiguration globalConfig, TableInfo tableInfo, Field field,
Class<?> clazz) {
String column = field.getName();
if (globalConfig.isCapitalMode()) {
column = column.toUpperCase();
}
if (DEFAULT_ID_NAME.equalsIgnoreCase(column)) {
if (tableInfo.getKeyColumn() == null) {
tableInfo.setIdType(globalConfig.getIdType());
tableInfo.setKeyColumn(column);
tableInfo.setKeyProperty(field.getName());
return true;
} else {
throwExceptionId(clazz);
}
}
return false;
}

/**
* <p>
* 发现设置多个主键注解抛出异常
* </p>
*/
private static void throwExceptionId(Class<?> clazz) {
StringBuffer errorMsg = new StringBuffer();
errorMsg.append("There must be only one, Discover multiple @TableId annotation in ");
errorMsg.append(clazz.getName());
throw new MybatisPlusException(errorMsg.toString());
}

/**
* <p>
* 字段属性初始化
* </p>
*
* @param fieldList
* @param clazz
* @return true 继续下一个属性判断,返回 continue;
*/
private static boolean initTableField(GlobalConfiguration globalCache, List<TableFieldInfo> fieldList, Field field,
Class<?> clazz) {
/* 获取注解属性,自定义字段 */
TableField tableField = field.getAnnotation(TableField.class);
if (tableField != null) {
String columnName = field.getName();
if (StringUtils.isNotEmpty(tableField.value())) {
columnName = tableField.value();
}

Class<?> fieldType = field.getType();
FieldStrategy validate = tableField.validate();
/* 字符串类型默认 FieldStrategy.NOT_EMPTY */
if (String.class.isAssignableFrom(fieldType) && FieldStrategy.NOT_NULL.equals(validate)) {
validate = FieldStrategy.NOT_EMPTY;
}

/*
* el 语法支持,可以传入多个参数以逗号分开
*/
String el = field.getName();
if (StringUtils.isNotEmpty(tableField.el())) {
el = tableField.el();
}
String[] columns = columnName.split(";");
String[] els = el.split(";");
if (null != columns && null != els && columns.length == els.length) {
for (int i = 0; i < columns.length; i++) {
fieldList.add(new TableFieldInfo(globalCache, columns[i], field.getName(), els[i], validate));
}
} else {
String errorMsg = "Class: %s, Field: %s, 'value' 'el' Length must be consistent.";
throw new MybatisPlusException(String.format(errorMsg, clazz.getName(), field.getName()));
}

return true;
}

return false;
}

/**
* 获取该类的所有属性列表
*
* @param clazz
* 反射类
* @return
*/
private static List<Field> getAllFields(Class<?> clazz) {
List<Field> result = new LinkedList<Field>();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {

/* 过滤静态属性 */
if (Modifier.isStatic(field.getModifiers())) {
continue;
}

/* 过滤 transient关键字修饰的属性 */
if (Modifier.isTransient(field.getModifiers())) {
continue;
}

/* 过滤注解非表字段属性 */
TableField tableField = field.getAnnotation(TableField.class);
if (tableField == null || tableField.exist()) {
result.add(field);
}
}

/* 处理父类字段 */
Class<?> superClass = clazz.getSuperclass();
if (superClass.equals(Object.class)) {
return result;
}
result.addAll(getAllFields(superClass));
return result;
}

/**
* 初始化SqlSessionFactory (供Mybatis原生调用)
*
* @param sqlSessionFactory
* @return
*/
public static void initSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
Configuration configuration = sqlSessionFactory.getConfiguration();
GlobalConfiguration globalCache = GlobalConfiguration.GlobalConfig(configuration);
if (globalCache == null) {
GlobalConfiguration defaultCache = GlobalConfiguration.defaults();
defaultCache.setSqlSessionFactory(sqlSessionFactory);
GlobalConfiguration.setGlobalConfig(configuration, defaultCache);
} else {
globalCache.setSqlSessionFactory(sqlSessionFactory);
}
}
}