瀏覽代碼

瑕疵修复

Caratacus 8 年之前
父節點
當前提交
f82c3be219

+ 5 - 4
mybatis-plus/src/main/java/com/baomidou/mybatisplus/MybatisConfiguration.java

@@ -122,10 +122,6 @@ public class MybatisConfiguration extends Configuration {
 		}
 		super.setDefaultScriptingLanguage(driver);
 	}
-	@Override
-	public <T> void addMapper(Class<T> type) {
-		mybatisPulsMapperRegistry.addMapper(type);
-	}
 
 	/**
 	 * Mapper注册
@@ -135,6 +131,11 @@ public class MybatisConfiguration extends Configuration {
 		return mybatisPulsMapperRegistry;
 	}
 
+	@Override
+	public <T> void addMapper(Class<T> type) {
+		mybatisPulsMapperRegistry.addMapper(type);
+	}
+
 	@Override
 	public void addMappers(String packageName, Class<?> superType) {
 		mybatisPulsMapperRegistry.addMappers(packageName, superType);

+ 50 - 67
mybatis-plus/src/main/java/com/baomidou/mybatisplus/MybatisPulsMapperRegistry.java

@@ -32,84 +32,67 @@ import com.baomidou.mybatisplus.MybatisPlusMapperBuilder;
 
 /**
  * 继承至MapperRegistry
+ * 
  * @author Clinton Begin
  * @author Eduardo Macarron
  * @author Lasse Voss
  */
 public class MybatisPulsMapperRegistry extends MapperRegistry {
 
-  private final Configuration config;
-  private final Map<Class<?>, MapperProxyFactory<?>> knownMappers = new HashMap<Class<?>, MapperProxyFactory<?>>();
+	private final Configuration config;
+	private final Map<Class<?>, MapperProxyFactory<?>> knownMappers = new HashMap<Class<?>, MapperProxyFactory<?>>();
 
-  public MybatisPulsMapperRegistry(Configuration config) {
-    super(config);
-    this.config = config;
-  }
+	public MybatisPulsMapperRegistry(Configuration config) {
+		super(config);
+		this.config = config;
+	}
 
-  @SuppressWarnings("unchecked")
-  public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
-    final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
-    if (mapperProxyFactory == null) {
-      throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
-    }
-    try {
-      return mapperProxyFactory.newInstance(sqlSession);
-    } catch (Exception e) {
-      throw new BindingException("Error getting mapper instance. Cause: " + e, e);
-    }
-  }
+	@SuppressWarnings("unchecked")
+	public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
+		final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
+		if (mapperProxyFactory == null) {
+			throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
+		}
+		try {
+			return mapperProxyFactory.newInstance(sqlSession);
+		} catch (Exception e) {
+			throw new BindingException("Error getting mapper instance. Cause: " + e, e);
+		}
+	}
 
-  public <T> boolean hasMapper(Class<T> type) {
-    return knownMappers.containsKey(type);
-  }
+	public <T> boolean hasMapper(Class<T> type) {
+		return knownMappers.containsKey(type);
+	}
 
-  public <T> void addMapper(Class<T> type) {
-    if (type.isInterface()) {
-      if (hasMapper(type)) {
-        throw new BindingException("Type " + type + " is already known to the MapperRegistry.");
-      }
-      boolean loadCompleted = false;
-      try {
-        knownMappers.put(type, new MapperProxyFactory<T>(type));
-        // It's important that the type is added before the parser is run
-        // otherwise the binding may automatically be attempted by the
-        // mapper parser. If the type is already known, it won't try.
+	public <T> void addMapper(Class<T> type) {
+		if (type.isInterface()) {
+			if (hasMapper(type)) {
+				throw new BindingException("Type " + type + " is already known to the MapperRegistry.");
+			}
+			boolean loadCompleted = false;
+			try {
+				knownMappers.put(type, new MapperProxyFactory<T>(type));
+				// It's important that the type is added before the parser is
+				// run
+				// otherwise the binding may automatically be attempted by the
+				// mapper parser. If the type is already known, it won't try.
 
-        MybatisPlusMapperBuilder parser = new MybatisPlusMapperBuilder(config, type);
-        parser.parse();
-        loadCompleted = true;
-      } finally {
-        if (!loadCompleted) {
-          knownMappers.remove(type);
-        }
-      }
-    }
-  }
+				MybatisPlusMapperBuilder parser = new MybatisPlusMapperBuilder(config, type);
+				parser.parse();
+				loadCompleted = true;
+			} finally {
+				if (!loadCompleted) {
+					knownMappers.remove(type);
+				}
+			}
+		}
+	}
 
