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]
相关推荐
bing_1583 小时前
跨多个微服务使用 Redis 共享数据时,如何管理数据一致性?
redis·微服务·mybatis
云之兕6 小时前
MyBatis 的动态 SQL
数据库·sql·mybatis
loser.loser13 小时前
QQ邮箱发送验证码(Springboot)
java·spring boot·mybatis
毅航16 小时前
Trae复刻Mybatis之旅(一):创建SqlSession会话,构建代理
后端·mybatis·trae
潮流coder17 小时前
mybatis的if判断==‘1‘不生效,改成‘1‘.toString()才生效的原因
java·tomcat·mybatis
BillKu19 小时前
Java + Spring Boot + Mybatis 实现批量插入
java·spring boot·mybatis
dog shit20 小时前
web第十次课后作业--Mybatis的增删改查
android·前端·mybatis
emo了小猫20 小时前
Mybatis #{} 和 ${}区别,使用场景,LIKE模糊查询避免SQL注入
数据库·sql·mysql·mybatis
yuren_xia1 天前
Spring Boot + MyBatis 集成支付宝支付流程
spring boot·tomcat·mybatis
神仙别闹1 天前
基于Java(SpringBoot、Mybatis、SpringMvc)+MySQL实现(Web)小二结账系统
java·spring boot·mybatis