mybatis设置select返回HashMap,字段值为null时,不会保存key

说明:使用mybatis3.0以上版本时,结果集设置为HashMap,返回的字段值为null时,不会显示key,因为callSettersOnNulls默认为false

1、mapping.xml

1 <resultMap type="java.util.HashMap" id="BaseResultMap">
2   <result column="t_product_id" property="id" jdbcType="INTEGER" />
3   <result column="t_product_name" property="name" jdbcType="VARCHAR" />
4   <result column="t_product_price" property="price" jdbcType="FLOAT" />
5 </resultMap>

 

2、mybatis-config.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE configuration
 3   PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4   "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6     <settings>
 7         <!-- 设置返回HashMap,字段值为null时保存key,如果不设置默认是false -->
 8         <setting name="callSettersOnNulls" value="true" />
 9     </settings>
10 </configuration>

3、spring-mybatis.xml

1 <!-- spring和MyBatis整合,不需要MyBatis的配置映射文件 -->
2     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
3         <property name="dataSource" ref="dataSource" />
4         <!-- 自动扫描mapping.xml文件 -->
5         <property name="mapperLocations" value="classpath:com/cn/stock/mapping/*.xml" />
6         <!-- 配置MyBatis全局配置文件 -->
7         <property name="configLocation" value="classpath:mybatis-config.xml"></property>
8     </bean>

 

posted @ 2017-07-19 14:21  Mr.攻城师  阅读(1801)  评论(1编辑  收藏  举报