转http://blog.csdn.net/lulidaitian/article/details/70941769

一、查询sql添加每个字段的判断空

IFNULL(rate,'') as rate

二、ResultType利用实体返回,不用map

三、springMVC+mybatis查询数据,返回resultType=”map”时,如果数据为空的字段,则该字段省略不显示,可以通过添加配置文件,规定查询数据为空是则返回null。

<?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>

spring-mybatis.xml

<!-- spring和MyBatis完美整合,添加mybatis的配置映射文件 -->

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="dataSource" ref="dataSource"/>

<property name="configLocation" value="classpath:mybatis-configuration.xml"/>

<!-- 自动扫描mapping.xml文件 -->

<property name="mapperLocations" value="classpath:mapping/*.xml"></property>

</bean>

如果想要配置rate的默认值,例如“”字符串,则可以建立一个类,实现Mybatis的TypeHandler接口

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);

}

@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) throws SQLException { }}

在sql.xml文件定义与使用如下如下

<resultMap id="find" type="java.util.LinkedHashMap">

<result property="name" column="name" />

<result property="phone" column="phone" />

<result property="rate" column="rate" typeHandler="com.mybatis.EmptyStringIfNull"/>

</resultMap>

一、查询sql添加每个字段的判断空

IFNULL(rate,'') as rate

二、ResultType利用实体返回,不用map

三、springMVC+mybatis查询数据,返回resultType=”map”时,如果数据为空的字段,则该字段省略不显示,可以通过添加配置文件,规定查询数据为空是则返回null。

<?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>

spring-mybatis.xml

<!-- spring和MyBatis完美整合,添加mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-configuration.xml"/>
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:mapping/*.xml"></property>
</bean>

如果想要配置rate的默认值,例如“”字符串,则可以建立一个类,实现Mybatis的TypeHandler接口

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);
}
@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) throws SQLException { }}

在sql.xml文件定义与使用如下如下

<resultMap id="find" type="java.util.LinkedHashMap">
<result property="name" column="name" />
<result property="phone" column="phone" />
<result property="rate" column="rate" typeHandler="com.mybatis.EmptyStringIfNull"/>
</resultMap>

mybatis返回map类型数据空值字段不显示(三种解决方法)的更多相关文章

  1. mybatis返回map类型数据空值字段不显示的解决方法

    在日常开发中,查询数据返回类型为map,数据库中有些自动值为null,则返回的结果中没有值为空的字段,则如何显示值为空的字段呢? Spring boot + MyBatis返回map中null值默认不 ...

  2. 【ibatis】IBatis返回map类型数据

    有时侯不想创建javabean,或者污染现有的javaBean对象,就需要返回Map类型的数据对象: 1)最简单的方法就是将查询到的字段,使用""进行引起来,这样就可以返回map类 ...

  3. MyBatis探究-----返回Map类型数据

    1.使用@MapKey @MapKey:告诉mybatis封装Map的时候使用哪个属性作为Map的key Map<K, V>:键是这条记录的主键key,值是记录封装后的javaBean 1 ...

  4. mybatis返回Date类型数据 格式化

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") public Date getC ...

  5. MyBatis 传一个类型为String的参数时常见问题及解决方法

    MyBatis要求如果参数为String的话,不管接口方法的形参是什么,在Mapper.xml中引用时需要改变为_parameter才能识别 : <select id="selectB ...

  6. 数据分页 THINKPHP3.2 分页 三种分页方法

    数据分页 复制本页链接 opensns 通常在数据查询后都会对数据集进行分页操作,ThinkPHP也提供了分页类来对数据分页提供支持. 下面是数据分页的两种示例. 第一种:利用Page类和limit方 ...

  7. JMeter接口响应数据出现乱码的三种解决方法

    第一种方法: Content encoding设置为utf-8,若仍为乱码,请用方法2 图1 第二种方法: 修改bin文件夹下的jmeter.properties文件 搜索ISO,把“#sampler ...

  8. MyBatis 返回 Map 字段丢失问题

    问题现象 执行存储过程返回 Map 集合数据,发现有字段丢失情况,仔细研究发现丢失的字段值都为 NULL. 解决办法1: 在查询 SQL 语句中增加 NULL 判断函数 MSSQL: isnull(字 ...

  9. Struts2+Jquery实现ajax并返回json类型数据

    来源于:http://my.oschina.net/simpleton/blog/139212 摘要 主要实现步骤如下: 1.JSP页面使用脚本代码执行ajax请求 2.Action中查询出需要返回的 ...

随机推荐

  1. windows API普通函数跟回调函数有何区别

    通俗点讲:1.普通函数(假设我们都是函数)你卖电脑,我买电脑,我给你钱(调用你)后,你给我电脑(得到返回值).这种情况下,我给钱后就不能走开,必须等你把电脑给我,否则你交货的时候可能找不到人.2.回调 ...

  2. C#画图——Graphics

    C#要实现简单的画图功能可以利用Graphics这个类,要使用Graphics必需using命名空间System.Drawing(此名明空间下都是关于图形的操作).首先创建画布: Bitmap bmp ...

  3. Plugging an Unplugged Pluggable Database issue 3

    Multitenant Unplug/Plug Best Practices (文档 ID 1935365.1) 1.source 从0419 升级到1019 ,但是datapatch 没有回退041 ...

  4. SpringCloud开发学习总结(六)—— 结合注解的AOP示例

    面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型.利用AOP ...

  5. 未来十年Python的前景会怎样?

    转自:一位非常优秀的Python倡导者 作者:alex链接:https://www.zhihu.com/question/22112542/answer/166053516来源:知乎著作权归作者所有. ...

  6. ural1437

    1437 记忆化 模拟倒水过程 #include <iostream> #include<cstdio> #include<cstring> #include< ...

  7. Oracle 的备份和恢复

    Oracle数据库有三种标准的备份方法,它们分别是导出/导入(EXP/IMP).热备份和冷备 份.导出备件是一种逻辑备份,冷备份和热备份是物理备份. 一. 导出/导入(Export/Import) 利 ...

  8. String的用法——其他功能

    package cn.itcast_06; /* String类的其他功能: 替换功能: String replace(char old,char new) String replace(String ...

  9. GIT配置及用法

    ssh配置 TortoiseGit配置 用法: 下面是我整理的常用 Git 命令清单.几个专用名词的译名如下. Workspace:工作区 Index / Stage:暂存区 Repository:仓 ...

  10. Android基础夯实--重温动画(五)之属性动画 ObjectAnimator详解

    只有一种真正的英雄主义 一.摘要 ObjectAnimator是ValueAnimator的子类,它和ValueAnimator一样,同样具有计算属性值的功能,但对比ValueAnimator,它会更 ...