5.11 mybatis之returnInstanceForEmptyRow作用

文章目录

  • [1. 当returnInstanceForEmptyRow=true时](#1. 当returnInstanceForEmptyRow=true时)
  • [2 当returnInstanceForEmptyRow=false时](#2 当returnInstanceForEmptyRow=false时)

mybatis的settings配置中有个属性returnInstanceForEmptyRow,该属性新增于mybatis的3.4.2版本,低于此版本不可用。该属性的作用官方解释为:当返回行的所有列都是空时,MyBatis默认返回 null。 当开启这个设置时,MyBatis会返回一个空实例。 请注意,它也适用于嵌套的结果集(如集合或关联)。该属性默认配置为false。如何理解上面这句话?
假设有一个库表名字为CAR,该表有2列,分别为NAME和BRAND,其中有一条数据,NAME列和BRAND列都为NULL,当配置returnInstanceForEmptyRow=false时,返回的car对象也为NULL;当returnInstanceForEmptyRow=true时,返回的car对象是非NULL对象,只不过是这个实例对象中brand属性和name属性都为NULL。通过下面的例子就可明白

1. 当returnInstanceForEmptyRow=true时

我们以CAR表为例,表中有2条数据,其中有一条name列和brand列都为NULL。

下面查询name列为NULL的数据,对应的mapper如下所示

xml 复制代码
<select id="select2" resultType="com.lzj.bean.Car">
    select * from car where name is null
</select>

对应的DAO为

java 复制代码
package com.lzj.dao;

import com.lzj.bean.Car;
import java.util.List;

public interface CarDao {
    public List<Car> select2();
}

然后修改配置文件如下所示,主要是配置returnInstanceForEmptyRow=true

xml 复制代码
<settings>
    <setting name="logImpl" value="STDOUT_LOGGING"/>
    <setting name="returnInstanceForEmptyRow" value="true"/>
</settings>

一切就绪后执行下面的测试方法

java 复制代码
public void sqlSessionTest2(){
    SqlSessionFactory factory = mybatisUtil.getFactory();
    SqlSession sqlSession = factory.openSession(true);  //true表示自动提交
    List<Car> cars = sqlSession.selectList("com.lzj.dao.CarDao.select2");
    System.out.println("个数: " + cars.size());
    System.out.println("cars: " + cars);
    sqlSession.close();
}

执行完测试方法后输出结果如下所示,通过日志可以看出,返回list中有1个car对象,对象中的name和brand属性都为null。

java 复制代码
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Opening JDBC Connection
Created connection 79094208.
==>  Preparing: select * from car where name is null 
==> Parameters: 
<==    Columns: name, brand
<==        Row: null, null
<==      Total: 1
个数: 1
cars: [Car{name='null', brand='null'}]
Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@4b6e1c0]

2 当returnInstanceForEmptyRow=false时

下面看一下当当returnInstanceForEmptyRow=false时返回的是什么对象,当然也可以不配置,因为该参数默认就是false的。还是以上面的例子来说,只是修改mybatis的配置文件如下所示

xml 复制代码
<settings>
    <setting name="logImpl" value="STDOUT_LOGGING"/>
    <setting name="returnInstanceForEmptyRow" value="false"/>
</settings>

然后再次运行上面测试案例输出结果如下所示,返回的list中同样也是只有1个对象,不过这个对象是空对象null。到这里再回到上面理解最开始的解释就一目了然了。

java 复制代码
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Opening JDBC Connection
Created connection 1184248953.
==>  Preparing: select * from car where name is null 
==> Parameters: 
<==    Columns: name, brand
<==        Row: null, null
<==      Total: 1
个数: 1
cars: [null]
Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@46963479]
相关推荐
芯眼3 小时前
STM32启动文件详解(重点)
java·开发语言·c++·stm32·单片机·mybatis
遗憾皆是温柔5 小时前
MyBatis—动态 SQL
java·数据库·ide·sql·mybatis
CircleMouse1 天前
springboot如何通过提供的注解方式来操作Redis
java·spring boot·redis·spring·mybatis
荔枝吻1 天前
【抽丝剥茧知识讲解】引入mybtis-plus后,mapper实现方式
java·sql·mybatis
Allen Bright2 天前
【MyBatis-9】MyBatis分页插件PageHelper深度解析与实践指南
mybatis
柴薪之王、睥睨众生3 天前
(自用)Java学习-5.8(总结,springboot)
java·开发语言·spring boot·学习·mybatis
唐僧洗头爱飘柔95273 天前
【SSM-SSM整合】将Spring、SpringMVC、Mybatis三者进行整合;本文阐述了几个核心原理知识点,附带对应的源码以及描述解析
java·spring·mybatis·springmvc·动态代理·ioc容器·视图控制器
意倾城3 天前
浅说MyBatis-Plus 的 saveBatch 方法
java·mybatis
Brilliant Nemo4 天前
五、框架实战:SSM整合原理和实战
maven·mybatis
小赵面校招4 天前
Spring Boot整合MyBatis全攻略:原理剖析与最佳实践
java·spring boot·mybatis