TableFieldInfo.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /**
  2. * Copyright (c) 2011-2014, hubin (jobob@qq.com).
  3. * <p>
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. * <p>
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. * <p>
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.baomidou.mybatisplus.entity;
  17. import com.baomidou.mybatisplus.annotations.TableField;
  18. import com.baomidou.mybatisplus.annotations.TableLogic;
  19. import com.baomidou.mybatisplus.enums.FieldIgnore;
  20. import com.baomidou.mybatisplus.enums.FieldStrategy;
  21. import com.baomidou.mybatisplus.toolkit.SqlReservedWords;
  22. import com.baomidou.mybatisplus.toolkit.StringUtils;
  23. import java.lang.reflect.Field;
  24. /**
  25. * <p>
  26. * 数据库表字段反射信息
  27. * </p>
  28. *
  29. * @author hubin sjy willenfoo tantan
  30. * @Date 2016-09-09
  31. */
  32. public class TableFieldInfo {
  33. /**
  34. * <p>
  35. * 是否有存在字段名与属性名关联
  36. * </p>
  37. * true , false
  38. */
  39. private boolean related = false;
  40. /**
  41. * 字段名
  42. */
  43. private String column;
  44. /**
  45. * 属性名
  46. */
  47. private String property;
  48. /**
  49. * 属性表达式#{property}, 可以指定jdbcType, typeHandler等
  50. */
  51. private String el;
  52. /**
  53. * 属性类型
  54. */
  55. private String propertyType;
  56. /**
  57. * 字段策略【 默认,自判断 null 】
  58. */
  59. private FieldStrategy fieldStrategy = FieldStrategy.NOT_NULL;
  60. /**
  61. * 逻辑删除值
  62. */
  63. private String logicDeleteValue;
  64. /**
  65. * 逻辑未删除值
  66. */
  67. private String logicNotDeleteValue;
  68. /**
  69. * 字段忽略策略
  70. */
  71. private FieldIgnore fieldIgnore = FieldIgnore.DEFAULT;
  72. /**
  73. * <p>
  74. * 存在 TableField 注解构造函数
  75. * </p>
  76. */
  77. public TableFieldInfo(GlobalConfiguration globalConfig, TableInfo tableInfo, String column,
  78. String el, Field field, TableField tableField) {
  79. this.property = field.getName();
  80. this.propertyType = field.getType().getName();
  81. /*
  82. * 1、注解 value 不存在,开启字段下划线申明<br>
  83. * 2、没有开启下划线申明,但是column与property不等的情况<br>
  84. * 设置 related 为 true
  85. */
  86. if (StringUtils.isEmpty(tableField.value())
  87. && globalConfig.isDbColumnUnderline()) {
  88. /* 开启字段下划线申明 */
  89. this.related = true;
  90. this.setColumn(globalConfig, StringUtils.camelToUnderline(column));
  91. } else {
  92. this.setColumn(globalConfig, column);
  93. if (!column.equals(this.property)) {
  94. this.related = true;
  95. }
  96. }
  97. this.el = el;
  98. /*
  99. * 优先使用单个字段注解,否则使用全局配置<br>
  100. * 自定义字段验证策略 fixed-239
  101. */
  102. if (FieldStrategy.NOT_NULL != tableField.validate()) {
  103. this.fieldStrategy = tableField.validate();
  104. } else {
  105. this.fieldStrategy = globalConfig.getFieldStrategy();
  106. }
  107. tableInfo.setLogicDelete(this.initLogicDelete(globalConfig, field));
  108. /*
  109. * 保存当前字段的插入忽略,更新忽略值
  110. */
  111. this.fieldIgnore = tableField.ignore();
  112. }
  113. public TableFieldInfo(GlobalConfiguration globalConfig, TableInfo tableInfo, Field field) {
  114. if (globalConfig.isDbColumnUnderline()) {
  115. /* 开启字段下划线申明 */
  116. this.related = true;
  117. this.setColumn(globalConfig, StringUtils.camelToUnderline(field.getName()));
  118. } else {
  119. this.setColumn(globalConfig, field.getName());
  120. }
  121. this.property = field.getName();
  122. this.el = field.getName();
  123. this.fieldStrategy = globalConfig.getFieldStrategy();
  124. this.propertyType = field.getType().getName();
  125. tableInfo.setLogicDelete(this.initLogicDelete(globalConfig, field));
  126. }
  127. /**
  128. * <p>
  129. * 逻辑删除初始化
  130. * </p>
  131. *
  132. * @param globalConfig 全局配置
  133. * @param field 字段属性对象
  134. */
  135. private boolean initLogicDelete(GlobalConfiguration globalConfig, Field field) {
  136. if (null == globalConfig.getLogicDeleteValue()) {
  137. // 未设置逻辑删除值不进行
  138. return false;
  139. }
  140. /* 获取注解属性,逻辑处理字段 */
  141. TableLogic tableLogic = field.getAnnotation(TableLogic.class);
  142. if (null != tableLogic) {
  143. if (StringUtils.isNotEmpty(tableLogic.value())) {
  144. this.logicNotDeleteValue = tableLogic.value();
  145. } else {
  146. this.logicNotDeleteValue = globalConfig.getLogicNotDeleteValue();
  147. }
  148. if (StringUtils.isNotEmpty(tableLogic.delval())) {
  149. this.logicDeleteValue = tableLogic.delval();
  150. } else {
  151. this.logicDeleteValue = globalConfig.getLogicDeleteValue();
  152. }
  153. return true;
  154. }
  155. return false;
  156. }
  157. public boolean isRelated() {
  158. return related;
  159. }
  160. public void setRelated(boolean related) {
  161. this.related = related;
  162. }
  163. public String getColumn() {
  164. return column;
  165. }
  166. public void setColumn(GlobalConfiguration globalConfig, String column) {
  167. String temp = SqlReservedWords.convert(globalConfig, column);
  168. if (globalConfig.isCapitalMode() && !isRelated()) {
  169. // 全局大写,非注解指定
  170. temp = temp.toUpperCase();
  171. }
  172. this.column = temp;
  173. }
  174. public String getProperty() {
  175. return property;
  176. }
  177. public void setProperty(String property) {
  178. this.property = property;
  179. }
  180. public String getEl() {
  181. return el;
  182. }
  183. public void setEl(String el) {
  184. this.el = el;
  185. }
  186. public FieldStrategy getFieldStrategy() {
  187. return fieldStrategy;
  188. }
  189. public void setFieldStrategy(FieldStrategy fieldStrategy) {
  190. this.fieldStrategy = fieldStrategy;
  191. }
  192. public String getPropertyType() {
  193. return propertyType;
  194. }
  195. public void setPropertyType(String propertyType) {
  196. this.propertyType = propertyType;
  197. }
  198. /**
  199. * 是否开启逻辑删除
  200. */
  201. public boolean isLogicDelete() {
  202. return StringUtils.isNotEmpty(logicDeleteValue);
  203. }
  204. public String getLogicDeleteValue() {
  205. return logicDeleteValue;
  206. }
  207. public void setLogicDeleteValue(String logicDeleteValue) {
  208. this.logicDeleteValue = logicDeleteValue;
  209. }
  210. public String getLogicNotDeleteValue() {
  211. return logicNotDeleteValue;
  212. }
  213. public void setLogicNotDeleteValue(String logicNotDeleteValue) {
  214. this.logicNotDeleteValue = logicNotDeleteValue;
  215. }
  216. public FieldIgnore getFieldIgnore() {
  217. return fieldIgnore;
  218. }
  219. public void setFieldIgnore(FieldIgnore fieldIgnore) {
  220. this.fieldIgnore = fieldIgnore;
  221. }
  222. }