org.apache.ibatis.session.AutoMappingBehavior Java Examples

The following examples show how to use org.apache.ibatis.session.AutoMappingBehavior. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NpeExtendsTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
private SqlSessionFactory getSqlSessionFactoryWithConstructor() {
    UnpooledDataSourceFactory unpooledDataSourceFactory = new UnpooledDataSourceFactory();
    Properties properties = new Properties();
    properties.setProperty("driver", "org.hsqldb.jdbcDriver");
    properties.setProperty("url", "jdbc:hsqldb:mem:extends_with_constructor");
    properties.setProperty("username", "sa");
    unpooledDataSourceFactory.setProperties(properties);
    Environment environment = new Environment("extends_with_constructor", new JdbcTransactionFactory(), unpooledDataSourceFactory.getDataSource());
    
    Configuration configuration = new Configuration();
    configuration.setEnvironment(environment);
    configuration.addMapper(StudentConstructorMapper.class);
    configuration.addMapper(TeacherMapper.class);
    configuration.getMappedStatementNames();
    configuration.setAutoMappingBehavior(AutoMappingBehavior.NONE);
    
    return new DefaultSqlSessionFactory(configuration);
}
 
Example #2
Source File: NpeExtendsTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
private SqlSessionFactory getSqlSessionFactoryWithConstructor() {
    UnpooledDataSourceFactory unpooledDataSourceFactory = new UnpooledDataSourceFactory();
    Properties properties = new Properties();
    properties.setProperty("driver", "org.hsqldb.jdbcDriver");
    properties.setProperty("url", "jdbc:hsqldb:mem:extends_with_constructor");
    properties.setProperty("username", "sa");
    unpooledDataSourceFactory.setProperties(properties);
    Environment environment = new Environment("extends_with_constructor", new JdbcTransactionFactory(), unpooledDataSourceFactory.getDataSource());
    
    Configuration configuration = new Configuration();
    configuration.setEnvironment(environment);
    configuration.addMapper(StudentConstructorMapper.class);
    configuration.addMapper(TeacherMapper.class);
    configuration.getMappedStatementNames();
    configuration.setAutoMappingBehavior(AutoMappingBehavior.NONE);
    
    return new DefaultSqlSessionFactory(configuration);
}
 
Example #3
Source File: AutomappingTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldUpdateFinalField() {
  // set automapping to default partial
  sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.PARTIAL);
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    Mapper mapper = sqlSession.getMapper(Mapper.class);
    Article article = mapper.getArticle();
    // Java Language Specification 17.5.3 Subsequent Modification of Final Fields
    // http://docs.oracle.com/javase/specs/jls/se5.0/html/memory.html#17.5.3
    // The final field should be updated in mapping
    Assert.assertTrue("should update version in mapping", article.version > 0);
  } finally {
    sqlSession.close();
  }
}
 
Example #4
Source File: AutomappingTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldIgnorePartialAutoMappingBehavior_ExternalNestedResultMap() {
  // For nested resultMaps, PARTIAL works the same as NONE
  sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.PARTIAL);
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    Mapper mapper = sqlSession.getMapper(Mapper.class);
    User user = mapper.getUserWithPets_External(2);
    Assert.assertEquals(Integer.valueOf(2), user.getId());
    Assert.assertEquals("User2", user.getName());
    Assert.assertNull("should not inherit auto-mapping", user.getPets().get(0).getPetName());
    Assert.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName());
  } finally {
    sqlSession.close();
  }
}
 
Example #5
Source File: AutomappingTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldIgnorePartialAutoMappingBehavior_InlineNestedResultMap() {
  // For nested resultMaps, PARTIAL works the same as NONE
  sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.PARTIAL);
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    Mapper mapper = sqlSession.getMapper(Mapper.class);
    User user = mapper.getUserWithPets_Inline(2);
    Assert.assertEquals(Integer.valueOf(2), user.getId());
    Assert.assertEquals("User2", user.getName());
    Assert.assertNull("should not inherit auto-mapping", user.getPets().get(0).getPetName());
    Assert.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName());
  } finally {
    sqlSession.close();
  }
}
 
