Kaynağa Gözat

1、将查询参数封装为Condition对象,初步测试未发现问题
2、Page中的orderByField与isAsc优先级最高
3、sqlSegment为空时加入1=1,避免出错情况
4、Condition由于参数较多,为提供构造函数,是否提供有待讨论

D.Yang 9 yıl önce
ebeveyn
işleme
015c062347

+ 8 - 17
mybatis-plus/src/main/java/com/baomidou/framework/service/IService.java

@@ -18,6 +18,7 @@ package com.baomidou.framework.service;
 import java.util.List;
 import java.util.Map;
 
+import com.baomidou.mybatisplus.mapper.Condition;
 import com.baomidou.mybatisplus.mapper.EntityWrapper;
 import com.baomidou.mybatisplus.plugins.Page;
 
@@ -226,18 +227,12 @@ public interface IService<T, I> {
 	 * <p>
 	 * 查询列表
 	 * </p>
-	 * 
-	 * @param entity
-	 *            实体对象
-	 * @param sqlSelect
-	 *            SQL 查询字段内容,例如:id,name,age
-	 * @param sqlSegment
-	 *            SQL 片段
-	 * @param orderByField
-	 *            对应 EntityWrapper 类中 orderByField 字段 {@link EntityWrapper}
+	 *
+	 * @param condition
+	 *            SQL 查询条件,详见Condition类 {@link Condition}
 	 * @return
 	 */
-	List<T> selectList(T entity, String sqlSelect, String sqlSegment, String orderByField);
+	List<T> selectList(Condition<T> condition);
 
 	/**
 	 * <p>
@@ -246,14 +241,10 @@ public interface IService<T, I> {
 	 * 
 	 * @param page
 	 *            翻页对象
-	 * @param sqlSelect
-	 *            SQL 查询字段内容,例如:id,name,age
-	 * @param entity
-	 *            实体对象
-	 * @param sqlSegment
-	 *            SQL 片段
+	 * @param condition
+	 *            SQL 查询条件,详见Condition类 {@link Condition}
 	 * @return
 	 */
-	Page<T> selectPage(Page<T> page, String sqlSelect, T entity, String sqlSegment);
+	Page<T> selectPage(Page<T> page, Condition<T> condition);
 
 }

+ 7 - 22
mybatis-plus/src/main/java/com/baomidou/framework/service/impl/ServiceImpl.java

@@ -18,6 +18,7 @@ package com.baomidou.framework.service.impl;
 import java.util.List;
 import java.util.Map;
 
+import com.baomidou.mybatisplus.mapper.Condition;
 import org.springframework.beans.factory.annotation.Autowired;
 
 import com.baomidou.framework.service.IService;
@@ -117,31 +118,15 @@ public class ServiceImpl<M extends BaseMapper<T, I>, T, I> implements IService<T
 		return baseMapper.selectCount(entity);
 	}
 
