Explorar o código

Mybatis依赖升级至3.4.2 Mybatis-Spring依赖升级至1.3.1

Caratacus %!s(int64=8) %!d(string=hai) anos
pai
achega
0f575fd558

+ 4 - 4
mybatis-plus/pom.xml

@@ -33,11 +33,11 @@
 	<properties>
 		<!-- <gpg.keyname>F4B46FB9</gpg.keyname> -->
 		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
-		<mybatis-spring.version>1.3.0</mybatis-spring.version>
+		<mybatis-spring.version>1.3.1</mybatis-spring.version>
 		<commons.dbcp2.version>2.1.1</commons.dbcp2.version>
 		<commons.pool2.version>2.4.2</commons.pool2.version>
 		<sqljdbc4.version>4.0</sqljdbc4.version>
-		<mybatis.version>3.4.1</mybatis.version>
+		<mybatis.version>3.4.2</mybatis.version>
 		<jsqlparser.version>0.9.6</jsqlparser.version>
 		<alibaba.druid.version>1.0.24</alibaba.druid.version>
 		<slf4j.version>1.7.21</slf4j.version>
@@ -46,8 +46,8 @@
 		<ojdbc14.version>10.2.0.5.0</ojdbc14.version>
 		<postgresql.version>9.4.1212</postgresql.version>
 		<servlet-api.version>2.5</servlet-api.version>
-		<spring.version>4.2.5.RELEASE</spring.version>
-		<mybatis-ehcache.version>1.0.3</mybatis-ehcache.version>
+		<spring.version>4.3.5.RELEASE</spring.version>
+		<mybatis-ehcache.version>1.1.0</mybatis-ehcache.version>
 		<junit.version>4.12</junit.version>
 		<velocity.version>1.7</velocity.version>
 	</properties>

+ 43 - 5
mybatis-plus/src/main/java/com/baomidou/mybatisplus/MybatisMapperAnnotationBuilder.java

@@ -30,6 +30,7 @@ import org.apache.ibatis.annotations.Lang;
 import org.apache.ibatis.annotations.MapKey;
 import org.apache.ibatis.annotations.Options;
 import org.apache.ibatis.annotations.Options.FlushCachePolicy;
+import org.apache.ibatis.annotations.Property;
 import org.apache.ibatis.annotations.Result;
 import org.apache.ibatis.annotations.ResultMap;
 import org.apache.ibatis.annotations.ResultType;
@@ -63,6 +64,7 @@ import org.apache.ibatis.mapping.ResultSetType;
 import org.apache.ibatis.mapping.SqlCommandType;
 import org.apache.ibatis.mapping.SqlSource;
 import org.apache.ibatis.mapping.StatementType;
+import org.apache.ibatis.parsing.PropertyParser;
 import org.apache.ibatis.reflection.TypeParameterResolver;
 import org.apache.ibatis.scripting.LanguageDriver;
 import org.apache.ibatis.session.Configuration;
@@ -88,6 +90,7 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
+import java.util.Properties;
 import java.util.Set;
 
 /**
@@ -96,7 +99,7 @@ import java.util.Set;
  * </p>
  * 
  * @author Caratacus
- * @Date 2016-09-26
+ * @Date 2017-01-04
  */
 public class MybatisMapperAnnotationBuilder extends MapperAnnotationBuilder {
 
@@ -207,15 +210,36 @@ public class MybatisMapperAnnotationBuilder extends MapperAnnotationBuilder {
 		if (cacheDomain != null) {
 			Integer size = cacheDomain.size() == 0 ? null : cacheDomain.size();
 			Long flushInterval = cacheDomain.flushInterval() == 0 ? null : cacheDomain.flushInterval();
+			Properties props = convertToProperties(cacheDomain.properties());
 			assistant.useNewCache(cacheDomain.implementation(), cacheDomain.eviction(), flushInterval, size,
-					cacheDomain.readWrite(), cacheDomain.blocking(), null);
+					cacheDomain.readWrite(), cacheDomain.blocking(), props);
 		}
 	}
 
