EntityWrapper.java 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. @Override
  43. public T getEntity() {
  44. return entity;
  45. }
  46. public void setEntity(T entity) {
  47. this.entity = entity;
  48. }
  49. /**
  50. * SQL 片段
  51. */
  52. @Override
  53. public String getSqlSegment() {
  54. /*
  55. * 无条件
  56. */
  57. String sqlWhere = sql.toString();
  58. if (StringUtils.isEmpty(sqlWhere)) {
  59. return null;
  60. }
  61. /*
  62. * 根据当前实体判断是否需要将WHERE替换成 AND 增加实体不为空但所有属性为空的情况
  63. */
  64. return isWhere != null ? (isWhere ? sqlWhere : sqlWhere.replaceFirst("WHERE", AND_OR)) : sqlWhere.replaceFirst("WHERE", AND_OR);
  65. }
  66. }