Mybatis查询数据,返回resultType="map"时,如果数据为空的字段,则该字段省略不显示 问题解决方案


1. 返回时实体类 

例:  resultType="com.li.job.entity.ShopInfo"

2. 配置 mybatis.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>
        <!--解决,查询返回结果含null没有对应字段值问题-->
        <setting name="callSettersOnNulls" value="true"/>
    </settings>
</configuration>

把文件添加到 配置文件中

Spring 配置 : 

 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="configLocation" value="classpath:mybatis.xml"/>
 </bean>

SpringBoot 配置 :

mybatis.configLocation=classpath:mybatis.xml

3. 查询sql添加每个字段的判断空

 IFNULL(shop_name,"") as shop_name ,使用该方法在查询的时候需要判断每一个字段,使用不方便 。

例:  select IFNULL(shop_name,'') as shop_name from shop_info


 

猜你喜欢

转载自blog.csdn.net/lm9521/article/details/81093016
今日推荐