+	private Properties convertToProperties(Property[] properties) {
+		if (properties.length == 0) {
+			return null;
+		}
+		Properties props = new Properties();
+		for (Property property : properties) {
+			props.setProperty(property.name(), PropertyParser.parse(property.value(), configuration.getVariables()));
+		}
+		return props;
+	}
+
 	private void parseCacheRef() {
 		CacheNamespaceRef cacheDomainRef = type.getAnnotation(CacheNamespaceRef.class);
 		if (cacheDomainRef != null) {
-			assistant.useCacheRef(cacheDomainRef.value().getName());
+			Class<?> refType = cacheDomainRef.value();
+			String refName = cacheDomainRef.name();
+			if (refType == void.class && refName.isEmpty()) {
+				throw new BuilderException("Should be specified either value() or name() attribute in the @CacheNamespaceRef");
+			}
+			if (refType != void.class && !refName.isEmpty()) {
+				throw new BuilderException("Cannot use both value() and name() attribute in the @CacheNamespaceRef");
+			}
+			String namespace = (refType != void.class) ? refType.getName() : refName;
+			assistant.useCacheRef(namespace);
 		}
 	}
 
@@ -305,6 +329,7 @@ public class MybatisMapperAnnotationBuilder extends MapperAnnotationBuilder {
 			boolean isSelect = sqlCommandType == SqlCommandType.SELECT;
 			boolean flushCache = !isSelect;
 			boolean useCache = isSelect;
+
 			KeyGenerator keyGenerator;
 			String keyProperty = "id";
 			String keyColumn = null;
@@ -326,6 +351,7 @@ public class MybatisMapperAnnotationBuilder extends MapperAnnotationBuilder {
 			} else {
 				keyGenerator = new NoKeyGenerator();
 			}
+
 			if (options != null) {
 				if (FlushCachePolicy.TRUE.equals(options.flushCache())) {
 					flushCache = true;
@@ -334,11 +360,12 @@ public class MybatisMapperAnnotationBuilder extends MapperAnnotationBuilder {
 				}
 				useCache = options.useCache();
 				fetchSize = options.fetchSize() > -1 || options.fetchSize() == Integer.MIN_VALUE ? options.fetchSize() : null; // issue
-				// #348
+																																// #348
 				timeout = options.timeout() > -1 ? options.timeout() : null;
 				statementType = options.statementType();
 				resultSetType = options.resultSetType();
 			}
+
 			String resultMapId = null;
 			ResultMap resultMapAnnotation = method.getAnnotation(ResultMap.class);
 			if (resultMapAnnotation != null) {
@@ -354,6 +381,7 @@ public class MybatisMapperAnnotationBuilder extends MapperAnnotationBuilder {
 			} else if (isSelect) {
 				resultMapId = parseResultMap(method);
 			}
+
 			assistant.addMappedStatement(mappedStatementId, sqlSource, statementType, sqlCommandType, fetchSize, timeout,
 			// ParameterMapID
 					null, parameterTypeClass, resultMapId, getReturnType(method), resultSetType, flushCache, useCache,
@@ -442,6 +470,7 @@ public class MybatisMapperAnnotationBuilder extends MapperAnnotationBuilder {
 				}
 			}
 		}
+
 		return returnType;
 	}
 
@@ -478,11 +507,14 @@ public class MybatisMapperAnnotationBuilder extends MapperAnnotationBuilder {
 
 	private SqlCommandType getSqlCommandType(Method method) {
 		Class<? extends Annotation> type = getSqlAnnotationType(method);
+
 		if (type == null) {
 			type = getSqlProviderAnnotationType(method);
+
 			if (type == null) {
 				return SqlCommandType.UNKNOWN;
 			}
+
 			if (type == SelectProvider.class) {
 				type = Select.class;
 			} else if (type == InsertProvider.class) {
@@ -493,6 +525,7 @@ public class MybatisMapperAnnotationBuilder extends MapperAnnotationBuilder {
 				type = Delete.class;
 			}
 		}
+
 		return SqlCommandType.valueOf(type.getSimpleName().toUpperCase(Locale.ENGLISH));
 	}
 
@@ -598,6 +631,7 @@ public class MybatisMapperAnnotationBuilder extends MapperAnnotationBuilder {
 		String keyProperty = selectKeyAnnotation.keyProperty();
 		String keyColumn = selectKeyAnnotation.keyColumn();
 		boolean executeBefore = selectKeyAnnotation.before();
+
 		// defaults
 		boolean useCache = false;
 		KeyGenerator keyGenerator = new NoKeyGenerator();
@@ -607,16 +641,20 @@ public class MybatisMapperAnnotationBuilder extends MapperAnnotationBuilder {
 		String parameterMap = null;
 		String resultMap = null;
 		ResultSetType resultSetTypeEnum = null;
+
 		SqlSource sqlSource = buildSqlSourceFromStrings(selectKeyAnnotation.statement(), parameterTypeClass, languageDriver);
 		SqlCommandType sqlCommandType = SqlCommandType.SELECT;
+
 		assistant.addMappedStatement(id, sqlSource, statementType, sqlCommandType, fetchSize, timeout, parameterMap,
 				parameterTypeClass, resultMap, resultTypeClass, resultSetTypeEnum, flushCache, useCache, false, keyGenerator,
 				keyProperty, keyColumn, null, languageDriver, null);
+
 		id = assistant.applyCurrentNamespace(id, false);
+
 		MappedStatement keyStatement = configuration.getMappedStatement(id, false);
 		SelectKeyGenerator answer = new SelectKeyGenerator(keyStatement, executeBefore);
 		configuration.addKeyGenerator(id, answer);
 		return answer;
 	}
 
-}
+}

+ 24 - 29
mybatis-plus/src/main/java/com/baomidou/mybatisplus/MybatisXMLConfigBuilder.java

@@ -15,14 +15,6 @@
  */
 package com.baomidou.mybatisplus;
 
-import java.io.InputStream;
-import java.io.Reader;
-import java.util.HashSet;
-import java.util.Properties;
-import java.util.Set;
-
-import javax.sql.DataSource;
-
 import org.apache.ibatis.builder.BaseBuilder;
 import org.apache.ibatis.builder.BuilderException;
 import org.apache.ibatis.builder.xml.XMLMapperEntityResolver;
@@ -51,6 +43,13 @@ import org.apache.ibatis.session.LocalCacheScope;
 import org.apache.ibatis.transaction.TransactionFactory;
 import org.apache.ibatis.type.JdbcType;
 
+import javax.sql.DataSource;
+import java.io.InputStream;
+import java.io.Reader;
+import java.util.HashSet;
+import java.util.Properties;
+import java.util.Set;
+
 /**
  * <p>
  * Copy from XMLConfigBuilder in Mybatis and replace default Configuration class
@@ -58,7 +57,7 @@ import org.apache.ibatis.type.JdbcType;
  * </p>
  *
  * @author hubin
- * @Date 2016-04-20
+ * @Date 2017-01-04
  */
 public class MybatisXMLConfigBuilder extends BaseBuilder {
 
@@ -112,9 +111,9 @@ public class MybatisXMLConfigBuilder extends BaseBuilder {
 
     private void parseConfiguration(XNode root) {
         try {
-            Properties settings = settingsAsPropertiess(root.evalNode("settings"));
-            // issue #117 read properties first
+            //issue #117 read properties first
             propertiesElement(root.evalNode("properties"));
+            Properties settings = settingsAsProperties(root.evalNode("settings"));
             loadCustomVfs(settings);
             typeAliasesElement(root.evalNode("typeAliases"));
             pluginElement(root.evalNode("plugins"));
@@ -132,7 +131,7 @@ public class MybatisXMLConfigBuilder extends BaseBuilder {
         }
     }
 
-    private Properties settingsAsPropertiess(XNode context) {
+    private Properties settingsAsProperties(XNode context) {
         if (context == null) {
             return new Properties();
         }
@@ -141,8 +140,7 @@ public class MybatisXMLConfigBuilder extends BaseBuilder {
         MetaClass metaConfig = MetaClass.forClass(Configuration.class, localReflectorFactory);
         for (Object key : props.keySet()) {
             if (!metaConfig.hasSetter(String.valueOf(key))) {
-                throw new BuilderException(
-                        "The setting " + key + " is not known.  Make sure you spelled it correctly (case sensitive).");
+                throw new BuilderException("The setting " + key + " is not known.  Make sure you spelled it correctly (case sensitive).");
             }
         }
         return props;
@@ -155,7 +153,7 @@ public class MybatisXMLConfigBuilder extends BaseBuilder {
             for (String clazz : clazzes) {
                 if (!clazz.isEmpty()) {
                     @SuppressWarnings("unchecked")
-                    Class<? extends VFS> vfsImpl = (Class<? extends VFS>) Resources.classForName(clazz);
+                    Class<? extends VFS> vfsImpl = (Class<? extends VFS>)Resources.classForName(clazz);
                     configuration.setVfsImpl(vfsImpl);
                 }
             }
@@ -230,8 +228,7 @@ public class MybatisXMLConfigBuilder extends BaseBuilder {
             String resource = context.getStringAttribute("resource");
             String url = context.getStringAttribute("url");
             if (resource != null && url != null) {
-                throw new BuilderException(
-                        "The properties element cannot specify both a URL and a resource based property file reference.  Please specify one or the other.");
+                throw new BuilderException("The properties element cannot specify both a URL and a resource based property file reference.  Please specify one or the other.");
             }
             if (resource != null) {
                 defaults.putAll(Resources.getResourceAsProperties(resource));
@@ -248,16 +245,13 @@ public class MybatisXMLConfigBuilder extends BaseBuilder {
     }
 
     private void settingsElement(Properties props) throws Exception {
-        configuration.setAutoMappingBehavior(
-                AutoMappingBehavior.valueOf(props.getProperty("autoMappingBehavior", "PARTIAL")));
-        configuration.setAutoMappingUnknownColumnBehavior(AutoMappingUnknownColumnBehavior
-                .valueOf(props.getProperty("autoMappingUnknownColumnBehavior", "NONE")));
+        configuration.setAutoMappingBehavior(AutoMappingBehavior.valueOf(props.getProperty("autoMappingBehavior", "PARTIAL")));
+        configuration.setAutoMappingUnknownColumnBehavior(AutoMappingUnknownColumnBehavior.valueOf(props.getProperty("autoMappingUnknownColumnBehavior", "NONE")));
         configuration.setCacheEnabled(booleanValueOf(props.getProperty("cacheEnabled"), true));
         configuration.setProxyFactory((ProxyFactory) createInstance(props.getProperty("proxyFactory")));
         configuration.setLazyLoadingEnabled(booleanValueOf(props.getProperty("lazyLoadingEnabled"), false));
-        configuration.setAggressiveLazyLoading(booleanValueOf(props.getProperty("aggressiveLazyLoading"), true));
-        configuration
-                .setMultipleResultSetsEnabled(booleanValueOf(props.getProperty("multipleResultSetsEnabled"), true));
+        configuration.setAggressiveLazyLoading(booleanValueOf(props.getProperty("aggressiveLazyLoading"), false));
+        configuration.setMultipleResultSetsEnabled(booleanValueOf(props.getProperty("multipleResultSetsEnabled"), true));
         configuration.setUseColumnLabel(booleanValueOf(props.getProperty("useColumnLabel"), true));
         configuration.setUseGeneratedKeys(booleanValueOf(props.getProperty("useGeneratedKeys"), false));
         configuration.setDefaultExecutorType(ExecutorType.valueOf(props.getProperty("defaultExecutorType", "SIMPLE")));
@@ -267,15 +261,15 @@ public class MybatisXMLConfigBuilder extends BaseBuilder {
         configuration.setSafeRowBoundsEnabled(booleanValueOf(props.getProperty("safeRowBoundsEnabled"), false));
         configuration.setLocalCacheScope(LocalCacheScope.valueOf(props.getProperty("localCacheScope", "SESSION")));
         configuration.setJdbcTypeForNull(JdbcType.valueOf(props.getProperty("jdbcTypeForNull", "OTHER")));
-        configuration.setLazyLoadTriggerMethods(
-                stringSetValueOf(props.getProperty("lazyLoadTriggerMethods"), "equals,clone,hashCode,toString"));
+        configuration.setLazyLoadTriggerMethods(stringSetValueOf(props.getProperty("lazyLoadTriggerMethods"), "equals,clone,hashCode,toString"));
         configuration.setSafeResultHandlerEnabled(booleanValueOf(props.getProperty("safeResultHandlerEnabled"), true));
         configuration.setDefaultScriptingLanguage(resolveClass(props.getProperty("defaultScriptingLanguage")));
         configuration.setCallSettersOnNulls(booleanValueOf(props.getProperty("callSettersOnNulls"), false));
-        configuration.setUseActualParamName(booleanValueOf(props.getProperty("useActualParamName"), false));
+        configuration.setUseActualParamName(booleanValueOf(props.getProperty("useActualParamName"), true));
+        configuration.setReturnInstanceForEmptyRow(booleanValueOf(props.getProperty("returnInstanceForEmptyRow"), false));
         configuration.setLogPrefix(props.getProperty("logPrefix"));
         @SuppressWarnings("unchecked")
-        Class<? extends Log> logImpl = (Class<? extends Log>) resolveClass(props.getProperty("logImpl"));
+        Class<? extends Log> logImpl = (Class<? extends Log>)resolveClass(props.getProperty("logImpl"));
         configuration.setLogImpl(logImpl);
         configuration.setConfigurationFactory(resolveClass(props.getProperty("configurationFactory")));
     }
@@ -291,7 +285,8 @@ public class MybatisXMLConfigBuilder extends BaseBuilder {
                     TransactionFactory txFactory = transactionManagerElement(child.evalNode("transactionManager"));
                     DataSourceFactory dsFactory = dataSourceElement(child.evalNode("dataSource"));
                     DataSource dataSource = dsFactory.getDataSource();
-                    Environment.Builder environmentBuilder = new Environment.Builder(id).transactionFactory(txFactory)
+                    Environment.Builder environmentBuilder = new Environment.Builder(id)
+                            .transactionFactory(txFactory)
                             .dataSource(dataSource);
                     configuration.setEnvironment(environmentBuilder.build());
                 }

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

@@ -66,7 +66,7 @@ import static org.springframework.util.StringUtils.tokenizeToStringArray;
  * </p>
  *
  * @author hubin
- * @Date 2016-01-23
+ * @Date 2017-01-04
  */
 public class MybatisSqlSessionFactoryBean implements FactoryBean<SqlSessionFactory>, InitializingBean,
 		ApplicationListener<ApplicationEvent> {
@@ -284,7 +284,7 @@ public class MybatisSqlSessionFactoryBean implements FactoryBean<SqlSessionFacto
 
 	/**
 	 * Set a customized MyBatis configuration.
-	 *
+	 * 
 	 * @param configuration
 	 *            MyBatis configuration
 	 * @since 1.3.0
@@ -437,11 +437,13 @@ public class MybatisSqlSessionFactoryBean implements FactoryBean<SqlSessionFacto
 			configuration = xmlConfigBuilder.getConfiguration();
 		} else {
 			if (LOGGER.isDebugEnabled()) {
-				LOGGER.debug("Property `configuration` or 'configLocation' not specified, using default MyBatis Configuration");
+				LOGGER.debug("Property 'configuration' or 'configLocation' not specified, using default MyBatisPlus Configuration");
 			}
 			// TODO 使用自定义配置
 			configuration = new MybatisConfiguration();
-			configuration.setVariables(this.configurationProperties);
+			if (this.configurationProperties != null) {
+				configuration.setVariables(this.configurationProperties);
+			}
 		}
 
 		if (this.objectFactory != null) {
@@ -516,7 +518,7 @@ public class MybatisSqlSessionFactoryBean implements FactoryBean<SqlSessionFacto
 		}
 
 		if (this.databaseIdProvider != null) {// fix #64 set databaseId before
-			// parse mapper xmls
+												// parse mapper xmls
 			try {
 				configuration.setDatabaseId(this.databaseIdProvider.getDatabaseId(this.dataSource));
 			} catch (SQLException e) {
@@ -568,7 +570,6 @@ public class MybatisSqlSessionFactoryBean implements FactoryBean<SqlSessionFacto
 					MybatisXMLMapperBuilder xmlMapperBuilder = new MybatisXMLMapperBuilder(mapperLocation.getInputStream(),
 							configuration, mapperLocation.toString(), configuration.getSqlFragments());
 					xmlMapperBuilder.parse();
-
 				} catch (Exception e) {
 					throw new NestedIOException("Failed to parse mapping resource: '" + mapperLocation + "'", e);
 				} finally {