Procházet zdrojové kódy

新增无 @TableId 注解跳过注入SQL,支持非表映射对象插入不执行填充

青苗 před 8 roky
rodič
revize
6e7d3d8c6b

+ 16 - 9
mybatis-plus/src/main/java/com/baomidou/mybatisplus/MybatisDefaultParameterHandler.java

@@ -64,24 +64,28 @@ public class MybatisDefaultParameterHandler extends DefaultParameterHandler {
 			 * 只处理插入操作
 			 */
 			Collection<Object> parameters = getParameters(parameterObject);
-			if (parameters != null) {
+			if (null != parameters) {
 				List<Object> objList = new ArrayList<Object>();
 				for (Object parameter : parameters) {
-					if (parameter instanceof Map) {
-						/* map 插入不处理 */
-						objList.add(parameter);
+					TableInfo tableInfo = TableInfoHelper.getTableInfo(parameter.getClass());
+					if (null != tableInfo) {
+						objList.add(populateKeys(tableInfo, ms, parameter));
 					} else {
-						objList.add(populateKeys(ms, parameter));
+						/*
+						 * 非表映射类不处理
+						 */
+						objList.add(parameter);
 					}
 				}
 				return objList;
 			} else {
-				return populateKeys(ms, parameterObject);
+				TableInfo tableInfo = TableInfoHelper.getTableInfo(parameterObject.getClass());
+				return populateKeys(tableInfo, ms, parameterObject);
 			}
 		}
 		return parameterObject;
 	}
-
+	
 	/**
 	 * <p>
 	 * 处理正常批量插入逻辑
@@ -118,13 +122,13 @@ public class MybatisDefaultParameterHandler extends DefaultParameterHandler {
 	 * 填充主键 ID
 	 * </p>
 	 * 
+	 * @param tableInfo
 	 * @param ms
 	 * @param parameterObject
 	 *            插入数据库对象
 	 * @return
 	 */
-	protected static Object populateKeys(MappedStatement ms, Object parameterObject) {
-		TableInfo tableInfo = TableInfoHelper.getTableInfo(parameterObject.getClass());
+	protected static Object populateKeys(TableInfo tableInfo, MappedStatement ms, Object parameterObject) {
 		if (null != tableInfo && null != tableInfo.getIdType() && tableInfo.getIdType().getKey() >= 2) {
 			MetaObject metaObject = ms.getConfiguration().newMetaObject(parameterObject);
 			Object idValue = metaObject.getValue(tableInfo.getKeyProperty());
@@ -143,6 +147,9 @@ public class MybatisDefaultParameterHandler extends DefaultParameterHandler {
 			}
 			return metaObject.getOriginalObject();
 		}
+		/*
+		 * 不处理
+		 */
 		return parameterObject;
 	}
 

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

@@ -65,12 +65,12 @@ public class AutoSqlInjector implements ISqlInjector {
 		this.languageDriver = configuration.getDefaultScriptingLanuageInstance();
 		this.dbType = MybatisConfiguration.DB_TYPE;
 		Class<?> modelClass = extractModelClass(mapperClass);
-		TableInfo table = TableInfoHelper.getTableInfo(modelClass);
+		TableInfo table = TableInfoHelper.initTableInfo(modelClass);
 
 		/**
 		 * 没有指定主键,默认方法不能使用
 		 */
-		if (table.getKeyProperty() != null) {
+		if (null != table && null != table.getKeyProperty()) {
 			/* 插入 */
 			this.injectInsertOneSql(false, mapperClass, modelClass, table);
 			this.injectInsertOneSql(true, mapperClass, modelClass, table);
@@ -104,7 +104,7 @@ public class AutoSqlInjector implements ISqlInjector {
 			/**
 			 * 提示
 			 */
-			System.err.println(String.format("%s ,The unknown primary key, cannot use the generic method", modelClass.toString()));
+			System.err.println(String.format("%s ,Not found @TableId annotation, cannot use mybatis-plus curd method.", modelClass.toString()));
 		}
 	}
 	

+ 24 - 7
mybatis-plus/src/main/java/com/baomidou/mybatisplus/toolkit/TableInfoHelper.java

@@ -46,7 +46,7 @@ public class TableInfoHelper {
 
 	/**
 	 * <p>
-	 * 根据实体类反射获取表信息
+	 * 获取实体映射表信息
 	 * <p>
 	 * 
 	 * @param clazz
@@ -54,11 +54,23 @@ public class TableInfoHelper {
 	 * @return
 	 */
 	public synchronized static TableInfo getTableInfo(Class<?> clazz) {
+		return tableInfoCache.get(clazz.getName());
+	}
+
+	/**
+	 * <p>
+	 * 实体类反射获取表信息【初始化】
+	 * <p>
+	 * 
+	 * @param clazz
+	 *            反射实体类
+	 * @return
+	 */
+	public synchronized static TableInfo initTableInfo(Class<?> clazz) {
 		TableInfo ti = tableInfoCache.get(clazz.getName());
 		if (ti != null) {
 			return ti;
 		}
-		List<Field> list = getAllFields(clazz);
 		TableInfo tableInfo = new TableInfo();
 
 		/* 表名 */
@@ -70,6 +82,7 @@ public class TableInfoHelper {
 		}
 
 		List<TableFieldInfo> fieldList = new ArrayList<TableFieldInfo>();
+		List<Field> list = getAllFields(clazz);
 		for (Field field : list) {
 			/**
 			 * 主键ID
@@ -78,7 +91,7 @@ public class TableInfoHelper {
 			if (tableId != null) {
 				if (tableInfo.getKeyColumn() == null) {
 					tableInfo.setIdType(tableId.type());
-					if(StringUtils.isNotEmpty(tableId.value())) {
+					if (StringUtils.isNotEmpty(tableId.value())) {
 						/* 自定义字段 */
 						tableInfo.setKeyColumn(tableId.value());
 						tableInfo.setKeyRelated(true);
@@ -117,16 +130,20 @@ public class TableInfoHelper {
 		/* 字段列表 */
 		tableInfo.setFieldList(fieldList);
 
-		/* 未发现主键注解抛出异常 */
-		if (tableInfo.getKeyColumn() == null) {
-			throw new MybatisPlusException("Not found @TableId annotation in " + clazz);
+		/*
+		 * 未发现主键注解,跳过注入
+		 */
+		if (null == tableInfo.getKeyColumn()) {
+			return null;
 		}
 
+		/*
+		 * 注入
+		 */
 		tableInfoCache.put(clazz.getName(), tableInfo);
 		return tableInfo;
 	}
 
-	
 	/**
 	 * 获取该类的所有属性列表
 	 *