Mybatis resultMap空值映射问题解决
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
Mybatis resultMap空值映射问题解决的更多相关文章
- Mybatis resultMap空值映射问题
参考博客:https://www.oschina.net/question/1032714_224673 http://stackoverflow.com/questions/22852383/how ...
- Mybatis中输出映射resultType与resultMap的区别
Mybatis中输出映射resultType与resultMap的区别 (原文地址:http://blog.csdn.net/acmman/article/details/46509375) 一.re ...
- MyBatis 的 XML 映射文件使用说明
简介 文档参考地址:http://www.mybatis.org/mybatis-3/zh/index.html MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器 ...
- Mybatis系列全解(五):全网最全!详解Mybatis的Mapper映射文件
封面:洛小汐 作者:潘潘 若不是生活所迫,谁愿意背负一身才华. 前言 上节我们介绍了 < Mybatis系列全解(四):全网最全!Mybatis配置文件 XML 全貌详解 >,内容很详细( ...
- 5.Mybatis的输出映射(就是对查询的结果集的映射)
Mybatis的输出映射,也就是对查询结果集的一个映射,主要有两种: 1.resultType(不需要配置,可以直接用) 一般是实体类 基本类型也可以 2.resultMap(需要配置resultMa ...
- MyBatis框架——关系映射(一对多、多对多、多对一查询)
关系映射 一.映射(多)对一.(一)对一的关联关系 1).使用列的别名 ①.若不关联数据表,则可以得到关联对象的id属性 ②.若还希望得到关联对象的其它属性.则必须关联其它的数据表 1.创建表: 员工 ...
- mybatis 根据参数映射对应模型
ORM 框架的优势在于能让我们利用面向对象的思维去操作数据库, hibernate 作为重量级的 ORM 框架对面向对象的支持很强大.作为半自动化的 mybatis ,对面向对象的支持也是很完备的.这 ...
- Mybatis中输入输出映射和动态Sql
一.输入映射 我们通过配置parameterType的值来指定输入参数的类型,这些类型可以是简单数据类型.POJO.HashMap等数据类型 1.简单类型 2.POJO包装类型 ①这是单表查询的时候传 ...
- MyBatis实战之映射器
映射器是MyBatis最强大的工具,也是我们使用MyBatis时用得最多的工具,因此熟练掌握它十分必要.MyBatis是针对映射器构造的SQL构建的轻量级框架,并且通过配置生成对应的JavaBean返 ...
随机推荐
- iOS PickerView动态加载数据
将新的数据放入临时数组 NSMutableArray *tmp=[[NSMutableArray alloc] init]; [tmp addObject:[[NSString alloc] init ...
- Excel 备忘
1.如何统计一列中数值重复出现的次数: 在A列旁边插入一B列,在B1中写入公式 =countif(A:A,A1),然后下拉到A列没有数据为止,这样B列中出现的数字就是重复次数了. 2.如何将EXCEL ...
- iftop
http://book.51cto.com/art/201409/452431.htm https://wiki.vpsmm.com/iftop/ http://www.cnblogs.com/Alo ...
- 在Android中将子View的坐标转换为父View的坐标
在Android中,我们有时候可能会将子View的坐标转换为父View中的坐标.感觉很有用,分享给大家. 在Launcher中有这么一段代码可以完成这项工作. public float getDes ...
- Xamarin.Android开发实践(六)
Xamarin.Android通知详解 一.发送通知的机制 在日常的app应用中经常需要使用通知,因为服务.广播后台活动如果有事件需要通知用户,则需要通过通知栏显示,而在Xamarin.Android ...
- Linux常用命令_(网络管理)
网络信息:hostname.netstat.route.ifconfig网络配置:netconfig网络测试:ping hostname–查看主机名称ifconfig–查看和设置网络配置–ifconf ...
- ClassPathXmlApplicationContext的启动
Spring将ApplicationContext启动的全过程,refresh函数中包含了几乎ApplicationContext中提供的全部功能,而且此函数中逻辑非常清晰明了,很容易分析对应的层次及 ...
- Ajax 学习之动态获取,返回服务器的值
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- Ajax 学习之创建XMLHttpRequest对象------Ajax的核心
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- POJ2516 Minimum Cost(最小费用最大流)
一开始我把每个店主都拆成k个点,然后建图..然后TLE.. 看题解= =哦,愚钝了,k个商品是独立的,可以分别跑k次最小费用最大流,结果就是k次总和.. #include<cstdio> ...