瀏覽代碼

Merge remote-tracking branch 'origin/master'

D.Yang 8 年之前
父節點
當前提交
e6a6fd2dc4
共有 17 個文件被更改,包括 191 次插入40 次删除
  1. 1 1
      README.md
  2. 22 0
      mybatis-plus/src/main/java/com/baomidou/framework/service/IService.java
  3. 16 6
      mybatis-plus/src/main/java/com/baomidou/framework/service/impl/ServiceImpl.java
  4. 9 0
      mybatis-plus/src/main/java/com/baomidou/mybatisplus/MybatisConfiguration.java
  5. 7 0
      mybatis-plus/src/main/java/com/baomidou/mybatisplus/MybatisSessionFactoryBuilder.java
  6. 11 1
      mybatis-plus/src/main/java/com/baomidou/mybatisplus/annotations/FieldStrategy.java
  7. 5 0
      mybatis-plus/src/main/java/com/baomidou/mybatisplus/generator/AutoGenerator.java
  8. 1 1
      mybatis-plus/src/main/java/com/baomidou/mybatisplus/generator/ConfigDataSource.java
  9. 69 22
      mybatis-plus/src/main/java/com/baomidou/mybatisplus/mapper/AutoSqlInjector.java
  10. 14 4
      mybatis-plus/src/main/java/com/baomidou/mybatisplus/mapper/BaseMapper.java
  11. 1 0
      mybatis-plus/src/main/java/com/baomidou/mybatisplus/mapper/SqlMethod.java
  12. 6 0
      mybatis-plus/src/main/java/com/baomidou/mybatisplus/spring/MybatisSqlSessionFactoryBean.java
  13. 9 1
      mybatis-plus/src/main/java/com/baomidou/mybatisplus/toolkit/TableFieldInfo.java
  14. 7 0
      mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/MyMetaObjectHandler.java
  15. 9 2
      mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/UserMapperTest.java
  16. 2 2
      mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/entity/User.java
  17. 2 0
      mybatis-plus/src/test/resources/mysql-config.xml

+ 1 - 1
README.md

@@ -71,7 +71,7 @@ Mybatis 增强工具包 - 只做增强不做改变,简化`CRUD`操作
 
 # 捐赠 | Donate
 