-	public List<T> selectList(T entity, String sqlSelect, String sqlSegment, String orderByField) {
-		return baseMapper.selectList(new EntityWrapper<T>(entity, sqlSelect, this.convertSqlSegmet(sqlSegment, orderByField, true)));
+	public List<T> selectList(Condition<T> condition) {
+		return baseMapper.selectList(condition.getEntityWrapper());
 	}
 
-	public Page<T> selectPage(Page<T> page, String sqlSelect, T entity, String sqlSegment) {
-		EntityWrapper<T> ew = new EntityWrapper<T>(entity, sqlSelect, this.convertSqlSegmet(page.getOrderByField(), sqlSegment, page.isAsc()));
-		page.setRecords(baseMapper.selectPage(page, ew));
+	public Page<T> selectPage(Page<T> page, Condition<T> condition) {
+		condition.setOrderByField(page.getOrderByField());
+		condition.setAsc(page.isAsc());
+		page.setRecords(baseMapper.selectPage(page, condition.getEntityWrapper()));
 		return page;
 	}
 
-	/**
-	 * 转换 SQL 片段 + 排序
-	 */
-	protected String convertSqlSegmet(String sqlSegment, String orderByField, boolean isAsc) {
-		StringBuffer segment = new StringBuffer();
-		if (null != sqlSegment) {
-			segment.append(sqlSegment);
-		}
-		if (null != orderByField) {
-			segment.append(" ORDER BY ").append(orderByField);
-			if (!isAsc) {
-				segment.append(" DESC");
-			}
-		}
-		return segment.toString();
-	}
-
 }

+ 131 - 0
mybatis-plus/src/main/java/com/baomidou/mybatisplus/mapper/Condition.java

@@ -0,0 +1,131 @@
+/**
+ * Copyright (c) 2011-2014, hubin (jobob@qq.com).
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.baomidou.mybatisplus.mapper;
+
+import com.baomidou.mybatisplus.toolkit.StringUtils;
+
+/**
+ * 查询条件
+ *
+ * @author hubin
+ * @Date 2016-08-18
+ */
+public class Condition<T> {
+    /**
+     * 根据实体类查询
+     */
+    private T whereEntity = null;
+    /**
+     * 需要查询的参数
+     */
+    private String sqlSelect = null;
+    /**
+     * 附加Sql片段,实现自定义查询
+     */
+    private String sqlSegment = null;
+    /**
+     * <p>
+     * SQL 排序 ORDER BY 字段,例如: id DESC(根据id倒序查询)
+     * </p>
+     * <p>
+     * DESC 表示按倒序排序(即:从大到小排序)<br>
+     * ASC 表示按正序排序(即:从小到大排序)
+     * </p>
+     */
+    private String orderByField = null;
+    /**
+     * 是否为升序 ASC( 默认: true )
+     */
+    private boolean isAsc = true;
+    /**
+     * 封装EntityWrapper
+     */
+    private EntityWrapper<T> entityWrapper = null;
+
+    public T getWhereEntity() {
+        return whereEntity;
+    }
+
+    public void setWhereEntity(T whereEntity) {
+        this.whereEntity = whereEntity;
+    }
+
+    public String getSqlSelect() {
+        return sqlSelect;
+    }
+
+    public void setSqlSelect(String sqlSelect) {
+        if (StringUtils.isNotEmpty(sqlSelect)) {
+            this.sqlSelect = sqlSelect;
+        }
+    }
+
+    public String getSqlSegment() {
+        return sqlSegment;
+    }
+
+    public void setSqlSegment(String sqlSegment) {
+        if (StringUtils.isNotEmpty(sqlSegment)) {
+            this.sqlSegment = sqlSegment;
+        }
+    }
+
+    public String getOrderByField() {
+        return orderByField;
+    }
+
+    public void setOrderByField(String orderByField) {
+        if (StringUtils.isNotEmpty(orderByField)) {
+            this.orderByField = orderByField;
+        }
+    }
+
+    public boolean isAsc() {
+        return isAsc;
+    }
+
+    public void setAsc(boolean asc) {
+        isAsc = asc;
+    }
+
+    public EntityWrapper<T> getEntityWrapper() {
+        return new EntityWrapper<T>(whereEntity, sqlSelect, convertSqlSegment(sqlSegment, orderByField, isAsc));
+    }
+
+    public void setEntityWrapper(EntityWrapper<T> entityWrapper) {
+        this.entityWrapper = entityWrapper;
+    }
+
+    /**
+     * 转换 SQL 片段 + 排序
+     */
+    private String convertSqlSegment(String sqlSegment, String orderByField, boolean isAsc) {
+        StringBuffer segment = new StringBuffer();
+        if (StringUtils.isNotEmpty(sqlSegment)) {
+            segment.append(sqlSegment);
+        } else {
+            segment.append(" 1=1 ");
+        }
+        if (StringUtils.isNotEmpty(orderByField)) {
+            segment.append(" ORDER BY ").append(orderByField);
+            if (!isAsc) {
+                segment.append(" DESC");
+            }
+        }
+        return segment.toString();
+    }
+
+}