Mybatis在使用resultMap来映射查询结果中的列,如果查询结果中包含空值的列(不是null),则Mybatis在映射的时候,不会映射这个字段,例如 查询 name,sex,age,数据库中的age字段没有值,Mybatis返回的map中只映射了 name和sex字段,而age字段则没有包含。

那么如何将age字段映射到map中呢。提供两种解决方法:

使用Mybatis config配置

创建configuration.xml

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD SQL MAP Config 3.1//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
  <settings>
      <setting name="callSettersOnNulls" value="true"/>
  </settings>
</configuration>

配置Mybatis的SqlSessionFactoryBean

1
2
3
4
5
6
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="classpath:/META-INF/spring/configuration.    xml" />
    <property name="mapperLocations"
    value="classpath:/META-INF/spring/mybatis/modelMap/*.xml" />
</bean>

在这种配置中,age将以null值映射到map中。

如果想要配置age的默认值,则可以建立一个类,实现Mybatis的TypeHandler接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class EmptyStringIfNull implements TypeHandler<String> {
 
    @Override
    public String getResult(ResultSet rs, String columnName) throws SQLException {
     return (rs.getString(columnName) == null) ? "" : rs.getString(columnName);
    }
 
    @Override
    public String getResult(ResultSet rs, int columnIndex) throws SQLException {
     return (rs.getString(columnIndex) == null) ? "" : rs.getString(columnIndex);
    }
    <span></span>@Override
    public String getResult(CallableStatement cs, int columnIndex)   throws SQLException {
     return (cs.getString(columnIndex) == null) ? "" : cs.getString(columnIndex);
    }
    @Override
    public void setParameter(PreparedStatement ps, int arg1, String str, JdbcType jdbcType) <span></span><span style="font-size:9pt;line-height:1.5;">throws SQLException {</span><span style="font-size:9pt;line-height:1.5;"> }</span><span style="font-size:9pt;line-height:1.5;">}</span>

继续在resultMap中使用,即可配置age的默认值(上述代码中age的默认值为"")

1
2
3
4
5
<resultMap id="list" type="java.util.LinkedHashMap">
    <result property="name" column="name" />
    <result property="sex" column="sex" />
    <result property="age" column="age" typeHandler="com.demo.EmptyStringIfNull"/>
</resultMap>

网上有些资料中提到可以使用 defaultValue 和 nullValue的配置,但是这中配置是ibatis的用法,在Mybatis中已经移除。

参考链接http://stackoverflow.com/questions/22852383/how-to-change-valuenull-to-empty-string-from-query-when-using-mybatis

最新文章

  1. log4j属性详解
  2. HDU2205 又见回文(区间DP)
  3. [刘阳Java]_Java程序员的成长路线_第3讲
  4. Python静态方法的使用
  5. 基于HtmlUnit的模板的网页数据抽取
  6. 查看Linux声卡基本信息[转载]
  7. Onthink学习随笔
  8. Genealogical tree(拓扑结构+邻接表+优先队列)
  9. 自定义Base16加密
  10. 计蒜客NOIP模拟赛(3) D1T2 信息传递
  11. linux内核中默认logo的具体位置
  12. 使用SpringSecurity体验OAUTH2之一 (入门1)
  13. 在MSYS2环境下 用msvc 编译 zlib
  14. JQuery 的Ajax的使用
  15. openstack 之~keystone基础
  16. Struts2学习第四天——拦截器及文件上传
  17. 《机器学习实战(基于scikit-learn和TensorFlow)》第三章内容的学习心得
  18. JSON WEB TOKEN,简单谈谈TOKEN的使用及在C#中的实现
  19. 网易云课堂Dubbo学习笔记
  20. [To Selina] 《撒野》读后感

热门文章

  1. Fedora 启动sshd服务:
  2. 【读书笔记】读《JavaScript高级程序设计-第2版》 - 函数部分
  3. RedHat6.2 下RRDTool安装方法
  4. php 工厂模式
  5. C#对XML进行操作(添加、修改)
  6. Source Insight 多标签插件
  7. pl/sql developer 登陆提示ORA-12514(转)
  8. Python开发的10个小贴士
  9. 在虚拟机环境下,电脑间拷贝配置好的伪分布式Hadoop环境,出现namenode不能启动的问题!
  10. Messenger信使