浏览代码

Optimize Wrapper

Caratacus 8 年之前
父节点
当前提交
9011d849f5

+ 3 - 0
mybatis-plus/src/main/java/com/baomidou/mybatisplus/mapper/SqlHelper.java

@@ -166,6 +166,9 @@ public class SqlHelper {
 	 * @param wrapper
 	 */
 	public static void fillWrapper(Page<?> page, Wrapper<?> wrapper) {
+		if (null == page) {
+			return;
+		}
 		if (null != wrapper) {
 			if (page.isOpenSort()) {
 				wrapper.orderBy(page.getOrderByField(), page.isAsc());

+ 32 - 4
mybatis-plus/src/main/java/com/baomidou/mybatisplus/mapper/Wrapper.java

@@ -16,6 +16,7 @@
 package com.baomidou.mybatisplus.mapper;
 
 import com.baomidou.mybatisplus.enums.SqlLike;
+import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
 import com.baomidou.mybatisplus.toolkit.ArrayUtils;
 import com.baomidou.mybatisplus.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.toolkit.MapUtils;
@@ -44,16 +45,19 @@ public abstract class Wrapper<T> implements Serializable {
 	/**
 	 * 占位符
 	 */
-	public static final String PLACE_HOLDER = "{%s}";
+	private static final String PLACE_HOLDER = "{%s}";
 
-	public static final String MYBATIS_PLUS_TOKEN = "#{ew.paramNameValuePairs.%s}";
+	private static final String MYBATIS_PLUS_TOKEN = "#{%s.paramNameValuePairs.%s}";
 
 	private static final String MP_GENERAL_PARAMNAME = "MPGENVAL";
 
+	private static final String DEFAULT_PARAM_ALIAS = "ew";
+
 	private Map<String, Object> paramNameValuePairs = new HashMap<String, Object>(4);
 
 	private AtomicInteger paramNameSeq = new AtomicInteger(0);
 
+	protected String paramAlias = null;
 	/**
 	 * SQL 查询字段内容,例如:id,name,age
 	 */
@@ -105,7 +109,7 @@ public abstract class Wrapper<T> implements Serializable {
 	public String toString() {
 		String sqlSegment = getSqlSegment();
 		if (StringUtils.isNotEmpty(sqlSegment)) {
-			sqlSegment = sqlSegment.replaceAll("#\\{ew.paramNameValuePairs.MPGENVAL[0-9]+\\}", "\\?");
+			sqlSegment = sqlSegment.replaceAll("#\\{" + getParamAlias() + ".paramNameValuePairs.MPGENVAL[0-9]+\\}", "\\?");
 		}
 		return sqlSegment;
 	}
@@ -774,7 +778,8 @@ public abstract class Wrapper<T> implements Serializable {
 		if (ArrayUtils.isNotEmpty(params)) {
 			for (int i = 0; i < params.length; ++i) {
 				String genParamName = MP_GENERAL_PARAMNAME + paramNameSeq.incrementAndGet();
-				sqlStr = sqlStr.replace(String.format(PLACE_HOLDER, i), String.format(MYBATIS_PLUS_TOKEN, genParamName));
+				sqlStr = sqlStr.replace(String.format(PLACE_HOLDER, i),
+						String.format(MYBATIS_PLUS_TOKEN, getParamAlias(), genParamName));
 				paramNameValuePairs.put(genParamName, params[i]);
 			}
 		}
@@ -803,4 +808,27 @@ public abstract class Wrapper<T> implements Serializable {
 	public Map<String, Object> getParamNameValuePairs() {
 		return paramNameValuePairs;
 	}
+
+	public String getParamAlias() {
+		return StringUtils.isEmpty(paramAlias) ? DEFAULT_PARAM_ALIAS : paramAlias;
+	}
+
+	/**
+	 * <p>
+	 * 调用该方法时 应当在吃初始化时优先设置该值 不要重复设置该值 要不然我就给你抛异常了
+	 * </p>
+	 * 
+	 * @param paramAlias
+	 * @return
+	 */
+	public Wrapper<T> setParamAlias(String paramAlias) {
+		if (StringUtils.isNotEmpty(getSqlSegment())) {
+			throw new MybatisPlusException("Error: Please call this method when initializing!");
+		}
+		if (StringUtils.isNotEmpty(this.paramAlias)) {
+			throw new MybatisPlusException("Error: Please do not call the method repeatedly!");
+		}
+		this.paramAlias = paramAlias;
+		return this;
+	}
 }

+ 2 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/EntityWrapperTest.java

@@ -333,9 +333,11 @@ public class EntityWrapperTest {
 		 * 实体带where ifneed
 		 */
 		ew.setEntity(new User(1));
+		ew.setParamAlias("ceshi");
 		String sqlPart = ew.or("sql = {0}", "sql").like("default", "default", SqlLike.DEFAULT).like("left", "left", SqlLike.LEFT)
 				.like("right", "right", SqlLike.RIGHT).isWhere(true).eq("bool", true).toString();
 		System.out.println("sql ==> " + sqlPart);
 		Assert.assertEquals("WHERE (sql = ? AND default = ? AND left = ? AND right = ? AND bool = ?)", sqlPart);
+		System.out.println(ew.getSqlSegment());
 	}
 }