EntityWrapper.java 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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.mapper;
  17. import com.baomidou.mybatisplus.toolkit.StringUtils;
  18. /**
  19. * <p>
  20. * Entity 对象封装操作类,定义T-SQL语法
  21. * </p>
  22. *
  23. * @author hubin , yanghu , Dyang , Caratacus
  24. * @Date 2016-11-7
  25. */
  26. @SuppressWarnings("serial")
  27. public class EntityWrapper<T> extends Wrapper<T> {
  28. /**
  29. * 数据库表映射实体类
  30. */
  31. protected T entity = null;
  32. public EntityWrapper() {
  33. /* 注意,传入查询参数 */
  34. }
  35. public EntityWrapper(T entity) {
  36. this.entity = entity;
  37. }
  38. public EntityWrapper(T entity, String sqlSelect) {
  39. this.entity = entity;
  40. this.sqlSelect = sqlSelect;
  41. }
  42. public T getEntity() {
  43. return entity;
  44. }
  45. public void setEntity(T entity) {
  46. this.entity = entity;
  47. }
  48. /**
  49. * SQL 片段
  50. */
  51. @Override
  52. public String getSqlSegment() {
  53. /*
  54. * 无条件
  55. */
  56. String sqlWhere = sql.toString();
  57. if (StringUtils.isEmpty(sqlWhere)) {
  58. return null;
  59. }
  60. /*
  61. * 根据当前实体判断是否需要将WHERE替换成 AND 增加实体不为空但所有属性为空的情况
  62. */
  63. return isWhere != null ? (isWhere ? sqlWhere : sqlWhere.replaceFirst("WHERE", AND_OR)) : sqlWhere.replaceFirst("WHERE", AND_OR);
  64. }
  65. }