浏览代码

Merge branch 'dev' of https://git.oschina.net/baomidou/mybatis-plus.git into dev

jobob 8 年之前
父节点
当前提交
970785935a

+ 20 - 0
mybatis-plus/pom.xml

@@ -128,6 +128,26 @@
 		<!-- test end -->
 	</dependencies>
 
+	<!-- 发版时注释START -->
+	<build>
+		<plugins>
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-source-plugin</artifactId>
+				<version>2.2.1</version>
+				<executions>
+					<execution>
+						<phase>package</phase>
+						<goals>
+							<goal>jar-no-fork</goal>
+						</goals>
+					</execution>
+				</executions>
+			</plugin>
+		</plugins>
+	</build>
+	<!-- 发版时注释END -->
+
 	<profiles>
 		<profile>
 			<id>release</id>

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

@@ -149,9 +149,9 @@ public class ServiceImpl<M extends BaseMapper<T, PK>, T, PK extends Serializable
 			int size = entityList.size();
 			for (int i = 0; i < size; i++) {
 				if (isSelective) {
-					baseMapper.insertSelective(entityList.get(0));
+					baseMapper.insertSelective(entityList.get(i));
 				} else {
-					baseMapper.insert(entityList.get(0));
+					baseMapper.insert(entityList.get(i));
 				}
 				if (i % batchSize == 0) {
 					batchSqlSession.flushStatements();

+ 32 - 0
mybatis-plus/src/main/java/com/baomidou/mybatisplus/MybatisPlusHolder.java

@@ -0,0 +1,32 @@
+package com.baomidou.mybatisplus;
+
+import org.apache.ibatis.session.SqlSession;
+import org.apache.ibatis.session.SqlSessionFactory;
+
+/**
+ * <p>
+ * MybatisPlusHolder
+ * </p>
+ *
+ * @author Caratacus
+ * @Date 2016-10-26
+ */
+public class MybatisPlusHolder {
+
+	private static SqlSession sqlSession;
+	private static SqlSessionFactory sqlSessionFactory;
+
+	public static void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
+		MybatisPlusHolder.sqlSessionFactory = sqlSessionFactory;
+		MybatisPlusHolder.sqlSession = sqlSessionFactory.openSession();
+	}
+
+	public static SqlSessionFactory getSqlSessionFactory() {
+		return sqlSessionFactory;
+	}
+
+	public static SqlSession getSqlSession() {
+		return sqlSession;
+	}
+
+}

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

@@ -21,7 +21,7 @@ import com.baomidou.mybatisplus.toolkit.StringUtils;
 import java.io.Serializable;
 import java.text.MessageFormat;
 import java.util.Arrays;
-import java.util.List;
+import java.util.Collection;
 
 /**
  * <p>
@@ -384,7 +384,7 @@ public class EntityWrapper<T> implements Serializable {
 	 *            匹配值 List集合
 	 * @return this
 	 */
-	public EntityWrapper<T> in(String column, List<?> value) {
+	public EntityWrapper<T> in(String column, Collection<?> value) {
 		sql.IN(column, value);
 		return this;
 	}
@@ -398,7 +398,7 @@ public class EntityWrapper<T> implements Serializable {
 	 *            匹配值 List集合
 	 * @return this
 	 */
-	public EntityWrapper<T> notIn(String column, List<?> value) {
+	public EntityWrapper<T> notIn(String column, Collection<?> value) {
 		sql.NOT_IN(column, value);
 		return this;
 	}
@@ -412,7 +412,7 @@ public class EntityWrapper<T> implements Serializable {
 	 *            匹配值 object数组
 	 * @return this
 	 */
-	public EntityWrapper<T> in(String column, Object... value) {
+	public EntityWrapper<T> in(String column, Object[] value) {
 		sql.IN(column, Arrays.asList(value));
 		return this;
 	}

+ 10 - 6
mybatis-plus/src/main/java/com/baomidou/mybatisplus/mapper/TSqlPlus.java

@@ -20,7 +20,8 @@ import com.baomidou.mybatisplus.toolkit.CollectionUtil;
 import com.baomidou.mybatisplus.toolkit.StringUtils;
 
 import java.text.MessageFormat;
-import java.util.List;
+import java.util.Collection;
+import java.util.Iterator;
 
 /**
  * <p>
@@ -126,7 +127,7 @@ public class TSqlPlus extends MybatisAbstractSQL<TSqlPlus> {
      *            List集合
      * @return
      */
-    public TSqlPlus IN(String column, List<?> value) {
+    public TSqlPlus IN(String column, Collection<?> value) {
         handerIn(column, value, false);
         return this;
     }
@@ -140,7 +141,7 @@ public class TSqlPlus extends MybatisAbstractSQL<TSqlPlus> {
      *            List集合
      * @return
      */
-    public TSqlPlus NOT_IN(String column, List<?> value) {
+    public TSqlPlus NOT_IN(String column, Collection<?> value) {
         handerIn(column, value, true);
         return this;
     }
@@ -223,7 +224,7 @@ public class TSqlPlus extends MybatisAbstractSQL<TSqlPlus> {
      * @param isNot
      *            是否为NOT IN操作
      */
-    private void handerIn(String column, List<?> value, boolean isNot) {
+    private void handerIn(String column, Collection<?> value, boolean isNot) {
         if (StringUtils.isNotEmpty(column) && CollectionUtil.isNotEmpty(value)) {
             StringBuilder inSql = new StringBuilder();
             inSql.append(column);
@@ -232,14 +233,17 @@ public class TSqlPlus extends MybatisAbstractSQL<TSqlPlus> {
             }
             inSql.append(" IN (");
             int _size = value.size();
-            for (int i = 0; i < _size; i++) {
-                String tempVal = StringUtils.quotaMark(value.get(i));
+            int i = 0;
+            Iterator<?> iterator = value.iterator();
+            while(iterator.hasNext()) {
+                String tempVal = StringUtils.quotaMark(iterator.next());
                 if (i + 1 == _size) {
                     inSql.append(tempVal);
                 } else {
                     inSql.append(tempVal);
                     inSql.append(",");
                 }
+                i++;
             }
             inSql.append(")");
             WHERE(inSql.toString());

+ 2 - 2
mybatis-plus/src/main/java/com/baomidou/mybatisplus/spring/MybatisSqlSessionFactoryBean.java

@@ -16,6 +16,7 @@
 package com.baomidou.mybatisplus.spring;
 
 import com.baomidou.mybatisplus.MybatisConfiguration;
+import com.baomidou.mybatisplus.MybatisPlusHolder;
 import com.baomidou.mybatisplus.MybatisXMLConfigBuilder;
 import com.baomidou.mybatisplus.MybatisXMLMapperBuilder;
 import com.baomidou.mybatisplus.annotations.FieldStrategy;
@@ -24,7 +25,6 @@ import com.baomidou.mybatisplus.mapper.DBType;
 import com.baomidou.mybatisplus.mapper.IMetaObjectHandler;
 import com.baomidou.mybatisplus.mapper.ISqlInjector;
 import com.baomidou.mybatisplus.toolkit.PackageHelper;
-import com.baomidou.mybatisplus.toolkit.TableInfoHelper;
 import org.apache.ibatis.cache.Cache;
 import org.apache.ibatis.executor.ErrorContext;
 import org.apache.ibatis.io.VFS;
@@ -598,7 +598,7 @@ public class MybatisSqlSessionFactoryBean implements FactoryBean<SqlSessionFacto
 		}
 		SqlSessionFactory sqlSessionFactory = this.sqlSessionFactoryBuilder.build(configuration);
 		// TODO 缓存 sqlSessionFactory
-		TableInfoHelper.cacheSqlSessionFactory(sqlSessionFactory);
+		MybatisPlusHolder.setSqlSessionFactory(sqlSessionFactory);
 		return sqlSessionFactory;
 	}
 

+ 137 - 152
mybatis-plus/src/main/java/com/baomidou/mybatisplus/toolkit/TableInfoHelper.java

@@ -16,16 +16,15 @@
 package com.baomidou.mybatisplus.toolkit;
 
 import com.baomidou.mybatisplus.MybatisConfiguration;
+import com.baomidou.mybatisplus.MybatisPlusHolder;
 import com.baomidou.mybatisplus.annotations.TableField;
 import com.baomidou.mybatisplus.annotations.TableId;
 import com.baomidou.mybatisplus.annotations.TableName;
 import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
-import org.apache.ibatis.session.SqlSessionFactory;
 
 import java.lang.reflect.Field;
 import java.lang.reflect.Modifier;
 import java.util.ArrayList;
-import java.util.Collection;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
@@ -41,184 +40,170 @@ import java.util.concurrent.ConcurrentHashMap;
  */
 public class TableInfoHelper {
 
-    /**
-     * 缓存反射类表信息
-     */
-    private static Map<String, TableInfo> tableInfoCache = new ConcurrentHashMap<String, TableInfo>();
-
-    /**
-     * <p>
-     * 获取实体映射表信息
-     * <p>
-     *
-     * @param clazz
-     *            反射实体类
-     * @return
-     */
-    public 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;
-        }
-        TableInfo tableInfo = new TableInfo();
+	/**
+	 * 缓存反射类表信息
+	 */
+	private static Map<String, TableInfo> tableInfoCache = new ConcurrentHashMap<String, TableInfo>();
+
+	/**
+	 * <p>
+	 * 获取实体映射表信息
+	 * <p>
+	 *
+	 * @param clazz
+	 *            反射实体类
+	 * @return
+	 */
+	public 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;
+		}
+		TableInfo tableInfo = new TableInfo();
 
 		/* 表名 */
-        TableName table = clazz.getAnnotation(TableName.class);
-        if (table != null && StringUtils.isNotEmpty(table.value())) {
-            tableInfo.setTableName(table.value());
-        } else {
-            tableInfo.setTableName(StringUtils.camelToUnderline(clazz.getSimpleName()));
-        }
+		TableName table = clazz.getAnnotation(TableName.class);
+		if (table != null && StringUtils.isNotEmpty(table.value())) {
+			tableInfo.setTableName(table.value());
+		} else {
+			tableInfo.setTableName(StringUtils.camelToUnderline(clazz.getSimpleName()));
+		}
 
 		/* 表结果集映射 */
-        if (table != null && StringUtils.isNotEmpty(table.resultMap())) {
-            tableInfo.setResultMap(table.resultMap());
-        }
-
-        List<TableFieldInfo> fieldList = new ArrayList<TableFieldInfo>();
-        List<Field> list = getAllFields(clazz);
-        for (Field field : list) {
-            /**
-             * 主键ID
-             */
-            TableId tableId = field.getAnnotation(TableId.class);
-            if (tableId != null) {
-                if (tableInfo.getKeyColumn() == null) {
-                    tableInfo.setIdType(tableId.type());
-                    if (StringUtils.isNotEmpty(tableId.value())) {
+		if (table != null && StringUtils.isNotEmpty(table.resultMap())) {
+			tableInfo.setResultMap(table.resultMap());
+		}
+
+		List<TableFieldInfo> fieldList = new ArrayList<TableFieldInfo>();
+		List<Field> list = getAllFields(clazz);
+		for (Field field : list) {
+			/**
+			 * 主键ID
+			 */
+			TableId tableId = field.getAnnotation(TableId.class);
+			if (tableId != null) {
+				if (tableInfo.getKeyColumn() == null) {
+					tableInfo.setIdType(tableId.type());
+					if (StringUtils.isNotEmpty(tableId.value())) {
 						/* 自定义字段 */
-                        tableInfo.setKeyColumn(tableId.value());
-                        tableInfo.setKeyRelated(true);
-                    } else if (MybatisConfiguration.DB_COLUMN_UNDERLINE) {
+						tableInfo.setKeyColumn(tableId.value());
+						tableInfo.setKeyRelated(true);
+					} else if (MybatisConfiguration.DB_COLUMN_UNDERLINE) {
 						/* 开启字段下划线申明 */
-                        tableInfo.setKeyColumn(StringUtils.camelToUnderline(field.getName()));
-                    } else {
-                        tableInfo.setKeyColumn(field.getName());
-                    }
-                    tableInfo.setKeyProperty(field.getName());
-                    continue;
-                } else {
+						tableInfo.setKeyColumn(StringUtils.camelToUnderline(field.getName()));
+					} else {
+						tableInfo.setKeyColumn(field.getName());
+					}
+					tableInfo.setKeyProperty(field.getName());
+					continue;
+				} else {
 					/* 发现设置多个主键注解抛出异常 */
-                    throw new MybatisPlusException("There must be only one, Discover multiple @TableId annotation in " + clazz);
-                }
-            }
+					throw new MybatisPlusException("There must be only one, Discover multiple @TableId annotation in " + clazz);
+				}
+			}
 
 			/* 获取注解属性,自定义字段 */
-            TableField tableField = field.getAnnotation(TableField.class);
-            if (tableField != null) {
-                String columnName = field.getName();
-                if (StringUtils.isNotEmpty(tableField.value())) {
-                    columnName = tableField.value();
-                }
+			TableField tableField = field.getAnnotation(TableField.class);
+			if (tableField != null) {
+				String columnName = field.getName();
+				if (StringUtils.isNotEmpty(tableField.value())) {
+					columnName = tableField.value();
+				}
 
 				/*
 				 * el 语法支持,可以传入多个参数以逗号分开
 				 */
-                String el = field.getName();
-                if (StringUtils.isNotEmpty(tableField.el())) {
-                    el = tableField.el();
-                }
-                String[] columns = columnName.split(";");
-                String[] els = el.split(";");
-                if (null != columns && null != els && columns.length == els.length) {
-                    for (int i = 0; i < columns.length; i++) {
-                        fieldList.add(new TableFieldInfo(true, columns[i], field.getName(), els[i], tableField.validate()));
-                    }
-                } else {
-                    String errorMsg = "Class: %s, Field: %s, 'value' 'el' Length must be consistent.";
-                    throw new MybatisPlusException(String.format(errorMsg, clazz.getName(), field.getName()));
-                }
-
-                continue;
-            }
-
-            /**
-             * 字段, 使用 camelToUnderline 转换驼峰写法为下划线分割法, 如果已指定 TableField , 便不会执行这里
-             */
-            if (MybatisConfiguration.DB_COLUMN_UNDERLINE) {
+				String el = field.getName();
+				if (StringUtils.isNotEmpty(tableField.el())) {
+					el = tableField.el();
+				}
+				String[] columns = columnName.split(";");
+				String[] els = el.split(";");
+				if (null != columns && null != els && columns.length == els.length) {
+					for (int i = 0; i < columns.length; i++) {
+						fieldList.add(new TableFieldInfo(true, columns[i], field.getName(), els[i], tableField.validate()));
+					}
+				} else {
+					String errorMsg = "Class: %s, Field: %s, 'value' 'el' Length must be consistent.";
+					throw new MybatisPlusException(String.format(errorMsg, clazz.getName(), field.getName()));
+				}
+
+				continue;
+			}
+
+			/**
+			 * 字段, 使用 camelToUnderline 转换驼峰写法为下划线分割法, 如果已指定 TableField , 便不会执行这里
+			 */
+			if (MybatisConfiguration.DB_COLUMN_UNDERLINE) {
 				/* 开启字段下划线申明 */
-                fieldList.add(new TableFieldInfo(true, StringUtils.camelToUnderline(field.getName()), field.getName()));
-            } else {
-                fieldList.add(new TableFieldInfo(field.getName()));
-            }
-        }
+				fieldList.add(new TableFieldInfo(true, StringUtils.camelToUnderline(field.getName()), field.getName()));
+			} else {
+				fieldList.add(new TableFieldInfo(field.getName()));
+			}
+		}
 
 		/* 字段列表 */
-        tableInfo.setFieldList(fieldList);
+		tableInfo.setFieldList(fieldList);
 
 		/*
 		 * 未发现主键注解,跳过注入
 		 */
-        if (null == tableInfo.getKeyColumn()) {
-            return null;
-        }
-
+		if (null == tableInfo.getKeyColumn()) {
+			return null;
+		}
+		// 缓存
+		tableInfo.setSqlSessionFactory(MybatisPlusHolder.getSqlSessionFactory());
 		/*
 		 * 注入
 		 */
-        tableInfoCache.put(clazz.getName(), tableInfo);
-        return tableInfo;
-    }
-
-    /**
-     * 获取该类的所有属性列表
-     *
-     * @param clazz
-     *            反射类
-     * @return
-     */
-    private static List<Field> getAllFields(Class<?> clazz) {
-        List<Field> result = new LinkedList<Field>();
-        Field[] fields = clazz.getDeclaredFields();
-        for (Field field : fields) {
+		tableInfoCache.put(clazz.getName(), tableInfo);
+		return tableInfo;
+	}
+
+	/**
+	 * 获取该类的所有属性列表
+	 *
+	 * @param clazz
+	 *            反射类
+	 * @return
+	 */
+	private static List<Field> getAllFields(Class<?> clazz) {
+		List<Field> result = new LinkedList<Field>();
+		Field[] fields = clazz.getDeclaredFields();
+		for (Field field : fields) {
 
 			/* 过滤 transient关键字修饰的属性 */
-            if (Modifier.isTransient(field.getModifiers())) {
-                continue;
-            }
+			if (Modifier.isTransient(field.getModifiers())) {
+				continue;
+			}
 
 			/* 过滤注解非表字段属性 */
-            TableField tableField = field.getAnnotation(TableField.class);
-            if (tableField == null || tableField.exist()) {
-                result.add(field);
-            }
-        }
+			TableField tableField = field.getAnnotation(TableField.class);
+			if (tableField == null || tableField.exist()) {
+				result.add(field);
+			}
+		}
 
 		/* 处理父类字段 */
-        Class<?> superClass = clazz.getSuperclass();
-        if (superClass.equals(Object.class)) {
-            return result;
-        }
-        result.addAll(getAllFields(superClass));
-        return result;
-    }
-
-    /**
-     * 缓存sqlSessionFactory
-     *
-     * @param sqlSessionFactory
-     * @return
-     */
-    public static void cacheSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
-        Collection<TableInfo> values = tableInfoCache.values();
-        for (TableInfo tableInfo : values) {
-            if (null == tableInfo.getSqlSessionFactory()) {
-                tableInfo.setSqlSessionFactory(sqlSessionFactory);
-            }
-        }
-    }
+		Class<?> superClass = clazz.getSuperclass();
+		if (superClass.equals(Object.class)) {
+			return result;
+		}
+		result.addAll(getAllFields(superClass));
+		return result;
+	}
 
 }

+ 17 - 1
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/EntityWrapperTest.java

@@ -23,6 +23,8 @@ import org.junit.Test;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Set;
+import java.util.TreeSet;
 
 /**
  * <p>
@@ -204,7 +206,7 @@ public class EntityWrapperTest {
 	 * 测试NOT IN
 	 */
 	@Test
-	public void testNul17() {
+	public void test17() {
 		List<String> list = new ArrayList<String>();
 		list.add("'1'");
 		list.add("'2'");
@@ -229,6 +231,20 @@ public class EntityWrapperTest {
 		System.out.println("sql ==> " + sqlPart);
 		Assert.assertEquals("WHERE (test_type IN (111111111,222222222,333333333))", sqlPart);
 	}
+	/**
+	 * 测试IN
+	 */
+	@Test
+	public void test18() {
+		Set<Long> list = new TreeSet<Long>();
+		list.add(111111111L);
+		list.add(222222222L);
+		list.add(333333333L);
+		ew.in("test_type", list);
+		String sqlPart = ew.getSqlSegment();
+		System.out.println("sql ==> " + sqlPart);
+		Assert.assertEquals("WHERE (test_type IN (111111111,222222222,333333333))", sqlPart);
+	}
 
 	/**
 	 * 测试BETWEEN AND