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>  


 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM