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]
相关推荐
B站计算机毕业设计超人3 分钟前
计算机毕业设计SpringBoot+Vue.jst房屋租赁系统(源码+LW文档+PPT+讲解)
vue.js·spring boot·后端·eclipse·intellij-idea·mybatis·课程设计
天上掉下来个程小白14 小时前
案例-14.文件上传-简介
数据库·spring boot·后端·mybatis·状态模式
sunnyday04261 天前
MyBatis XML映射文件中的批量插入和更新
xml·java·mysql·mybatis
剑走偏锋o.O2 天前
Java四大框架深度剖析:MyBatis、Spring、SpringMVC与SpringBoot
java·spring boot·spring·mybatis
风月歌2 天前
基于springboot校园健康系统的设计与实现(源码+文档)
java·spring boot·后端·mysql·毕业设计·mybatis·源码
剑走偏锋o.O2 天前
MyBatis框架详解与核心配置解读
java·学习·mybatis
ONEPEICE-ing3 天前
快速入门Springboot+vue——MybatisPlus多表查询及分页查询
前端·vue.js·spring boot·mybatis
wolf犭良3 天前
14、《SpringBoot+MyBatis集成(2)——进阶配置XML与注解的灵活运用》
xml·spring boot·mybatis
seabirdssss3 天前
重构测试项目为spring+springMVC+Mybatis框架
java·spring·重构·mvc·mybatis