mybatis返回map类型数据空值字段不显示(三种解决方法)
转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类型数据空值字段不显示(三种解决方法)的更多相关文章
- mybatis返回map类型数据空值字段不显示的解决方法
在日常开发中,查询数据返回类型为map,数据库中有些自动值为null,则返回的结果中没有值为空的字段,则如何显示值为空的字段呢? Spring boot + MyBatis返回map中null值默认不 ...
- 【ibatis】IBatis返回map类型数据
有时侯不想创建javabean,或者污染现有的javaBean对象,就需要返回Map类型的数据对象: 1)最简单的方法就是将查询到的字段,使用""进行引起来,这样就可以返回map类 ...
- MyBatis探究-----返回Map类型数据
1.使用@MapKey @MapKey:告诉mybatis封装Map的时候使用哪个属性作为Map的key Map<K, V>:键是这条记录的主键key,值是记录封装后的javaBean 1 ...
- mybatis返回Date类型数据 格式化
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") public Date getC ...
- MyBatis 传一个类型为String的参数时常见问题及解决方法
MyBatis要求如果参数为String的话,不管接口方法的形参是什么,在Mapper.xml中引用时需要改变为_parameter才能识别 : <select id="selectB ...
- 数据分页 THINKPHP3.2 分页 三种分页方法
数据分页 复制本页链接 opensns 通常在数据查询后都会对数据集进行分页操作,ThinkPHP也提供了分页类来对数据分页提供支持. 下面是数据分页的两种示例. 第一种:利用Page类和limit方 ...
- JMeter接口响应数据出现乱码的三种解决方法
第一种方法: Content encoding设置为utf-8,若仍为乱码,请用方法2 图1 第二种方法: 修改bin文件夹下的jmeter.properties文件 搜索ISO,把“#sampler ...
- MyBatis 返回 Map 字段丢失问题
问题现象 执行存储过程返回 Map 集合数据,发现有字段丢失情况,仔细研究发现丢失的字段值都为 NULL. 解决办法1: 在查询 SQL 语句中增加 NULL 判断函数 MSSQL: isnull(字 ...
- Struts2+Jquery实现ajax并返回json类型数据
来源于:http://my.oschina.net/simpleton/blog/139212 摘要 主要实现步骤如下: 1.JSP页面使用脚本代码执行ajax请求 2.Action中查询出需要返回的 ...
随机推荐
- IP地址简单入门
------------------------针对网络地址相关的小白,最快速接触网络知识------------------------- 可以使用python自带的模块IPy,进行处理IP地址或I ...
- CF916E
Codeforces 916E 简要题解Description Description 有一棵n个点的树,每个节点上有一个权值wi,最开始根为1号点.现在有3种类型的操作: 1 root, 表示将根设 ...
- 校赛F 比比谁更快(线段树)
http://acm.cug.edu.cn/JudgeOnline/problem.php?cid=1153&pid=5 题意:给你一个字符串,各两个操作: ch=0,[l,r]降序 ch=1 ...
- 洛谷 P1816 忠诚
https://www.luogu.org/problemnew/show/1816 st表模板 #include<cstdio> #include<algorithm> us ...
- 基于CentOS6.5或Ubuntu14.04下Suricata里搭配安装 ELK (elasticsearch, logstash, kibana)(图文详解)
前期博客 基于CentOS6.5下Suricata(一款高性能的网络IDS.IPS和网络安全监控引擎)的搭建(图文详解)(博主推荐) 基于Ubuntu14.04下Suricata(一款高性能的网络ID ...
- 转】R利剑NoSQL系列文章 之 Cassandra
原博文出自于: http://blog.fens.me/category/%E6%95%B0%E6%8D%AE%E5%BA%93/page/3/ 感谢! R利剑NoSQL系列文章 之 Cassandr ...
- Ref 和 Out 区别(演练代码)
一.代码 今天就总结Ref和Out 的总结,这东西,也是经常面试过程中,笔试经常考的,比如:请简述Ref和Out 的区别,或者通过一段代码让你计算这过程的结果.... Out代码实例::: stati ...
- 读取.properties配置信息
package com.ctcti.webcallcenter.utils; import java.io.FileInputStream;import java.io.FileNotFoundExc ...
- 前端phtooshop基础
1.图片理论基础 2.使用Adobe FireWorks切图和S0VG的处理 可以单独生成一个图片的切图 选择多个切图部分生成CSS Sprite,甚至CSS和html都生成了对应的文件. 3.Ph ...
- 用Python控制摄像头拍照并发邮件
概述前言 工具 思路 安装及导入包 设置参数 实现拍照 构造邮件内容 发送邮件 判断网络连接 开机自启 后记 o1 前言为什么会有写这个程序的想法呢? 最初的想法是写一个可以用电脑前置摄像头拍照的程序 ...