mybatis返回map类型数据空值字段不显示

最近自己摸索一下SpringMVC+Mybatis框架(小菜鸟一枚),自己搭建完环境,在做一个简单的小系统,遇到一些小问题,分享一下(纰漏之处,欢迎指正),废话不叨叨,开始正题:
自使用mybatis为持久层框架的时候,对于字段为空时返回结果集(返回类型为HashMap)会自动屏蔽掉该字段(结果集的Map中没有该字段名),mybatis的settings为此提供了解决方案--callSettersOnNulls
callSettersOnNulls配置具体如下:
<setting name="callSettersOnNulls" value="true"></setting>
callSettersOnNulls默认为false,其官方文档解释为:                            指定当结果集中值为null的时候是否调用映射对象的setter(map对象时为put)方法,这对于有Map.keySet()依赖或null值初始化的时候是有用的。注意基本类型(int,boolean等)是不能设置成null的。                                     该属性在mybatis在3.1.1版本儿之前(包括该版本)是没有该属性的,具体哪个办版本加入了该属性尚未考证,我现在现在用的是mybatis3.3.0.jar是有该配置的。如果没有该属性会报:org.apache.ibatis.builder.BuilderException: The setting callSettersOnNulls is not known.  Make sure you spelled it correctly (case sensitive).                                                      mybatis3.1.1.jar配置项源码如下图:        
private void settingsElement(XNode context) throws Exception {
    if (context != null) {
      Properties props = context.getChildrenAsProperties();

      MetaClass metaConfig = MetaClass.forClass(Configuration.class);
      for (Iterator i$ = props.keySet().iterator(); i$.hasNext(); ) { Object key = i$.next();
        if (!metaConfig.hasSetter(String.valueOf(key))) {
          throw new BuilderException("The setting " + key + " is not known.  Make sure you spelled it correctly (case sensitive).");
        }
      }
      this.configuration.setAutoMappingBehavior(AutoMappingBehavior.valueOf(stringValueOf(props.getProperty("autoMappingBehavior"), "PARTIAL")));
      this.configuration.setCacheEnabled(booleanValueOf(props.getProperty("cacheEnabled"), Boolean.valueOf(true)).booleanValue());
      this.configuration.setLazyLoadingEnabled(booleanValueOf(props.getProperty("lazyLoadingEnabled"), Boolean.valueOf(false)).booleanValue());
      this.configuration.setAggressiveLazyLoading(booleanValueOf(props.getProperty("aggressiveLazyLoading"), Boolean.valueOf(true)).booleanValue());
      this.configuration.setMultipleResultSetsEnabled(booleanValueOf(props.getProperty("multipleResultSetsEnabled"), Boolean.valueOf(true)).booleanValue());
      this.configuration.setUseColumnLabel(booleanValueOf(props.getProperty("useColumnLabel"), Boolean.valueOf(true)).booleanValue());
      this.configuration.setUseGeneratedKeys(booleanValueOf(props.getProperty("useGeneratedKeys"), Boolean.valueOf(false)).booleanValue());
      this.configuration.setDefaultExecutorType(ExecutorType.valueOf(stringValueOf(props.getProperty("defaultExecutorType"), "SIMPLE")));
      this.configuration.setDefaultStatementTimeout(integerValueOf(props.getProperty("defaultStatementTimeout"), null));
      this.configuration.setMapUnderscoreToCamelCase(booleanValueOf(props.getProperty("mapUnderscoreToCamelCase"), Boolean.valueOf(false)).booleanValue());
      this.configuration.setSafeRowBoundsEnabled(booleanValueOf(props.getProperty("safeRowBoundsEnabled"), Boolean.valueOf(false)).booleanValue());
      this.configuration.setLocalCacheScope(LocalCacheScope.valueOf(stringValueOf(props.getProperty("localCacheScope"), "SESSION")));
      this.configuration.setJdbcTypeForNull(JdbcType.valueOf(stringValueOf(props.getProperty("jdbcTypeForNull"), "OTHER")));
      this.configuration.setLazyLoadTriggerMethods(stringSetValueOf(props.getProperty("lazyLoadTriggerMethods"), "equals,clone,hashCode,toString"));
      this.configuration.setSafeResultHandlerEnabled(booleanValueOf(props.getProperty("safeResultHandlerEnabled"), Boolean.valueOf(true)).booleanValue());
    }
  }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值