Spring+MyBatis开发过程中,在xxMapper.xml配置文件进行select查询时resultType="map",如果要查询的字段是空值,在返回的map中会出现找不到这个字段对应的属性。要解决这个问题需要指定 callSettersOnNulls 属性,具体过程如下:
1,创建xml配置文件。
在applicationContext.xml 同一级目录中创建文件mybatis-config.xml,内容如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="callSettersOnNulls" value="true"/>
</settings>
...
</configuration>
2,在applicationContext-mybatis.xml文件中配置 mybatis-config.xml的引用,部分代码如下:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
p:dataSource-ref="dataSource" p:configLocation="classpath:mybatis-config.xml"
p:mapperLocations="classpath:mybatis/mapper/*.xml" />
3,重新部署项目。