广告
返回顶部
首页 > 资讯 > 后端开发 > Python >MyBatis查询无记录时的返回值问题
  • 332
分享到

MyBatis查询无记录时的返回值问题

2024-04-02 19:04:59 332人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录mybatis查询无记录的返回值在MyBatis 3.4.1下查询无结果时的返回值报错问题mybatis的查询无结果时报错MyBatis查询无记录的返回值 在MyBatis 3.

MyBatis查询无记录的返回值

在MyBatis 3.4.1下

如果Dao的返回值是实体,则select查询无记录时返回null。容易报空指针异常!

Notice findById();

如果Dao的返回值是List,则select查询无记录是返回的是[],也就是空数组

而不是null。所以这时候判空需要用CollectionUtils.isNotEmpty(),而不是"==null"

List<Notice> findById();

查询无结果时的返回值报错问题

mybatis的查询无结果时报错

(方法名)queryAllNumFromCart attempted to return null from a method with a primitive return type (long)的问题

queryAllNumFromCart此方法在mapper.xml中是这样定义的:

 <select id="queryAllNumFromCart" parameterType="java.lang.Integer" resultType="java.lang.Long">
      select sum(num) from t_cart where user_id = #{userId}
  </select>

在mapper中是这样定义的:

long queryAllNumFromCart(Integer userId);  //返回值为long类型

调用后运行报错:

java.lang.RuntimeException: org.apache.ibatis.binding.BindingException: Mapper method 'com.eGoo.mapper.CartMapper.queryAllNumFromCart attempted to return null from a method with a primitive return type (long).
org.apache.ibatis.binding.BindingException: Mapper method 'com.egoo.mapper.CartMapper.queryAllNumFromCart attempted to return null from a method with a primitive return type (long).
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:94)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
    at com.sun.proxy.$Proxy16.queryAllNumFromCart(Unknown Source)
    at com.egoo.service.impl.CartServiceImpl.getTotalFromMysql(CartServiceImpl.java:370)
    at com.alibaba.dubbo.common.bytecode.Wrapper2.invokeMethod(Wrapper2.java)
    at com.alibaba.dubbo.rpc.proxy.javassist.JavassistProxyFactory$1.doInvoke(JavassistProxyFactory.java:47)
    at com.alibaba.dubbo.rpc.proxy.AbstractProxyInvoker.invoke(AbstractProxyInvoker.java:76)
    at com.alibaba.dubbo.config.invoker.DelegateProviderMetaDatainvoker.invoke(DelegateProviderMetaDataInvoker.java:52)
    at com.alibaba.dubbo.rpc.protocol.InvokerWrapper.invoke(InvokerWrapper.java:56)
    at com.alibaba.dubbo.rpc.filter.ExceptionFilter.invoke(ExceptionFilter.java:62)
    ......

主要报错原因的语句为:

(method)attempted to return null from a method with a primitive return type (long)

此方法企图从定义了原始的返回类型(long)的方法中返回null ,显而易见,此处的返回值类型有问题。网上搜了之后发现是mybais的返回值为包装类,且sql语句的定义的返回值类型为:java.lang.Long。查询数据库为空后,会返回null而不是0L,所以此处需要将方法的返回值类型改为包装类java.lang.Long:

//Long queryAllNumFromCart(Integer userId);  //当查询到的数据为0条时,会返回null,而不是0,可以改为包装类

或者在sql语句中加入IFNULL(expr1,expr2)的函数进行判断:

select IFNULL(sum(num),0) from t_cart where user_id = 1

建议还是使用修改sql语句的方式。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: MyBatis查询无记录时的返回值问题

本文链接: https://www.lsjlt.com/news/163283.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作