Example #6
Source File: AutomappingTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldUpdateFinalField() {
  // set automapping to default partial
  sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.PARTIAL);
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    Mapper mapper = sqlSession.getMapper(Mapper.class);
    Article article = mapper.getArticle();
    // Java Language Specification 17.5.3 Subsequent Modification of Final Fields
    // http://docs.oracle.com/javase/specs/jls/se5.0/html/memory.html#17.5.3
    // The final field should be updated in mapping
    Assert.assertTrue("should update version in mapping", article.version > 0);
  } finally {
    sqlSession.close();
  }
}
 
Example #7
Source File: AutomappingTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldIgnorePartialAutoMappingBehavior_ExternalNestedResultMap() {
  // For nested resultMaps, PARTIAL works the same as NONE
  sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.PARTIAL);
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    Mapper mapper = sqlSession.getMapper(Mapper.class);
    User user = mapper.getUserWithPets_External(2);
    Assert.assertEquals(Integer.valueOf(2), user.getId());
    Assert.assertEquals("User2", user.getName());
    Assert.assertNull("should not inherit auto-mapping", user.getPets().get(0).getPetName());
    Assert.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName());
  } finally {
    sqlSession.close();
  }
}
 
Example #8
Source File: AutomappingTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldIgnorePartialAutoMappingBehavior_InlineNestedResultMap() {
  // For nested resultMaps, PARTIAL works the same as NONE
  sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.PARTIAL);
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    Mapper mapper = sqlSession.getMapper(Mapper.class);
    User user = mapper.getUserWithPets_Inline(2);
    Assert.assertEquals(Integer.valueOf(2), user.getId());
    Assert.assertEquals("User2", user.getName());
    Assert.assertNull("should not inherit auto-mapping", user.getPets().get(0).getPetName());
    Assert.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName());
  } finally {
    sqlSession.close();
  }
}
 
Example #9
Source File: XMLConfigBuilder.java    From QuickProject with Apache License 2.0 5 votes vote down vote up
private void settingsElement(XNode context) throws Exception {
  if (context != null) {
    Properties props = context.getChildrenAsProperties();
    // Check that all settings are known to the configuration class
    MetaClass metaConfig = MetaClass.forClass(Configuration.class);
    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).");
      }
    }
    configuration.setAutoMappingBehavior(AutoMappingBehavior.valueOf(props.getProperty("autoMappingBehavior", "PARTIAL")));
    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.setUseColumnLabel(booleanValueOf(props.getProperty("useColumnLabel"), true));
    configuration.setUseGeneratedKeys(booleanValueOf(props.getProperty("useGeneratedKeys"), false));
    configuration.setDefaultExecutorType(ExecutorType.valueOf(props.getProperty("defaultExecutorType", "SIMPLE")));
    configuration.setDefaultStatementTimeout(integerValueOf(props.getProperty("defaultStatementTimeout"), null));
    configuration.setMapUnderscoreToCamelCase(booleanValueOf(props.getProperty("mapUnderscoreToCamelCase"), false));
    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.setSafeResultHandlerEnabled(booleanValueOf(props.getProperty("safeResultHandlerEnabled"), true));
    configuration.setDefaultScriptingLanguage(resolveClass(props.getProperty("defaultScriptingLanguage")));
    configuration.setCallSettersOnNulls(booleanValueOf(props.getProperty("callSettersOnNulls"), false));
    configuration.setLogPrefix(props.getProperty("logPrefix"));
    configuration.setLogImpl(resolveClass(props.getProperty("logImpl")));
    configuration.setConfigurationFactory(resolveClass(props.getProperty("configurationFactory")));
  }
}
 