-  /**
-   * @since 3.2.2
-   */
-  public Collection<Class<?>> getMappers() {
-    return Collections.unmodifiableCollection(knownMappers.keySet());
-  }
-
-  /**
-   * @since 3.2.2
-   */
-  public void addMappers(String packageName, Class<?> superType) {
-    ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<Class<?>>();
-    resolverUtil.find(new ResolverUtil.IsA(superType), packageName);
-    Set<Class<? extends Class<?>>> mapperSet = resolverUtil.getClasses();
-    for (Class<?> mapperClass : mapperSet) {
-      addMapper(mapperClass);
-    }
-  }
-
-  /**
-   * @since 3.2.2
-   */
-  public void addMappers(String packageName) {
-    addMappers(packageName, Object.class);
-  }
+	/**
+	 * @since 3.2.2
+	 */
+	public Collection<Class<?>> getMappers() {
+		return Collections.unmodifiableCollection(knownMappers.keySet());
+	}
 
 }

+ 68 - 66
mybatis-plus/src/main/java/com/baomidou/mybatisplus/mapper/AutoSqlInjector.java

@@ -57,7 +57,7 @@ public class AutoSqlInjector implements ISqlInjector {
 	protected LanguageDriver languageDriver;
 
 	protected MapperBuilderAssistant builderAssistant;
-	
+
 	protected DBType dbType = DBType.MYSQL;
 
 	/**
@@ -74,7 +74,7 @@ public class AutoSqlInjector implements ISqlInjector {
 			inject(configuration, builderAssistant, mapperClass);
 			mapperRegistryCache.add(className);
 		}
-	};
+	}
 
 	/**
 	 * 注入单点 crudSql
@@ -117,23 +117,24 @@ public class AutoSqlInjector implements ISqlInjector {
 			this.injectSelectCountSql(mapperClass, modelClass, table);
 			this.injectSelectListSql(SqlMethod.SELECT_LIST, mapperClass, modelClass, table);
 			this.injectSelectListSql(SqlMethod.SELECT_PAGE, mapperClass, modelClass, table);
-			
+
 			/* 自定义方法 */
 			this.inject(configuration, builderAssistant, mapperClass, modelClass, table);
 		} else {
 			/**
 			 * 警告
 			 */
-			logger.warning(String.format("%s ,Not found @TableId annotation, cannot use mybatis-plus curd method.", modelClass.toString()));
+			logger.warning(String.format("%s ,Not found @TableId annotation, cannot use mybatis-plus curd method.",
+					modelClass.toString()));
 		}
 	}
