- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
最近在使用Mybatis3,发现当你的SQL语句从数据库中获取一个空的结果集时,Mybatis会新建一个List
并返回给你的程序。
给定一些代码,例如:
List<User> resultList = (List<User>)sqlSession.select("statementId");
<select id="statementId" resultType="User">
select * from user where id > 100
</select>
假设上述 SQL 没有返回任何行(即没有大于 100 的 id)。
变量 resultList
将是一个空的 List
,但我希望它是 null
。我该怎么做?
最佳答案
作为查询的结果,最好有一个空集合而不是 null。在处理集合时,您通常会循环遍历每个项目并对它做一些事情,就像这样:
List<User> resultList = (List<User>) sqlSession.select("statementId");
for (User u : resultList) {
//...
}
如果列表为空,它什么都不做。
但是,如果您返回 null,则必须保护您的代码免受 NullPointerException 的影响,并改为编写如下代码:
List<User> resultList = (List<User>) sqlSession.select("statementId");
if (resultList != null) {
for (User u : resultList) {
//...
}
}
第一种方法通常更好,MyBatis 就是这样做的,但是你可以强制它返回 null,如果那真的是你想要的。
为此你可以写一个 MyBatis plugin并拦截对任何查询的调用,如果查询结果为空则返回 null。
这里有一些代码:
在你的配置中添加:
<plugins>
<plugin interceptor="pack.test.MyInterceptor" />
</plugins>
拦截器代码:
package pack.test;
import java.util.List;
import java.util.Properties;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
@Intercepts({ @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}) })
public class MyInterceptor implements Interceptor {
public Object intercept(Invocation invocation) throws Throwable {
Object result = invocation.proceed();
List<?> list = (List<?>) result;
return (list.size() == 0 ? null : result);
}
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
public void setProperties(Properties properties) {
}
}
如果拦截对 ResultSetHandler
而不是 Executor
的调用,则可以进一步限制拦截器的范围。
关于java - MyBatis 是如何处理空结果集的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12156562/
1.概述 转载:MyBatis 二级缓存全详解 上一篇文章中我们介绍到了 MyBatis 一级缓存其实就是 SqlSession 级别的缓存,什么是 SqlSession 级别的缓存呢?一级缓存的本质
1.概述 转载:核心配置综述之StatementHandler 2.MyBatis 四大组件之StatementHandler StatementHandler 是四大组件中最重要的一个对象,负责操作
1.概述 转载:MyBatis 启动流程 MyBatis 是第一个支持自定义 SQL、存储过程和高级映射的类持久框架。MyBatis 消除了大部分 JDBC 的样板代码、手动设置参数以及检索结果。My
1.概述 转载:MyBatis 基础搭建及架构概述 2.MyBatis 是什么? MyBatis是第一个支持自定义SQL、存储过程和高级映射的类持久框架。MyBatis消除了大部分JDBC的样板代码、
1.概述 转载:核心配置综述之 ParameterHandler MyBatis 四大核心组件我们已经了解到了两种,一个是 Executor ,它是MyBatis 解析SQL请求首先会经过的第一道关卡
1.概述 转载:核心配置综述之 ResultSetHandler 我们之前介绍过了MyBatis 四大核心配置之 Executor、StatementHandler、 ParameterHandler
如果我使用mybatis,我可以很容易地得到更新的行数,就像 update table set desc = 'xxx' where name = ? 但是,如果我想获取更新的行数,而不是计数,我该如
如何在MyBatis 3中使用小于等于 SELECT * FROM( SELECT * FROM TABLE1 WHERE COL1 =#{COL1,jdbc
我将 mybatis3.0.6 与 java 一起使用 哪个性能更好? [select id="getData" parameterType="Integer" resultType="Integer
我无法在 mybatis 中使用动态排序类型创建 SQL,如下例 select user_profile.user_profile_id, user_profile.first_name
这是一个流行的例子。 insert into ACCOUNT ( ACC_ID, ACC_FIRST_NAME, ACC_LAST_NAME, ACC_EMAIL )values (
我下载了MyBatis,文件夹中有一个mybatis-3.0.4-javadoc.jar,我解压并打开它,但它几乎是空的。 哪里可以找到MyBatis的API文档? 最佳答案 http://repo1
我正在尝试为 ArrayList 编写类型处理程序,但这给了我错误,任何人都可以帮助我。 我想将 ArrayList 作为 VARCHAR 存储在数据库中并将其检索为 ArrayList。 packa
目录 依赖 配置 CodeGenerator mybatis-plus-generator + clickhouse 自动生成代码 依赖
目录 三者实现对比 使用fluent mybatis 来实现上面的功能 换成mybatis原生实现效果 换成mybatis plus
例如,我有查询从员工中选择 ID、姓名、年龄、地址,而不是拥有员工对象列表。我希望有一个 map 列表,如 list{ map{ ("id", 123), ("name","jac
我在使 MyBatis (3.4.6) 工作时遇到一些麻烦。 我已将 mybatis-config.xml 文件放置在项目的 src/main/resources 文件夹中,但是当我运行单元测试时,出
我现在使用 Mybatis 和 spring-boot。我没有添加mybatis-config.xml。我根据说明通过 application.properties 为数据源和 mybatis 进行所
这是我的第一篇文章,用我糟糕的英语...... 我使用的是MyBatis3.0 在查询中,我使用 SqlBuilder 的方法如下: public class DataStatisticSqlBuil
主题:MyBatis:Boolean Paraeter:MyBatis 正在使用 Getter 内容: 大家好, 我一直在寻找解决我近乎简单的 MyBatis 问题的方法: 给定代码(仅必要部分):
我是一名优秀的程序员,十分优秀!