-> [捐赠记录,感谢你们的支持!](http://git.oschina.net/juapk/kisso/wikis/%E6%8D%90%E8%B5%A0%E8%AE%B0%E5%BD%95)
+> [捐赠记录,感谢你们的支持!](http://git.oschina.net/baomidou/kisso/wikis/%E6%8D%90%E8%B5%A0%E8%AE%B0%E5%BD%95)
 
 ![捐赠 mybatis-plus](http://git.oschina.net/uploads/images/2015/1222/211207_0acab44e_12260.png "支持一下mybatis-plus")
 

+ 22 - 0
mybatis-plus/src/main/java/com/baomidou/framework/service/IService.java

@@ -243,6 +243,17 @@ public interface IService<T, PK> {
 	 */
 	int selectCount(T entity);
 
+	/**
+	 * <p>
+	 * 根据 EntityWrapper 条件,查询总记录数
+	 * </p>
+	 *
+	 * @param entityWrapper
+	 *            实体对象
+	 * @return int
+	 */
+	int selectCount(EntityWrapper<T> entityWrapper);
+
 	/**
 	 * <p>
 	 * 查询列表
@@ -254,6 +265,17 @@ public interface IService<T, PK> {
 	 */
 	List<T> selectList(EntityWrapper<T> entityWrapper);
 
+	/**
+	 * <p>
+	 * 根据 EntityWrapper,查询一条记录
+	 * </p>
+	 *
+	 * @param entityWrapper
+	 *            实体对象
+	 * @return T
+	 */
+	T selectOne(EntityWrapper<T> entityWrapper);
+
 	/**
 	 * <p>
 	 * 翻页查询

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

@@ -20,6 +20,7 @@ import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
 import com.baomidou.mybatisplus.mapper.BaseMapper;
 import com.baomidou.mybatisplus.mapper.EntityWrapper;
 import com.baomidou.mybatisplus.plugins.Page;
+import com.baomidou.mybatisplus.toolkit.CollectionUtil;
 import com.baomidou.mybatisplus.toolkit.ReflectionKit;
 import com.baomidou.mybatisplus.toolkit.TableInfo;
 import com.baomidou.mybatisplus.toolkit.TableInfoHelper;
@@ -72,10 +73,10 @@ public class ServiceImpl<M extends BaseMapper<T, PK>, T, PK extends Serializable
 			TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);
 			if (null != tableInfo) {
 				Object idVal = ReflectionKit.getMethodValue(cls, entity, tableInfo.getKeyProperty());
-				if (null != idVal) {
-					return isSelective ? updateSelectiveById(entity) : updateById(entity);
-				} else {
+				if (null == idVal || "".equals(idVal)) {
 					return isSelective ? insertSelective(entity) : insert(entity);
+				} else {
+					return isSelective ? updateSelectiveById(entity) : updateById(entity);
 				}
 			} else {
 				throw new MybatisPlusException("Error:  Cannot execute. Could not find @TableId.");
@@ -168,9 +169,18 @@ public class ServiceImpl<M extends BaseMapper<T, PK>, T, PK extends Serializable
 		return baseMapper.selectOne(entity);
 	}
 
-	public int selectCount(T entity) {
-		return baseMapper.selectCount(entity);
-	}
+    public T selectOne(EntityWrapper<T> entityWrapper) {
+        List<T> list = baseMapper.selectList(entityWrapper);
+        return CollectionUtil.isNotEmpty(list) ? list.get(0) : null;
+    }
+
+    public int selectCount(T entity) {
+        return baseMapper.selectCount(entity);
+    }
+
+    public int selectCount(EntityWrapper<T> entityWrapper) {
+        return baseMapper.selectCountByEw(entityWrapper);
+    }
 
 	public List<T> selectList(EntityWrapper<T> entityWrapper) {
 		return baseMapper.selectList(entityWrapper);

+ 9 - 0
mybatis-plus/src/main/java/com/baomidou/mybatisplus/MybatisConfiguration.java

@@ -15,6 +15,7 @@
  */
 package com.baomidou.mybatisplus;
 
+import com.baomidou.mybatisplus.annotations.FieldStrategy;
 import com.baomidou.mybatisplus.mapper.AutoSqlInjector;
 import com.baomidou.mybatisplus.mapper.DBType;
 import com.baomidou.mybatisplus.mapper.IMetaObjectHandler;
@@ -57,19 +58,27 @@ public class MybatisConfiguration extends Configuration {
 	 * SQL 注入器,实现 ISqlInjector 或继承 AutoSqlInjector 自定义方法
 	 */
 	public static ISqlInjector SQL_INJECTOR = new AutoSqlInjector();
+
 	/*
 	 * Mapper 注册
 	 */
 	public final MybatisMapperRegistry mybatisMapperRegistry = new MybatisMapperRegistry(this);
+
 	/**
 	 * 缓存注册标识
 	 */
 	public static Set<String> MAPPER_REGISTRY_CACHE = new ConcurrentSkipListSet<String>();
+
 	/*
 	 * 元对象字段填充控制器
 	 */
 	public static IMetaObjectHandler META_OBJECT_HANDLER = null;
 
+	/*
+	 * 字段验证策略
+	 */
+	public static FieldStrategy FIELD_STRATEGY = FieldStrategy.NOT_NULL;
+
 	/*
 	 * 是否刷新mapper
 	 */

+ 7 - 0
mybatis-plus/src/main/java/com/baomidou/mybatisplus/MybatisSessionFactoryBuilder.java

@@ -25,6 +25,7 @@ import org.apache.ibatis.executor.ErrorContext;
 import org.apache.ibatis.session.SqlSessionFactory;
 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 
+import com.baomidou.mybatisplus.annotations.FieldStrategy;
 import com.baomidou.mybatisplus.mapper.DBType;
 import com.baomidou.mybatisplus.mapper.IMetaObjectHandler;
 import com.baomidou.mybatisplus.mapper.ISqlInjector;
@@ -92,4 +93,10 @@ public class MybatisSessionFactoryBuilder extends SqlSessionFactoryBuilder {
 	public void setMetaObjectHandler(IMetaObjectHandler metaObjectHandler) {
 		MybatisConfiguration.META_OBJECT_HANDLER = metaObjectHandler;
 	}
+
+	// TODO 注入 元对象字段填充控制器
+	public void setFieldStrategy(int key) {
+		MybatisConfiguration.FIELD_STRATEGY = FieldStrategy.getFieldStrategy(key);
+	}
+
 }

+ 11 - 1
mybatis-plus/src/main/java/com/baomidou/mybatisplus/annotations/FieldStrategy.java

@@ -24,7 +24,7 @@ package com.baomidou.mybatisplus.annotations;
  * @Date 2016-09-09
  */
 public enum FieldStrategy {
-	IGNORED(0, "ignored"), NOT_NULL(1, "not null"), NOT_EMPTY(2, "not empty");
+	IGNORED(0, "ignored"), NOT_NULL(1, "not null"), NOT_EMPTY(2, "not empty"), FILL(3, "field fill");
 
 	/** 主键 */
 	private final int key;
@@ -45,4 +45,14 @@ public enum FieldStrategy {
 		return this.desc;
 	}
 
+	public static FieldStrategy getFieldStrategy(int key) {
+		FieldStrategy[] fss = FieldStrategy.values();
+		for (FieldStrategy fs : fss) {
+			if (fs.getKey() == key) {
+				return fs;
+			}
+		}
+		return FieldStrategy.NOT_NULL;
+	}
+
 }

+ 5 - 0
mybatis-plus/src/main/java/com/baomidou/mybatisplus/generator/AutoGenerator.java

@@ -411,6 +411,11 @@ public class AutoGenerator {
 		} else if (t.contains("DATE") || t.contains("TIMESTAMP")) {
 			return "Date";
 		} else if (t.contains("NUMBER")) {
+			if (t.matches("NUMBER\\(+\\d{1}+\\)")) {
+				return "Integer";
+			} else if (t.matches("NUMBER\\(+\\d{2}+\\)")) {
+				return "Long";
+			}
 			return "Double";
 		} else if (t.contains("FLOAT")) {
 			return "Float";

+ 1 - 1
mybatis-plus/src/main/java/com/baomidou/mybatisplus/generator/ConfigDataSource.java

@@ -26,7 +26,7 @@ package com.baomidou.mybatisplus.generator;
 public enum ConfigDataSource {
 	MYSQL("mysql", "show tables", "show table status", "show full fields from %s", "NAME", "COMMENT" ,"FIELD","TYPE","COMMENT","KEY"), 
 	ORACLE("oracle", "SELECT * FROM USER_TABLES", "SELECT * FROM USER_TAB_COMMENTS",
-			"SELECT A.COLUMN_NAME, A.DATA_TYPE, B.COMMENTS  FROM USER_TAB_COLUMNS A, USER_COL_COMMENTS B WHERE A.TABLE_NAME=B.TABLE_NAME AND A.COLUMN_NAME = B.COLUMN_NAME AND A.TABLE_NAME='%s'",
+			"SELECT A.COLUMN_NAME, CASE WHEN A.DATA_TYPE='NUMBER' THEN (CASE WHEN A.DATA_PRECISION IS NULL THEN A.DATA_TYPE WHEN NVL(A.DATA_SCALE, 0) > 0 THEN A.DATA_TYPE||'('||A.DATA_PRECISION||','||A.DATA_SCALE||')' ELSE A.DATA_TYPE||'('||A.DATA_PRECISION||')' END) ELSE A.DATA_TYPE END DATA_TYPE, B.COMMENTS  FROM USER_TAB_COLUMNS A, USER_COL_COMMENTS B WHERE A.TABLE_NAME=B.TABLE_NAME AND A.COLUMN_NAME = B.COLUMN_NAME AND A.TABLE_NAME='%s'",
 			"TABLE_NAME", "COMMENTS" ,"COLUMN_NAME","DATA_TYPE","COMMENTS","COLUMN_NAME");
 
 	private final String db;

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

@@ -15,12 +15,13 @@
  */
 package com.baomidou.mybatisplus.mapper;
 
-import com.baomidou.mybatisplus.MybatisConfiguration;
-import com.baomidou.mybatisplus.annotations.FieldStrategy;
-import com.baomidou.mybatisplus.annotations.IdType;
-import com.baomidou.mybatisplus.toolkit.TableFieldInfo;
-import com.baomidou.mybatisplus.toolkit.TableInfo;
-import com.baomidou.mybatisplus.toolkit.TableInfoHelper;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.logging.Logger;
+
 import org.apache.ibatis.builder.MapperBuilderAssistant;
 import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
 import org.apache.ibatis.executor.keygen.KeyGenerator;
@@ -33,12 +34,12 @@ import org.apache.ibatis.scripting.LanguageDriver;
 import org.apache.ibatis.scripting.defaults.RawSqlSource;
 import org.apache.ibatis.session.Configuration;
 
-import java.lang.reflect.ParameterizedType;
-import java.lang.reflect.Type;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.logging.Logger;
+import com.baomidou.mybatisplus.MybatisConfiguration;
+import com.baomidou.mybatisplus.annotations.FieldStrategy;
+import com.baomidou.mybatisplus.annotations.IdType;
+import com.baomidou.mybatisplus.toolkit.TableFieldInfo;
+import com.baomidou.mybatisplus.toolkit.TableInfo;
+import com.baomidou.mybatisplus.toolkit.TableInfoHelper;
 
 /**
  * <p>
@@ -83,6 +84,10 @@ public class AutoSqlInjector implements ISqlInjector {
 		this.builderAssistant = builderAssistant;
 		this.languageDriver = configuration.getDefaultScriptingLanuageInstance();
 		this.dbType = MybatisConfiguration.DB_TYPE;
+		if (configuration.isMapUnderscoreToCamelCase()) {
+			/* 开启驼峰配置 */
+			MybatisConfiguration.DB_COLUMN_UNDERLINE = true;
+		}
 		Class<?> modelClass = extractModelClass(mapperClass);
 		TableInfo table = TableInfoHelper.initTableInfo(modelClass);
 
@@ -114,6 +119,7 @@ public class AutoSqlInjector implements ISqlInjector {
 			this.injectSelectByMapSql(mapperClass, modelClass, table);
 			this.injectSelectOneSql(mapperClass, modelClass, table);
 			this.injectSelectCountSql(mapperClass, modelClass, table);
+			this.injectSelectCountByEWSql(SqlMethod.SELECT_COUNT_EW, mapperClass, modelClass, table);
 			this.injectSelectListSql(SqlMethod.SELECT_LIST, mapperClass, modelClass, table);
 			this.injectSelectListSql(SqlMethod.SELECT_PAGE, mapperClass, modelClass, table);
 
@@ -192,14 +198,14 @@ public class AutoSqlInjector implements ISqlInjector {
 		List<TableFieldInfo> fieldList = table.getFieldList();
 		for (TableFieldInfo fieldInfo : fieldList) {
 			if (selective) {
-				fieldBuilder.append(convertIfTag(fieldInfo, false));
-				placeholderBuilder.append(convertIfTag(fieldInfo, false));
+				fieldBuilder.append(convertIfTagInsert(fieldInfo, false));
+				placeholderBuilder.append(convertIfTagInsert(fieldInfo, false));
 			}
 			fieldBuilder.append(fieldInfo.getColumn()).append(",");
 			placeholderBuilder.append("#{").append(fieldInfo.getEl()).append("},");
 			if (selective) {
-				fieldBuilder.append(convertIfTag(fieldInfo, true));
-				placeholderBuilder.append(convertIfTag(fieldInfo, true));
+				fieldBuilder.append(convertIfTagInsert(fieldInfo, true));
+				placeholderBuilder.append(convertIfTagInsert(fieldInfo, true));
 			}
 		}
 		fieldBuilder.append("\n</trim>");
@@ -481,15 +487,44 @@ public class AutoSqlInjector implements ISqlInjector {
 
 	/**
 	 * <p>
-	 * 注入实体查询记录列表 SQL 语句
+	 * 注入EntityWrapper方式查询记录列表 SQL 语句
 	 * </p>
-	 * 
+	 *
 	 * @param sqlMethod
 	 * @param mapperClass
 	 * @param modelClass
 	 * @param table
 	 */
 	protected void injectSelectListSql(SqlMethod sqlMethod, Class<?> mapperClass, Class<?> modelClass, TableInfo table) {
+		String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(table, true), table.getTableName(), sqlWhereEntityWrapper(table));
+		SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
+		this.addSelectMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource, modelClass, table);
+	}
+	/**
+	 * <p>
+	 * 注入EntityWrapper查询总记录数 SQL 语句
+	 * </p>
+	 *
+	 * @param sqlMethod
+	 * @param mapperClass
+	 * @param modelClass
+	 * @param table
+	 */
+	protected void injectSelectCountByEWSql(SqlMethod sqlMethod, Class<?> mapperClass, Class<?> modelClass, TableInfo table) {
+		String sql = String.format(sqlMethod.getSql(), table.getTableName(), sqlWhereEntityWrapper(table));
+		SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
+		this.addSelectMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource, Integer.class, null);
+	}
+
+	/**
+	 * <p>
+	 * EntityWrapper方式获取select where
+	 * </p>
+	 *
+	 * @param table
+	 * @return String
+	 */
+	protected String sqlWhereEntityWrapper(TableInfo table) {
 		StringBuilder where = new StringBuilder("\n<if test=\"ew!=null\">");
 		where.append("\n<if test=\"ew.entity!=null\">\n<where>");
 		where.append("\n<if test=\"ew.entity.").append(table.getKeyProperty()).append("!=null\">\n");
@@ -504,9 +539,7 @@ public class AutoSqlInjector implements ISqlInjector {
 		where.append("\n</where>\n</if>");
 		where.append("\n<if test=\"ew.sqlSegment!=null\">\n${ew.sqlSegment}\n</if>");
 		where.append("\n</if>");
-		String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(table, true), table.getTableName(), where.toString());
-		SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
-		this.addSelectMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource, modelClass, table);
+		return where.toString();
 	}
 
 	/**
@@ -639,6 +672,8 @@ public class AutoSqlInjector implements ISqlInjector {
 	 * IF 条件转换方法
 	 * </p>
 	 * 
+	 * @param sqlCommandType
+	 *            SQL 操作类型
 	 * @param fieldInfo
 	 *            字段信息
 	 * @param prefix
@@ -647,7 +682,8 @@ public class AutoSqlInjector implements ISqlInjector {
 	 *            是否闭合标签
 	 * @return
 	 */
-	protected String convertIfTag(TableFieldInfo fieldInfo, String prefix, boolean colse) {
+	protected String convertIfTag(SqlCommandType sqlCommandType, TableFieldInfo fieldInfo, String prefix,
+			boolean colse) {
 		/* 前缀处理 */
 		String property = fieldInfo.getProperty();
 		if (null != prefix) {
@@ -655,6 +691,9 @@ public class AutoSqlInjector implements ISqlInjector {
 		}
 
 		/* 判断策略 */
+		if (sqlCommandType == SqlCommandType.INSERT && fieldInfo.getFieldStrategy() == FieldStrategy.FILL) {
+			return "";
+		}
 		if (fieldInfo.getFieldStrategy() == FieldStrategy.NOT_NULL) {
 			if (colse) {
 				return "</if>";
@@ -670,6 +709,14 @@ public class AutoSqlInjector implements ISqlInjector {
 		}
 		return "";
 	}
+	
+	protected String convertIfTagInsert(TableFieldInfo fieldInfo, boolean colse) {
+		return convertIfTag(SqlCommandType.INSERT, fieldInfo, null, colse);
+	}
+
+	protected String convertIfTag(TableFieldInfo fieldInfo, String prefix, boolean colse) {
+		return convertIfTag(SqlCommandType.UNKNOWN, fieldInfo, prefix, colse);
+	}
 
 	protected String convertIfTag(TableFieldInfo fieldInfo, boolean colse) {
 		return convertIfTag(fieldInfo, null, colse);

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

@@ -15,13 +15,13 @@
  */
 package com.baomidou.mybatisplus.mapper;
 
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.session.RowBounds;
+
 import java.io.Serializable;
 import java.util.List;
 import java.util.Map;
 
-import org.apache.ibatis.annotations.Param;
-import org.apache.ibatis.session.RowBounds;
-
 /**
  * <p>
  * Mapper 继承该接口后,无需编写 mapper.xml 文件,即可获得CRUD功能
@@ -221,6 +221,16 @@ public interface BaseMapper<T, PK extends Serializable> {
 	 */
 	int selectCount( @Param("ew" ) T entity);
 
+	/**
+	 * <p>
+	 * 根据 EntityWrapper 条件,查询总记录数
+	 * </p>
+	 * 
+	 * @param entityWrapper
+	 * 				实体对象
+	 * @return int
+	 */
+	int selectCountByEw(@Param("ew") EntityWrapper<T> entityWrapper);
 
 	/**
 	 * <p>
@@ -231,7 +241,7 @@ public interface BaseMapper<T, PK extends Serializable> {
 	 * @return List<T>
 	 */
 	List<T> selectList( @Param("ew" ) EntityWrapper<T> entityWrapper);
-	
+
 	/**
 	 * <p>
 	 * 根据 entity 条件,查询全部记录(并翻页)

+ 1 - 0
mybatis-plus/src/main/java/com/baomidou/mybatisplus/mapper/SqlMethod.java

@@ -59,6 +59,7 @@ public enum SqlMethod {
 	SELECT_BATCH("selectBatchIds", "根据ID集合,批量查询数据", "<script>SELECT %s FROM %s WHERE %s IN (%s)</script>"),
 	SELECT_ONE("selectOne", "查询满足条件一条数据", "<script>SELECT %s FROM %s %s</script>"),
 	SELECT_COUNT("selectCount", "查询满足条件总记录数", "<script>SELECT COUNT(1) FROM %s %s</script>"),
+	SELECT_COUNT_EW("selectCountByEw", "查询满足条件总记录数", "<script>SELECT COUNT(1) FROM %s %s</script>"),
 	SELECT_LIST("selectList", "查询满足条件所有数据", "<script>SELECT %s FROM %s %s</script>"),
 	SELECT_PAGE("selectPage", "查询满足条件所有数据(并翻页)", "<script>SELECT %s FROM %s %s</script>");
 	

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

@@ -18,6 +18,7 @@ package com.baomidou.mybatisplus.spring;
 import com.baomidou.mybatisplus.MybatisConfiguration;
 import com.baomidou.mybatisplus.MybatisXMLConfigBuilder;
 import com.baomidou.mybatisplus.MybatisXMLMapperBuilder;
+import com.baomidou.mybatisplus.annotations.FieldStrategy;
 import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
 import com.baomidou.mybatisplus.mapper.DBType;
 import com.baomidou.mybatisplus.mapper.IMetaObjectHandler;
@@ -138,6 +139,11 @@ public class MybatisSqlSessionFactoryBean
 		MybatisConfiguration.META_OBJECT_HANDLER = metaObjectHandler;
 	}
 
+	// TODO 注入 元对象字段填充控制器
+	public void setFieldStrategy(int key) {
+		MybatisConfiguration.FIELD_STRATEGY = FieldStrategy.getFieldStrategy(key);
+	}
+
 	/**
 	 * Sets the ObjectFactory.
 	 *

+ 9 - 1
mybatis-plus/src/main/java/com/baomidou/mybatisplus/toolkit/TableFieldInfo.java

@@ -15,6 +15,7 @@
  */
 package com.baomidou.mybatisplus.toolkit;
 
+import com.baomidou.mybatisplus.MybatisConfiguration;
 import com.baomidou.mybatisplus.annotations.FieldStrategy;
 
 /**
@@ -60,7 +61,14 @@ public class TableFieldInfo {
 		this.setColumn(column);
 		this.property = property;
 		this.el = el;
-		this.fieldStrategy = fieldStrategy;
+		/*
+		 * 优先使用单个字段注解,否则使用全局配置
+		 */
+		if (fieldStrategy != FieldStrategy.NOT_NULL) {
+			this.fieldStrategy = fieldStrategy;
+		} else {
+			this.fieldStrategy = MybatisConfiguration.FIELD_STRATEGY;
+		}
 	}
 
 	public TableFieldInfo(boolean related, String column, String property) {

+ 7 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/MyMetaObjectHandler.java

@@ -37,6 +37,13 @@ public class MyMetaObjectHandler implements IMetaObjectHandler {
 		if (null == name) {
 			metaObject.setValue("name", "instert-fill");
 		}
+
+		// 测试下划线
+		Object testType = metaObject.getValue("testType");
+		System.err.println("testTypetestType==" + testType);
+		if (null == testType) {
+			metaObject.setValue("testType", 3);
+		}
 	}
 
 }

+ 9 - 2
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/UserMapperTest.java

@@ -152,10 +152,12 @@ public class UserMapperTest {
         ul.add(new User(15L, "5", 5, 1));
         ul.add(new User(16L, "6", 6, 0));
 
-        /* 测试 name 填充 */
+        /* 测试 name test_type 填充 */
         ul.add(new User(17L, 7));
+        ul.add(new User(18L, 8));
+        ul.add(new User(19L, 9));
         ul.add(new User(7));
-        ul.add(new User(18L, "deleteByMap", 7, 0));
+        ul.add(new User(20L, "deleteByMap", 7, 0));
 
 		/* 使用 ID_WORKER 自动生成 ID */
         ul.add(new User("8", 8, 1));
@@ -163,6 +165,11 @@ public class UserMapperTest {
         rlt = userMapper.insertBatch(ul);
         System.err.println("\n--------------insertBatch----------------" + rlt + "\n\n");
 
+        /**
+         * 提交,往下操作在一个事物中!!!
+         */
+        session.commit();
+
         /*
          * 删除
          */

+ 2 - 2
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/entity/User.java

@@ -49,8 +49,8 @@ public class User implements Serializable {
 
 	private Integer age;
 
-	/* 测试下划线字段命名类型 */
-	@TableField(value = "test_type")
+	/* 测试下划线字段命名类型, 字段填充 */
+	@TableField(value = "test_type", validate = FieldStrategy.FILL)
 	private Integer testType;
 
 	@TableField(el = "role.id")

+ 2 - 0
mybatis-plus/src/test/resources/mysql-config.xml

@@ -32,6 +32,8 @@
         <setting name="cacheEnabled" value="true" />
         <!-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载 -->
         <setting name="lazyLoadingEnabled" value="true" />
+        <!-- 是否开启驼峰命名策略,例如 user_nane 映射为 userName -->
+        <setting name="mapUnderscoreToCamelCase" value="false"/>
     </settings>
 
     <!--