Example #10
Source File: AutomappingTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldRespectFullAutoMappingBehavior_InlineNestedResultMap() {
  sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.FULL);
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    Mapper mapper = sqlSession.getMapper(Mapper.class);
    User user = mapper.getUserWithPets_Inline(2);
    Assert.assertEquals(Integer.valueOf(2), user.getId());
    Assert.assertEquals("User2", user.getName());
    Assert.assertEquals("Chien", user.getPets().get(0).getPetName());
    Assert.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName());
  } finally {
    sqlSession.close();
  }
}
 
Example #11
Source File: DefaultResultSetHandler.java    From mybaties with Apache License 2.0 5 votes vote down vote up
private boolean shouldApplyAutomaticMappings(ResultMap resultMap, boolean isNested) {
  if (resultMap.getAutoMapping() != null) {
    return resultMap.getAutoMapping();
  } else {
    if (isNested) {
      return AutoMappingBehavior.FULL == configuration.getAutoMappingBehavior();
    } else {
      return AutoMappingBehavior.NONE != configuration.getAutoMappingBehavior();
    }
  }
}
 
Example #12
Source File: AutomappingTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldGetBooks() {
  // set automapping to default partial
  sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.PARTIAL);
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    Mapper mapper = sqlSession.getMapper(Mapper.class);
    // no errors throw
    List<Book> books = mapper.getBooks();
    Assert.assertTrue("should return results,no errors throw", !books.isEmpty());
  } finally {
    sqlSession.close();
  }
}
 
Example #13
Source File: AutomappingTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldRespectFullAutoMappingBehavior_ExternalNestedResultMap() {
  sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.FULL);
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    Mapper mapper = sqlSession.getMapper(Mapper.class);
    User user = mapper.getUserWithPets_External(2);
    Assert.assertEquals(Integer.valueOf(2), user.getId());
    Assert.assertEquals("User2", user.getName());
    Assert.assertEquals("Chien", user.getPets().get(0).getPetName());
    Assert.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName());
  } finally {
    sqlSession.close();
  }
}
 
Example #14
Source File: AutomappingTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldGetAUser() {
  sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.NONE);
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    Mapper mapper = sqlSession.getMapper(Mapper.class);
    User user = mapper.getUser(1);
    Assert.assertEquals("User1", user.getName());
  } finally {
    sqlSession.close();
  }
}
 
Example #15
Source File: AutomappingTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldRespectFullAutoMappingBehavior_InlineNestedResultMap() {
  sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.FULL);
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    Mapper mapper = sqlSession.getMapper(Mapper.class);
    User user = mapper.getUserWithPets_Inline(2);
    Assert.assertEquals(Integer.valueOf(2), user.getId());
    Assert.assertEquals("User2", user.getName());
    Assert.assertEquals("Chien", user.getPets().get(0).getPetName());
    Assert.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName());
  } finally {
    sqlSession.close();
  }
}
 
Example #16
Source File: AutomappingTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotInheritAutoMappingInherited_InlineNestedResultMap() {
  sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.NONE);
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    Mapper mapper = sqlSession.getMapper(Mapper.class);
    User user = mapper.getUserWithPets_Inline(2);
    Assert.assertEquals(Integer.valueOf(2), user.getId());
    Assert.assertEquals("User2", user.getName());
    Assert.assertNull("should not inherit auto-mapping", user.getPets().get(0).getPetName());
    Assert.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName());
  } finally {
    sqlSession.close();
  }
}
 
Example #17
Source File: AutomappingTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotInheritAutoMappingInherited_ExternalNestedResultMap() {
  sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.NONE);
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    Mapper mapper = sqlSession.getMapper(Mapper.class);
    User user = mapper.getUserWithPets_External(2);
    Assert.assertEquals(Integer.valueOf(2), user.getId());
    Assert.assertEquals("User2", user.getName());
    Assert.assertNull("should not inherit auto-mapping", user.getPets().get(0).getPetName());
    Assert.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName());
  } finally {
    sqlSession.close();
  }
}
 
