您的位置:首页 > 其它

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

2017-06-09 09:43 846 查看
最近自己摸索一下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
8f76
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());
}
}
需要服务器的小伙伴儿可以找联邦在线哦(联邦在线-莹莹:80589423)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: