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]
相关推荐
三天不学习7 小时前
Lucene.Net 分词器选择指南:盘古分词 vs 结巴分词
.net·mybatis·lucene
要天天开心啊15 小时前
mybatis的第四天学习笔记中
笔记·学习·mybatis
Determined_man18 小时前
Mybatis-plus listObjs()
mybatis
Yharim1 天前
MyBatis中如何实现自定义插件
面试·mybatis
Y第五个季节1 天前
Redis 持久化
数据库·redis·mybatis
八股文领域大手子1 天前
《从 MyBatis-Plus 到 Elasticsearch:一个后端的性能优化踩坑实录》
elasticsearch·性能优化·mybatis
一切皆有迹可循1 天前
IntelliJ IDEA中Spring Boot项目整合MyBatis:从零实现高效数据持久化
java·spring boot·intellij-idea·mybatis
独泪了无痕2 天前
MyBatis中特殊符号处理总结
后端·mybatis
快乐的木子李2 天前
Java spring mybatis面试题(200道),八股文
java·开发语言·spring·mybatis