Example #18
Source File: AutomappingTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotInheritAutoMappingInherited_InlineNestedResultMap() {
  sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.NONE);
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    Mapper mapper = sqlSession.getMapper(Mapper.class);
    User user = mapper.getUserWithPets_Inline(2);
    Assert.assertEquals(Integer.valueOf(2), user.getId());
    Assert.assertEquals("User2", user.getName());
    Assert.assertNull("should not inherit auto-mapping", user.getPets().get(0).getPetName());
    Assert.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName());
  } finally {
    sqlSession.close();
  }
}
 
Example #19
Source File: AutomappingTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldGetAUser() {
  sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.NONE);
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    Mapper mapper = sqlSession.getMapper(Mapper.class);
    User user = mapper.getUser(1);
    Assert.assertEquals("User1", user.getName());
  } finally {
    sqlSession.close();
  }
}
 
Example #20
Source File: DefaultResultSetHandler.java    From mybatis with Apache License 2.0 5 votes vote down vote up
private boolean shouldApplyAutomaticMappings(ResultMap resultMap, boolean isNested) {
  if (resultMap.getAutoMapping() != null) {
    return resultMap.getAutoMapping();
  } else {
    if (isNested) {
      return AutoMappingBehavior.FULL == configuration.getAutoMappingBehavior();
    } else {
      return AutoMappingBehavior.NONE != configuration.getAutoMappingBehavior();
    }
  }
}
 
Example #21
Source File: HierarchicalXMLConfigBuilder.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void settingsElement(XNode context) throws Exception {
    if (context != null) {
        Properties props = context.getChildrenAsProperties();
        // Check that all settings are known to the configuration class
        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).");
            }
        }
        configuration.setAutoMappingBehavior(AutoMappingBehavior.valueOf(props.getProperty("autoMappingBehavior", "PARTIAL")));
        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.setUseColumnLabel(booleanValueOf(props.getProperty("useColumnLabel"), true));
        configuration.setUseGeneratedKeys(booleanValueOf(props.getProperty("useGeneratedKeys"), false));
        configuration.setDefaultExecutorType(ExecutorType.valueOf(props.getProperty("defaultExecutorType", "SIMPLE")));
        configuration.setDefaultStatementTimeout(integerValueOf(props.getProperty("defaultStatementTimeout"), null));
        configuration.setDefaultFetchSize(integerValueOf(props.getProperty("defaultFetchSize"), null));
        configuration.setMapUnderscoreToCamelCase(booleanValueOf(props.getProperty("mapUnderscoreToCamelCase"), false));
        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.setSafeResultHandlerEnabled(booleanValueOf(props.getProperty("safeResultHandlerEnabled"), true));
        configuration.setDefaultScriptingLanguage(resolveClass(props.getProperty("defaultScriptingLanguage")));
        configuration.setCallSettersOnNulls(booleanValueOf(props.getProperty("callSettersOnNulls"), false));
        configuration.setLogPrefix(props.getProperty("logPrefix"));
        configuration.setLogImpl(resolveClass(props.getProperty("logImpl")));
    }
}
 
Example #22
Source File: AutomappingTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotInheritAutoMappingInherited_ExternalNestedResultMap() {
  sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.NONE);
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    Mapper mapper = sqlSession.getMapper(Mapper.class);
    User user = mapper.getUserWithPets_External(2);
    Assert.assertEquals(Integer.valueOf(2), user.getId());
    Assert.assertEquals("User2", user.getName());
    Assert.assertNull("should not inherit auto-mapping", user.getPets().get(0).getPetName());
    Assert.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName());
  } finally {
    sqlSession.close();
  }
}
 
