mybatis使用Map返回时数据库为空的字段不返回问题

Mybatis在使用resultMap来映射查询结果中的列,如果查询结果中包含空值的列(不是null),则Mybatis在映射的时候,不会映射这个字段,例如 查询 name,sex,age,数据库中的age字段没有值,Mybatis返回的map中只映射了 name和sex字段,而age字段则没有包含。
那么如何将age字段映射到map中呢。提供四种解决方法:
 
方法一:
  这个也是最简单的解决办法:
  在application.properties中配置
  
mybatis.configuration.call-setters-on-nulls=true
  即可解决
方法二:
      MyBatis 不知道你传入的 null 参数对应的 jdbc 类型是什么,因为在 MyBatis 看来,null 在数据库中可以为多种类型(例如,可以为 CHAR、VARCHAR、DATE 等),于是 MyBatis 就傻眼了,解决办法自然是你要告诉 MyBatis 这个 null 对应的 jdbc 类型是什么。于是得出的答案如下:
      在你相应的 Mapper 中,传入该参数的地方写明 jdbc 类型即可,比如原来传参数是这样写的:#{myNullParameter},把它改成这样写即可:#{myNullParameter, jdbcType=VARCHAR}

方法三:
      使用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) throws SQLException { }}

    继续在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中已经移除。

方法五:

<!-- myBatis配置 -->  
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
    <property name="dataSource" ref="dataSource" /> 
    <property name="mapperLocations">  
        <value></value> 
    </property>  
    <property name="configurationProperties">  
        <props>  
            <prop key="cacheEnabled">true</prop>  
            <prop key="callSettersOnNulls">true</prop>  
        </props>  
    </property>  
</bean>  


Mybatis在使用resultMap来映射查询结果中的列,如果查询结果中包含空值的列(不是null),则Mybatis在映射的时候,不会映射这个字段,例如 查询 name,sex,age,数据库中的age字段没有值,Mybatis返回的map中只映射了 name和sex字段,而age字段则没有包含。
那么如何将age字段映射到map中呢。提供四种解决方法:

猜你喜欢

转载自www.cnblogs.com/guo-xu/p/12548949.html