转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. 洛谷P4141消失之物(背包经典题)——Chemist

    题目地址:https://www.luogu.org/problemnew/show/P4141 分析:这题当然可以直接暴力枚举去掉哪一个物品,然后每次暴力跑一遍背包,时间复杂度为O(m*n^2),显 ...

  2. 洛谷P2574 XOR的艺术(线段树)——Chemist

    当线段树遇上无敌位运算! 还是老套路,线段树维护区间和,一个区间有几个"1"就是这个区间的区间和,同时支持区间修改区间查询,只不过操作从加法变成了异或.主要难点就在更新懒标记那里, ...

  3. linux php5.6 提示 could not find driver

    1.进入在PHP源码包中进入ext/pdo_mysql # wget http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz 2.然后是解压缩. # tar -zxvf ...

  4. 循环队列 分类: c/c++ 2014-10-10 23:28 605人阅读 评论(0) 收藏

    利用线性表实现队列,为了有效利用空间,将其设计为循环结构,防止假溢出:牺牲一个存储单元以区分队空.队满. 设front队头,rear队尾,N为顺序表大小 队空:rear==front 队满:(rear ...

  5. C. Dasha and Password 预处理 + dp

    http://codeforces.com/contest/761/problem/C 对于每一个字符串,可以预处理出其到达数字,字母,和特殊符号所需的最小步数. 然后就是在n个东西中,选出数字.字母 ...

  6. Solr中的group与facet的区别 [转]

    Solr中的group与facet的区别 facet 自己理解就是分组聚合用的, 如下说明 http://blog.csdn.net/a925907195/article/details/472572 ...

  7. [转]Oracle - 数据库的实例、表空间、用户、表之间关系

    本文转自:http://www.cnblogs.com/adforce/p/3312252.html 完整的Oracle数据库通常由两部分组成:Oracle数据库和数据库实例. 1) 数据库是一系列物 ...

  8. ASP.NET中图片验证码与js获取验证码的值

    现在的程序中,为了防止用户恶意点击,我们一般都会加上验证,现在比较普遍的是加上图片验证码或者手机短信验证.验证码一般都是防机器不防人,有效的防止了恶意点击. 那么在webform中如何生成动态的图片验 ...

  9. Abp Framework中文文档上线

    感谢 ABP框架中国小组 给我们带来的ABP中文翻译,Web+为方便广大学习爱好者随时查阅,现推出了Gitbook风格的在线阅读文档:http://www.webplus.org.cn/documen ...

  10. skeljs框架关键点使用

    global  全局 style.css    containers: 1400px;容器宽度 xlarge  超大屏(media: max-width:1680px) style-xlarge.cs ...