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]
相关推荐
OLDERHARD4 小时前
Java - MyBatis(上)
java·oracle·mybatis
计算机学姐16 小时前
基于SpringBoot+Vue的高校运动会管理系统
java·vue.js·spring boot·后端·mysql·intellij-idea·mybatis
我是浮夸18 小时前
MyBatisPlus——学习笔记
java·spring boot·mybatis
编程、小哥哥1 天前
手写mybatis之Mapper XML的解析和注册使用
xml·java·mybatis
小桑要读研1 天前
Redis实现点赞
java·mysql·mybatis
真的想不出名儿2 天前
04-SpringBootWeb案例(下)
mybatis·spingbootweb
舞者H2 天前
如何实现Mybatis自定义插件
java·mybatis
程序员大金2 天前
基于SpringBoot+Vue+MySQL的考勤管理系统
java·javascript·vue.js·spring boot·后端·mysql·mybatis
beiback3 天前
Springboot + netty + rabbitmq + myBatis
spring boot·mysql·rabbitmq·mybatis·netty·java-rabbitmq
wclass-zhengge3 天前
Redis篇(缓存机制 - 基本介绍)(持续更新迭代)
redis·缓存·mybatis