-	
+
 	/**
 	 * 自定义方法,注入点(子类需重写该方法)
 	 */
 	public void inject(Configuration configuration, MapperBuilderAssistant builderAssistant, Class<?> mapperClass,
 			Class<?> modelClass, TableInfo table) {
-		//to do nothing
+		// to do nothing
 	}
 
 	protected Class<?> extractModelClass(Class<?> mapperClass) {
@@ -149,14 +150,14 @@ public class AutoSqlInjector implements ISqlInjector {
 		Class<?> modelClass = (Class<?>) parameters[0];
 		return modelClass;
 	}
-	
+
 	/**
 	 * <p>
 	 * 注入插入 SQL 语句
 	 * </p>
 	 * 
 	 * @param selective
-	 * 				是否选择插入
+	 *            是否选择插入
 	 * @param mapperClass
 	 * @param modelClass
 	 * @param table
@@ -204,10 +205,11 @@ public class AutoSqlInjector implements ISqlInjector {
 		}
 		fieldBuilder.append("\n</trim>");
 		placeholderBuilder.append("\n</trim>");
-		String sql = String.format(sqlMethod.getSql(), table.getTableName(), fieldBuilder.toString(), placeholderBuilder.toString());
+		String sql = String.format(sqlMethod.getSql(), table.getTableName(), fieldBuilder.toString(),
+				placeholderBuilder.toString());
 		SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
-		this.addInsertMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource, keyGenerator,
-			keyProperty, keyColumn);
+		this.addInsertMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource, keyGenerator, keyProperty,
+				keyColumn);
 	}
 
 	/**
@@ -250,10 +252,11 @@ public class AutoSqlInjector implements ISqlInjector {
 		}
 		fieldBuilder.append("\n</trim>");
 		placeholderBuilder.append("\n</trim>");
-		String sql = String.format(sqlMethod.getSql(), table.getTableName(), fieldBuilder.toString(), placeholderBuilder.toString());
+		String sql = String.format(sqlMethod.getSql(), table.getTableName(), fieldBuilder.toString(),
+				placeholderBuilder.toString());
 		SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
-		this.addInsertMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource, keyGenerator,
-				keyProperty, keyColumn);
+		this.addInsertMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource, keyGenerator, keyProperty,
+				keyColumn);
 	}
 
 	/**
@@ -271,7 +274,7 @@ public class AutoSqlInjector implements ISqlInjector {
 		SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
 		this.addDeleteMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource);
 	}
-	
+
 	/**
 	 * <p>
 	 * 注入 map 条件删除 SQL 语句
@@ -322,23 +325,22 @@ public class AutoSqlInjector implements ISqlInjector {
 	 * </p>
 	 * 
 	 * @param selective
-	 * 				是否选择更新
+	 *            是否选择更新
 	 * @param mapperClass
 	 * @param modelClass
 	 * @param table
 	 */
-	protected void injectUpdateByIdSql( boolean selective, Class<?> mapperClass, Class<?> modelClass, TableInfo table ) {
+	protected void injectUpdateByIdSql(boolean selective, Class<?> mapperClass, Class<?> modelClass, TableInfo table) {
 		SqlMethod sqlMethod = SqlMethod.UPDATE_BY_ID;
-		if ( selective ) {
+		if (selective) {
 			sqlMethod = SqlMethod.UPDATE_SELECTIVE_BY_ID;
 		}
-		String sql = String.format(sqlMethod.getSql(), table.getTableName(), sqlSet(selective, table),
-			table.getKeyColumn(), table.getKeyProperty());
+		String sql = String.format(sqlMethod.getSql(), table.getTableName(), sqlSet(selective, table), table.getKeyColumn(),
+				table.getKeyProperty());
 		SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
 		this.addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource);
 	}
 
-
 	/**
 	 * <p>
 	 * 注入批量更新 SQL 语句
@@ -369,7 +371,7 @@ public class AutoSqlInjector implements ISqlInjector {
 				set.append("} THEN #{i.").append(fieldInfo.getEl()).append("}");
 				set.append(convertIfTag(fieldInfo, true));
 				set.append("\n</foreach>");
-				set.append("\n</trim>");			
+				set.append("\n</trim>");
 			}
 		}
 		set.append("\n</trim>");
@@ -378,28 +380,27 @@ public class AutoSqlInjector implements ISqlInjector {
 		SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
 		this.addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource);
 	}
-	
+
 	/**
 	 * <p>
 	 * 注入批量更新 SQL 语句
 	 * </p>
 	 * 
 	 * @param selective
-	 * 				是否选择更新
+	 *            是否选择更新
 	 * @param mapperClass
 	 * @param modelClass
 	 * @param table
 	 */
-	protected void injectUpdateSql( boolean selective, Class<?> mapperClass, Class<?> modelClass, TableInfo table ) {
+	protected void injectUpdateSql(boolean selective, Class<?> mapperClass, Class<?> modelClass, TableInfo table) {
 		SqlMethod sqlMethod = SqlMethod.UPDATE;
-		if ( selective ) {
+		if (selective) {
 			sqlMethod = SqlMethod.UPDATE_SELECTIVE;
 		}
 		String sql = String.format(sqlMethod.getSql(), table.getTableName(), sqlSet(selective, table), sqlWhere(table, true));
 		SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
 		this.addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource);
 	}
-	
 
 	/**
 	 * <p>
@@ -429,7 +430,7 @@ public class AutoSqlInjector implements ISqlInjector {
 		}
 		this.addSelectMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource, modelClass, table);
 	}
-	
+
 	/**
 	 * <p>
 	 * 注入 map 查询 SQL 语句
@@ -455,13 +456,14 @@ public class AutoSqlInjector implements ISqlInjector {
 	 * @param modelClass
 	 * @param table
 	 */
-	protected void injectSelectOneSql( Class<?> mapperClass, Class<?> modelClass, TableInfo table ) {
+	protected void injectSelectOneSql(Class<?> mapperClass, Class<?> modelClass, TableInfo table) {
 		SqlMethod sqlMethod = SqlMethod.SELECT_ONE;
-		String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(table, false), table.getTableName(), sqlWhere(table, false));
+		String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(table, false), table.getTableName(),
+				sqlWhere(table, false));
 		SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
 		this.addSelectMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource, modelClass, table);
 	}
-	
+
 	/**
 	 * <p>
 	 * 注入实体查询总记录数 SQL 语句
@@ -471,13 +473,13 @@ public class AutoSqlInjector implements ISqlInjector {
 	 * @param modelClass
 	 * @param table
 	 */
-	protected void injectSelectCountSql( Class<?> mapperClass, Class<?> modelClass, TableInfo table ) {
+	protected void injectSelectCountSql(Class<?> mapperClass, Class<?> modelClass, TableInfo table) {
 		SqlMethod sqlMethod = SqlMethod.SELECT_COUNT;
 		String sql = String.format(sqlMethod.getSql(), table.getTableName(), sqlWhere(table, true));
 		SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
 		this.addSelectMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource, Integer.class, null);
 	}
-	
+
 	/**
 	 * <p>
 	 * 注入实体查询记录列表 SQL 语句
@@ -514,27 +516,27 @@ public class AutoSqlInjector implements ISqlInjector {
 	 * </p>
 	 * 
 	 * @param selective
-	 * 				是否选择更新
+	 *            是否选择更新
 	 * @param table
 	 * @return
 	 */
-	protected String sqlSet( boolean selective, TableInfo table ) {
+	protected String sqlSet(boolean selective, TableInfo table) {
 		StringBuilder set = new StringBuilder();
 		set.append("<trim prefix=\"SET\" suffixOverrides=\",\">");
 		List<TableFieldInfo> fieldList = table.getFieldList();
-		for ( TableFieldInfo fieldInfo : fieldList ) {
-			if ( selective ) {
+		for (TableFieldInfo fieldInfo : fieldList) {
+			if (selective) {
 				set.append(convertIfTag(fieldInfo, "et.", false));
 			}
 			set.append(fieldInfo.getColumn()).append("=#{et.").append(fieldInfo.getEl()).append("},");
-			if ( selective ) {
+			if (selective) {
 				set.append(convertIfTag(fieldInfo, true));
 			}
 		}
 		set.append("\n</trim>");
 		return set.toString();
 	}
-	
+
 	/**
 	 * <p>
 	 * SQL 查询所有表字段
@@ -595,12 +597,12 @@ public class AutoSqlInjector implements ISqlInjector {
 	 * 
 	 * @param table
 	 * @param space
-	 * 				是否为空判断
+	 *            是否为空判断
 	 * @return
 	 */
 	protected String sqlWhere(TableInfo table, boolean space) {
 		StringBuilder where = new StringBuilder();
-		if ( space ) {
+		if (space) {
 			where.append("\n<if test=\"ew!=null\">");
 		}
 		where.append("\n<where>");
@@ -614,12 +616,12 @@ public class AutoSqlInjector implements ISqlInjector {
 			where.append(convertIfTag(fieldInfo, true));
 		}
 		where.append("\n</where>");
-		if ( space ) {
+		if (space) {
 			where.append("\n</if>");
 		}
 		return where.toString();
 	}
-	
+
 	/**
 	 * <p>
 	 * SQL map 查询条件
@@ -629,7 +631,7 @@ public class AutoSqlInjector implements ISqlInjector {
 		StringBuilder where = new StringBuilder();
 		where.append("\n<foreach collection=\"cm.keys\" item=\"k\" separator=\"AND\"> ");
 		where.append("\n${k}=#{cm[${k}]}");
-		where.append("\n</foreach>"); 
+		where.append("\n</foreach>");
 		return where.toString();
 	}
 
@@ -639,11 +641,11 @@ public class AutoSqlInjector implements ISqlInjector {
 	 * </p>
 	 * 
 	 * @param fieldInfo
-	 * 				字段信息
+	 *            字段信息
 	 * @param prefix
-	 * 				条件前缀
+	 *            条件前缀
 	 * @param colse
-	 * 				是否闭合标签
+	 *            是否闭合标签
 	 * @return
 	 */
 	protected String convertIfTag(TableFieldInfo fieldInfo, String prefix, boolean colse) {
@@ -669,7 +671,7 @@ public class AutoSqlInjector implements ISqlInjector {
 		}
 		return "";
 	}
-	
+
 	protected String convertIfTag(TableFieldInfo fieldInfo, boolean colse) {
 		return convertIfTag(fieldInfo, null, colse);
 	}
@@ -677,46 +679,45 @@ public class AutoSqlInjector implements ISqlInjector {
 	/*
 	 * 查询
 	 */
-	public MappedStatement addSelectMappedStatement(Class<?> mapperClass, String id, SqlSource sqlSource,
-			Class<?> resultType, TableInfo table) {
+	public MappedStatement addSelectMappedStatement(Class<?> mapperClass, String id, SqlSource sqlSource, Class<?> resultType,
+			TableInfo table) {
 		if (null != table) {
 			String resultMap = table.getResultMap();
 			if (null != resultMap) {
 				/* 返回 resultMap 映射结果集 */
-				return this.addMappedStatement(mapperClass, id, sqlSource, SqlCommandType.SELECT, null, resultMap,
-						null, new NoKeyGenerator(), null, null);
+				return this.addMappedStatement(mapperClass, id, sqlSource, SqlCommandType.SELECT, null, resultMap, null,
+						new NoKeyGenerator(), null, null);
 			}
 		}
 
 		/* 普通查询 */
-		return this.addMappedStatement(mapperClass, id, sqlSource, SqlCommandType.SELECT, null, null,
-				resultType, new NoKeyGenerator(), null, null);
+		return this.addMappedStatement(mapperClass, id, sqlSource, SqlCommandType.SELECT, null, null, resultType,
+				new NoKeyGenerator(), null, null);
 	}
 
 	/*
 	 * 插入
 	 */
-	public MappedStatement addInsertMappedStatement(Class<?> mapperClass, Class<?> modelClass, String id,
-			SqlSource sqlSource, KeyGenerator keyGenerator, String keyProperty, String keyColumn) {
-		return this.addMappedStatement(mapperClass, id, sqlSource, SqlCommandType.INSERT, modelClass, null,
-				Integer.class, keyGenerator, keyProperty, keyColumn);
+	public MappedStatement addInsertMappedStatement(Class<?> mapperClass, Class<?> modelClass, String id, SqlSource sqlSource,
+			KeyGenerator keyGenerator, String keyProperty, String keyColumn) {
+		return this.addMappedStatement(mapperClass, id, sqlSource, SqlCommandType.INSERT, modelClass, null, Integer.class,
+				keyGenerator, keyProperty, keyColumn);
 	}
-	
+
 	/*
 	 * 删除
 	 */
 	public MappedStatement addDeleteMappedStatement(Class<?> mapperClass, String id, SqlSource sqlSource) {
-		return this.addMappedStatement(mapperClass, id, sqlSource, SqlCommandType.DELETE, null, null,
-				Integer.class, new NoKeyGenerator(), null, null);
+		return this.addMappedStatement(mapperClass, id, sqlSource, SqlCommandType.DELETE, null, null, Integer.class,
+				new NoKeyGenerator(), null, null);
 	}
 
 	/*
 	 * 更新
 	 */
-	public MappedStatement addUpdateMappedStatement(Class<?> mapperClass, Class<?> modelClass, String id,
-			SqlSource sqlSource) {
-		return this.addMappedStatement(mapperClass, id, sqlSource, SqlCommandType.UPDATE, modelClass, null,
-				Integer.class, new NoKeyGenerator(), null, null);
+	public MappedStatement addUpdateMappedStatement(Class<?> mapperClass, Class<?> modelClass, String id, SqlSource sqlSource) {
+		return this.addMappedStatement(mapperClass, id, sqlSource, SqlCommandType.UPDATE, modelClass, null, Integer.class,
+				new NoKeyGenerator(), null, null);
 	}
 
 	public MappedStatement addMappedStatement(Class<?> mapperClass, String id, SqlSource sqlSource,
@@ -724,7 +725,8 @@ public class AutoSqlInjector implements ISqlInjector {
 			KeyGenerator keyGenerator, String keyProperty, String keyColumn) {
 		String statementName = mapperClass.getName() + "." + id;
 		if (configuration.hasStatement(statementName)) {
-			System.err.println("{" + statementName + "} Has been loaded by XML or SqlProvider, ignoring the injection of the SQL.");
+			System.err.println("{" + statementName
+					+ "} Has been loaded by XML or SqlProvider, ignoring the injection of the SQL.");
 			return null;
 		}
 		/* 缓存逻辑处理 */

+ 4 - 1
mybatis-plus/src/main/java/com/baomidou/mybatisplus/mapper/ISqlInjector.java

@@ -37,7 +37,10 @@ public interface ISqlInjector {
 
 	/**
 	 * <p>
-	 * CRUD注入后给予标识 注入过后不再注入
+	 * 检查SQL是否已经注入
+	 * </p>
+	 * <p>
+	 * ps:注入基本SQL后给予标识 注入过不再注入
 	 * </p>
 	 */
 	void inspectInject(Configuration configuration, MapperBuilderAssistant builderAssistant, Class<?> mapperClass);