100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > map mybatis 的字段返回0_mybatis返回map类型数据空值字段不显示(三种解决方法)

map mybatis 的字段返回0_mybatis返回map类型数据空值字段不显示(三种解决方法)

时间:2020-04-29 03:22:33

相关推荐

map mybatis 的字段返回0_mybatis返回map类型数据空值字段不显示(三种解决方法)

mybatis的配置 mybatis-config.xml

2,springBoot配置 application.properties 添加

#mybatis resultType equal map void null value

mybatis.configuration.call-setters-on-nulls=true

3,转换成JSON时

//导包

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.serializer.SerializerFeature;

//代码

JSON.toJSONString(new HashMap<>(),SerializerFeature.WriteMapNullValue);

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

IFNULL(rate,'') as rate二、ResultType利用实体返回,不用map

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

/p>

"/dtd/mybatis-3-config.dtd">

spring-mybatis.xml

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

@Overridepublic String getResult(ResultSet rs, String columnName) throwsSQLException {return (rs.getString(columnName) == null) ? "": rs.getString(columnName);

}

@Overridepublic String getResult(ResultSet rs, int columnIndex) throwsSQLException {return (rs.getString(columnIndex) == null) ? "": rs.getString(columnIndex);

}

@Overridepublic String getResult(CallableStatement cs, int columnIndex) throwsSQLException {return (cs.getString(columnIndex) == null) ? "": cs.getString(columnIndex);

}

@Overridepublic void setParameter(PreparedStatement ps, int arg1, String str, JdbcType jdbcType) throwsSQLException { }}在sql.xml文件定义与使用如下如下

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。