MetaObjectHandler.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * Copyright (c) 2011-2020, 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.mapper;
  17. import org.apache.ibatis.reflection.MetaObject;
  18. /**
  19. * <p>
  20. * 元对象字段填充控制器抽象类,实现公共字段自动写入
  21. * </p>
  22. *
  23. * @author hubin
  24. * @Date 2016-08-28
  25. */
  26. public abstract class MetaObjectHandler {
  27. protected static final String META_OBJ_PREFIX = "et";
  28. /**
  29. * <p>
  30. * 插入元对象字段填充
  31. * </p>
  32. *
  33. * @param metaObject 元对象
  34. */
  35. public abstract void insertFill(MetaObject metaObject);
  36. /**
  37. * 更新元对象字段填充(用于更新时对公共字段的填充)
  38. * Created with IntelliJ IDEA.
  39. * Author: Wu Yujie
  40. * Email: coffee377@dingtalk.com
  41. * Time: 2017/04/16 15:03
  42. *
  43. * @param metaObject 元对象
  44. */
  45. public abstract void updateFill(MetaObject metaObject);
  46. /**
  47. * <p>
  48. * Common method to set value for java bean.
  49. * </p>
  50. * <p>
  51. * 如果包含前缀 et 使用该方法,否则可以直接 metaObject.setValue(fieldName, fieldVal);
  52. * </p>
  53. *
  54. * @param fieldName java bean property name
  55. * @param fieldVal java bean property value
  56. * @param metaObject meta object parameter
  57. */
  58. public MetaObjectHandler setFieldValByName(String fieldName, Object fieldVal, MetaObject metaObject) {
  59. if (metaObject.hasGetter(fieldName)) {
  60. metaObject.setValue(fieldName, fieldVal);
  61. } else if (metaObject.hasGetter(META_OBJ_PREFIX + "." + fieldName)) {
  62. metaObject.setValue(META_OBJ_PREFIX + "." + fieldName, fieldVal);
  63. }
  64. return this;
  65. }
  66. /**
  67. * <p>
  68. * get value from java bean by propertyName
  69. * </p>
  70. * <p>
  71. * 如果包含前缀 et 使用该方法,否则可以直接 metaObject.setValue(fieldName, fieldVal);
  72. * </p>
  73. *
  74. * @param fieldName java bean property name
  75. * @param metaObject parameter wrapper
  76. * @return
  77. */
  78. public Object getFieldValByName(String fieldName, MetaObject metaObject) {
  79. if (metaObject.hasGetter(fieldName)) {
  80. return metaObject.getValue(fieldName);
  81. } else if (metaObject.hasGetter(META_OBJ_PREFIX + "." + fieldName)) {
  82. return metaObject.getValue(META_OBJ_PREFIX + "." + fieldName);
  83. }
  84. return null;
  85. }
  86. /**
  87. * 开启插入填充
  88. */
  89. public boolean openInsertFill() {
  90. return true;
  91. }
  92. /**
  93. * 开启更新填充
  94. */
  95. public boolean openUpdateFill() {
  96. return true;
  97. }
  98. }