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)
相关文章推荐
- mybatis返回map类型数据空值字段不显示(三种解决方法)
- mybatis返回map类型数据空值字段不显示(三种解决方法)
- mybatis返回map类型数据空值字段不显示(三种解决方法)
- mybatis返回map类型数据空值字段不显示(三种解决方法)
- mybatis返回map类型数据空值字段不显示(三种解决方法)
- mybatis返回map类型数据空值字段不显示(三种解决方法)
- mybatis返回map类型数据空值字段不显示
- springMVC+mybatis查询数据,返回resultType=”map”时,如果数据为空的字段,则该字段省略不显示的解决方案
- Mybatis_ 返回List<Map<K,V>> 结构类型数据
- 获取一个网页数据返回的编码类型是gzip,解压后,网页中包含的中文字段变成了乱码,只需要把编码更改为BIG5 ,繁体字就正常显示了!
- MyBatis查询返回Map类型数据
- mybatis查询如何返回List<Map>类型数据
- 模拟实现SQL Server字段列显示的数据类型
- mybatis 返回类型为HashMap 字段为空
- 将泛型集合类(List<T>)转换成DataTable,返回的DataTable所有列的数据类型为string型,且列顺序与字符串提供的字段顺序一致
- IBatis返回map类型数据
- mybatis 返回map 一个项目使用另一个项目表中的三列数据
- hibernate查询数据表char类型字段只返回一个字符
- Java技术-将java中Map类型数据转化为json数据并以Ajax形式返回
- mybatis查询类型为int的字段,返回null的异常