Example #23
Source File: AutomappingTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldGetBooks() {
  // set automapping to default partial
  sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.PARTIAL);
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    Mapper mapper = sqlSession.getMapper(Mapper.class);
    // no errors throw
    List<Book> books = mapper.getBooks();
    Assert.assertTrue("should return results,no errors throw", !books.isEmpty());
  } finally {
    sqlSession.close();
  }
}
 
Example #24
Source File: AutomappingTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldRespectFullAutoMappingBehavior_ExternalNestedResultMap() {
  sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.FULL);
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    Mapper mapper = sqlSession.getMapper(Mapper.class);
    User user = mapper.getUserWithPets_External(2);
    Assert.assertEquals(Integer.valueOf(2), user.getId());
    Assert.assertEquals("User2", user.getName());
    Assert.assertEquals("Chien", user.getPets().get(0).getPetName());
    Assert.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName());
  } finally {
    sqlSession.close();
  }
}
 
Example #25
Source File: XMLConfigBuilder.java    From mybatis with Apache License 2.0 4 votes vote down vote up
private void settingsElement(XNode context) throws Exception {
  if (context != null) {
    Properties props = context.getChildrenAsProperties();
    // Check that all settings are known to the configuration class
    //检查下是否在Configuration类里都有相应的setter方法(没有拼写错误)
    MetaClass metaConfig = MetaClass.forClass(Configuration.class);
    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).");
      }
    }
    
    //下面非常简单,一个个设置属性
    //如何自动映射列到字段/ 属性
    configuration.setAutoMappingBehavior(AutoMappingBehavior.valueOf(props.getProperty("autoMappingBehavior", "PARTIAL")));
    //缓存
    configuration.setCacheEnabled(booleanValueOf(props.getProperty("cacheEnabled"), true));
    //proxyFactory (CGLIB | JAVASSIST)
    //延迟加载的核心技术就是用代理模式,CGLIB/JAVASSIST两者选一
    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.setUseColumnLabel(booleanValueOf(props.getProperty("useColumnLabel"), true));
    //允许 JDBC 支持生成的键
    configuration.setUseGeneratedKeys(booleanValueOf(props.getProperty("useGeneratedKeys"), false));
    //配置默认的执行器
    configuration.setDefaultExecutorType(ExecutorType.valueOf(props.getProperty("defaultExecutorType", "SIMPLE")));
    //超时时间
    configuration.setDefaultStatementTimeout(integerValueOf(props.getProperty("defaultStatementTimeout"), null));
    //是否将DB字段自动映射到驼峰式Java属性(A_COLUMN-->aColumn)
    configuration.setMapUnderscoreToCamelCase(booleanValueOf(props.getProperty("mapUnderscoreToCamelCase"), false));
    //嵌套语句上使用RowBounds
    configuration.setSafeRowBoundsEnabled(booleanValueOf(props.getProperty("safeRowBoundsEnabled"), false));
    //默认用session级别的缓存
    configuration.setLocalCacheScope(LocalCacheScope.valueOf(props.getProperty("localCacheScope", "SESSION")));
    //为null值设置jdbctype
    configuration.setJdbcTypeForNull(JdbcType.valueOf(props.getProperty("jdbcTypeForNull", "OTHER")));
    //Object的哪些方法将触发延迟加载
    configuration.setLazyLoadTriggerMethods(stringSetValueOf(props.getProperty("lazyLoadTriggerMethods"), "equals,clone,hashCode,toString"));
    //使用安全的ResultHandler
    configuration.setSafeResultHandlerEnabled(booleanValueOf(props.getProperty("safeResultHandlerEnabled"), true));
    //动态SQL生成语言所使用的脚本语言
    configuration.setDefaultScriptingLanguage(resolveClass(props.getProperty("defaultScriptingLanguage")));
    //当结果集中含有Null值时是否执行映射对象的setter或者Map对象的put方法。此设置对于原始类型如int,boolean等无效。 
    configuration.setCallSettersOnNulls(booleanValueOf(props.getProperty("callSettersOnNulls"), false));
    //logger名字的前缀
    configuration.setLogPrefix(props.getProperty("logPrefix"));
    //显式定义用什么log框架,不定义则用默认的自动发现jar包机制
    configuration.setLogImpl(resolveClass(props.getProperty("logImpl")));
    //配置工厂
    configuration.setConfigurationFactory(resolveClass(props.getProperty("configurationFactory")));
  }
}
 
Example #26
Source File: XMLConfigBuilder.java    From mybaties with Apache License 2.0 4 votes vote down vote up
private void settingsElement(XNode context) throws Exception {
  if (context != null) {
    Properties props = context.getChildrenAsProperties();
    // Check that all settings are known to the configuration class
    //检查下是否在Configuration类里都有相应的setter方法(没有拼写错误)
    MetaClass metaConfig = MetaClass.forClass(Configuration.class);
    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).");
      }
    }
    
    //下面非常简单,一个个设置属性
    //如何自动映射列到字段/ 属性
    configuration.setAutoMappingBehavior(AutoMappingBehavior.valueOf(props.getProperty("autoMappingBehavior", "PARTIAL")));
    //缓存
    configuration.setCacheEnabled(booleanValueOf(props.getProperty("cacheEnabled"), true));
    //proxyFactory (CGLIB | JAVASSIST)
    //延迟加载的核心技术就是用代理模式,CGLIB/JAVASSIST两者选一
    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.setUseColumnLabel(booleanValueOf(props.getProperty("useColumnLabel"), true));
    //允许 JDBC 支持生成的键
    configuration.setUseGeneratedKeys(booleanValueOf(props.getProperty("useGeneratedKeys"), false));
    //配置默认的执行器
    configuration.setDefaultExecutorType(ExecutorType.valueOf(props.getProperty("defaultExecutorType", "SIMPLE")));
    //超时时间
    configuration.setDefaultStatementTimeout(integerValueOf(props.getProperty("defaultStatementTimeout"), null));
    //是否将DB字段自动映射到驼峰式Java属性(A_COLUMN-->aColumn)
    configuration.setMapUnderscoreToCamelCase(booleanValueOf(props.getProperty("mapUnderscoreToCamelCase"), false));
    //嵌套语句上使用RowBounds
    configuration.setSafeRowBoundsEnabled(booleanValueOf(props.getProperty("safeRowBoundsEnabled"), false));
    //默认用session级别的缓存
    configuration.setLocalCacheScope(LocalCacheScope.valueOf(props.getProperty("localCacheScope", "SESSION")));
    //为null值设置jdbctype
    configuration.setJdbcTypeForNull(JdbcType.valueOf(props.getProperty("jdbcTypeForNull", "OTHER")));
    //Object的哪些方法将触发延迟加载
    configuration.setLazyLoadTriggerMethods(stringSetValueOf(props.getProperty("lazyLoadTriggerMethods"), "equals,clone,hashCode,toString"));
    //使用安全的ResultHandler
    configuration.setSafeResultHandlerEnabled(booleanValueOf(props.getProperty("safeResultHandlerEnabled"), true));
    //动态SQL生成语言所使用的脚本语言
    configuration.setDefaultScriptingLanguage(resolveClass(props.getProperty("defaultScriptingLanguage")));
    //当结果集中含有Null值时是否执行映射对象的setter或者Map对象的put方法。此设置对于原始类型如int,boolean等无效。 
    configuration.setCallSettersOnNulls(booleanValueOf(props.getProperty("callSettersOnNulls"), false));
    //logger名字的前缀
    configuration.setLogPrefix(props.getProperty("logPrefix"));
    //显式定义用什么log框架,不定义则用默认的自动发现jar包机制
    configuration.setLogImpl(resolveClass(props.getProperty("logImpl")));
    //配置工厂
    configuration.setConfigurationFactory(resolveClass(props.getProperty("configurationFactory")));
  }
}