MyBatis在字段返回为null不返回字段
来源:互联网 发布:java静态变量怎样泛型 编辑:程序博客网 时间:2024/05/27 14:14
原文地址:http://blog.csdn.net/zkd12344/article/details/53261253参考地址:http://www.360doc.com/content/15/0608/08/281812_476469535.shtml在用mybatis时没有用实体作为返回(用的是Map) 因此出现了 在返回参数为null的时候 不会返回字段,出现情况:在移动端get一个空的字段就会报NullPointerException;解决方案:在mybatis-config.xml中配置 <settings> <setting name="cacheEnabled" value="true"/> <setting name="callSettersOnNulls" value="true"/> </settings> Mybatis resultMap空值映射问题解决Mybatis在使用resultMap来映射查询结果中的列,如果查询结果中包含空值的列(不是null),则Mybatis在映射的时候,不会映射这个字段,例如 查询 name,sex,age,数据库中的age字段没有值,Mybatis返回的map中只映射了 name和sex字段,而age字段则没有包含。那么如何将age字段映射到map中呢。提供两种解决方法:使用Mybatis config配置创建configuration.xml<?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<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接口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) <span style="line-height:1.5;font-size:9pt;">throws SQLException {</span><span style="line-height:1.5;font-size:9pt;"> }</span><span style="line-height:1.5;font-size:9pt;">}</span>继续在resultMap中使用,即可配置age的默认值(上述代码中age的默认值为"")<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中已经移除。
0 0
- MyBatis在字段返回为null不返回字段
- MyBatis在字段返回为null不返回字段
- mybatis 返回HashMap是对字段值为null时不保存key
- MyBatis查询返回Map时设置值为null的字段也在结果集中
- Mybatis字段值为空时,不返回map问题
- mybatis查询,返回类型为HashMap,字段为null时没返回
- Mybatis查询数据库空字段不返回
- Spring boot + MyBatis 查询返回Map时设置值为null的字段也在结果集中
- MyBatis从数据库取值的时候,当某一字段为null时,如何在返回前端的时候,也包含有这个字段
- mybatis查询字段为空返回另一个字段的值
- mybatis查询类型为int的字段,返回null的异常
- mybatis查询类型为int的字段,返回null的异常
- Mybatis返回HashMap时,某个字段值为null时,不会保存key
- Mybatis返回HashMap时,某个字段值为null时,不会保存key
- mybatis查询类型为int的字段,返回null的异常
- Mybatis返回HashMap时,某个字段值为null时,不会保存key
- mybatis查询类型为int的字段,返回null的异常
- Mybatis返回HashMap时,某个字段值为null时,不会保存key
- 检查网络电缆与网卡连接状态的命令
- 设置主机名为www
- Video for linux 2 example (v4l2 demo)
- OpenMP的简单测试 并行化
- javascript之var与function定义的区别
- MyBatis在字段返回为null不返回字段
- python3: error while loading shared libraries: libpython3.5m.so.1.0: cannot open shared object file:
- C++学习小记
- 智能硬件全新开发方式
- 详解Java反射各种应用
- centos 常用命令
- 接下落的金币小游戏
- mybatis返回int类型报错 return null from a method with a primitive return type (int).
- Android 系统apk编译源代码的时候如何引用android-support-v4.jar 在Android.mk