TableField.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (c) 2011-2024, baomidou (jobob@qq.com).
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.baomidou.mybatisplus.annotation;
  17. import org.apache.ibatis.mapping.ParameterMapping;
  18. import org.apache.ibatis.mapping.ResultMapping;
  19. import org.apache.ibatis.type.JdbcType;
  20. import org.apache.ibatis.type.TypeHandler;
  21. import org.apache.ibatis.type.UnknownTypeHandler;
  22. import java.lang.annotation.*;
  23. /**
  24. * 表字段标识
  25. *
  26. * @author hubin sjy tantan
  27. * @since 2016-09-09
  28. */
  29. @Documented
  30. @Retention(RetentionPolicy.RUNTIME)
  31. @Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
  32. public @interface TableField {
  33. /**
  34. * 数据库字段值
  35. * <p>
  36. * 不需要配置该值的情况:
  37. * <li> 当 {@link com.baomidou.mybatisplus.core.MybatisConfiguration#mapUnderscoreToCamelCase} 为 true 时,
  38. * (mp下默认是true,mybatis默认是false), 数据库字段值.replace("_","").toUpperCase() == 实体属性名.toUpperCase() </li>
  39. * <li> 当 {@link com.baomidou.mybatisplus.core.MybatisConfiguration#mapUnderscoreToCamelCase} 为 false 时,
  40. * 数据库字段值.toUpperCase() == 实体属性名.toUpperCase() </li>
  41. */
  42. String value() default "";
  43. /**
  44. * 是否为数据库表字段
  45. * <p>
  46. * 默认 true 存在,false 不存在
  47. */
  48. boolean exist() default true;
  49. /**
  50. * 字段 where 实体查询比较条件
  51. * <p>
  52. * 默认 {@link SqlCondition#EQUAL}
  53. */
  54. String condition() default "";
  55. /**
  56. * 字段 update set 部分注入, 该注解优于 el 注解使用
  57. * <p>
  58. * 例1:@TableField(.. , update="%s+1") 其中 %s 会填充为字段
  59. * 输出 SQL 为:update 表 set 字段=字段+1 where ...
  60. * <p>
  61. * 例2:@TableField(.. , update="now()") 使用数据库时间
  62. * 输出 SQL 为:update 表 set 字段=now() where ...
  63. */
  64. String update() default "";
  65. /**
  66. * 字段验证策略之 insert: 当insert操作时,该字段拼接insert语句时的策略
  67. * <p>
  68. * IGNORED: 直接拼接 insert into table_a(column) values (#{columnProperty});
  69. * NOT_NULL: insert into table_a(<if test="columnProperty != null">column</if>) values (<if test="columnProperty != null">#{columnProperty}</if>)
  70. * NOT_EMPTY: insert into table_a(<if test="columnProperty != null and columnProperty!=''">column</if>) values (<if test="columnProperty != null and columnProperty!=''">#{columnProperty}</if>)
  71. * NOT_EMPTY 如果针对的是非 CharSequence 类型的字段则效果等于 NOT_NULL
  72. *
  73. * @since 3.1.2
  74. */
  75. FieldStrategy insertStrategy() default FieldStrategy.DEFAULT;
  76. /**
  77. * 字段验证策略之 update: 当更新操作时,该字段拼接set语句时的策略
  78. * <p>
  79. * IGNORED: 直接拼接 update table_a set column=#{columnProperty}, 属性为null/空string都会被set进去
  80. * NOT_NULL: update table_a set <if test="columnProperty != null">column=#{columnProperty}</if>
  81. * NOT_EMPTY: update table_a set <if test="columnProperty != null and columnProperty!=''">column=#{columnProperty}</if>
  82. * NOT_EMPTY 如果针对的是非 CharSequence 类型的字段则效果等于 NOT_NULL
  83. *
  84. * @since 3.1.2
  85. */
  86. FieldStrategy updateStrategy() default FieldStrategy.DEFAULT;
  87. /**
  88. * 字段验证策略之 where: 表示该字段在拼接where条件时的策略
  89. * <p>
  90. * IGNORED: 直接拼接 column=#{columnProperty}
  91. * NOT_NULL: <if test="columnProperty != null">column=#{columnProperty}</if>
  92. * NOT_EMPTY: <if test="columnProperty != null and columnProperty!=''">column=#{columnProperty}</if>
  93. * NOT_EMPTY 如果针对的是非 CharSequence 类型的字段则效果等于 NOT_NULL
  94. *
  95. * @since 3.1.2
  96. */
  97. FieldStrategy whereStrategy() default FieldStrategy.DEFAULT;
  98. /**
  99. * 字段自动填充策略
  100. * <p>
  101. * 在对应模式下将会忽略 insertStrategy 或 updateStrategy 的配置,等于断言该字段必有值
  102. */
  103. FieldFill fill() default FieldFill.DEFAULT;
  104. /**
  105. * 是否进行 select 查询
  106. * <p>
  107. * 大字段可设置为 false 不加入 select 查询范围
  108. */
  109. boolean select() default true;
  110. /**
  111. * 是否保持使用全局的 columnFormat 的值
  112. * <p>
  113. * 只生效于 既设置了全局的 columnFormat 也设置了上面 {@link #value()} 的值
  114. * 如果是 false , 全局的 columnFormat 不生效
  115. *
  116. * @since 3.1.1
  117. */
  118. boolean keepGlobalFormat() default false;
  119. /**
  120. * {@link ResultMapping#property} and {@link ParameterMapping#property}
  121. *
  122. * @since 3.4.4
  123. */
  124. String property() default "";
  125. /**
  126. * JDBC类型 (该默认值不代表会按照该值生效),
  127. * 只生效于 mp 自动注入的 method,
  128. * 建议配合 {@link TableName#autoResultMap()} 一起使用
  129. * <p>
  130. * {@link ResultMapping#jdbcType} and {@link ParameterMapping#jdbcType}
  131. *
  132. * @since 3.1.2
  133. */
  134. JdbcType jdbcType() default JdbcType.UNDEFINED;
  135. /**
  136. * 类型处理器 (该默认值不代表会按照该值生效),
  137. * 只生效于 mp 自动注入的 method,
  138. * 建议配合 {@link TableName#autoResultMap()} 一起使用
  139. * <p>
  140. * {@link ResultMapping#typeHandler} and {@link ParameterMapping#typeHandler}
  141. *
  142. * @since 3.1.2
  143. */
  144. Class<? extends TypeHandler> typeHandler() default UnknownTypeHandler.class;
  145. /**
  146. * 只在使用了 {@link #typeHandler()} 时判断是否辅助追加 javaType
  147. * <p>
  148. * 一般情况下不推荐使用
  149. * {@link ParameterMapping#javaType}
  150. *
  151. * @since 3.4.0 @2020-07-23
  152. */
  153. boolean javaType() default false;
  154. /**
  155. * 指定小数点后保留的位数,
  156. * 只生效于 mp 自动注入的 method,
  157. * 建议配合 {@link TableName#autoResultMap()} 一起使用
  158. * <p>
  159. * {@link ParameterMapping#numericScale}
  160. *
  161. * @since 3.1.2
  162. */
  163. String numericScale() default "";
  164. }