有时候进行数据库查询操作的时候,查询结果中一条参数或者有某几个参数为null,这种情况下,参数名都不会返回,解决办法如下:
第一种:mybatis返回值resultType="map" 改成实体类返回
第二种:还是用map接收,默认查询为控的字段不显示,
所以在mybatisConfig.xml配置文件中加上 <setting name="callSettersOnNulls" value="true"/>
<?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>
<!--解决,查询返回结果含null没有对应字段值问题-->
<setting name="callSettersOnNulls" value="true"/>
</settings>
</configuration>
然后在spring的配置applicationContext.xml中加上这一段
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath*:mappers/*Mapper.xml"></property>
<property name="configLocation">
<value>classpath:mybatisConfig.xml</value>
</property>
</bean>
第三种:
1、postgresql数据库:
select COALESCE(D.wk_ptn_cd, '00') as wk_ptn_cd
判断字段wk_ptn_cd是否为空,空给默认值‘00’,否则取该字段
2、非postgresql数据库:Mybatis使用IFNULL(P1,P2)函数