In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to report an error when there is no record in MyBatis query". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "MyBatis query no record when the return value error report how to do"!
The unrecorded return value of MyBatis query is under MyBatis 3.4.1.
If the return value of Dao is an entity, the select query returns null if there is no record. It is easy to report null pointer exception!
Notice findById ()
If the return value of Dao is List, then the select query returns [] if there is no record, that is, an empty array
Not null. So at this point, you need to use CollectionUtils.isNotEmpty () instead of "= = null".
List findById (); return value error problem when query has no result mybatis query without result Times error
Problem with (method name) queryAllNumFromCart attempted to return null from a method with a primitive return type (long)
QueryAllNumFromCart this method is defined in mapper.xml as follows:
Select sum (num) from t_cart where user_id = # {userId}
It is defined in mapper as follows:
Long queryAllNumFromCart (Integer userId); / / the returned value is of type long
An error is reported after the call:
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)
.
The statements that report the main reasons for the error are:
(method) attempted to return null from a method with a primitive return type (long)
This method attempts to return null from a method that defines the original return type (long), and it is obvious that there is a problem with the return type. After searching on the Internet, it is found that the return value of mybais is the wrapper class, and the return type defined by the sql statement is: java.lang.Long. When the query database is empty, null is returned instead of 0L, so here you need to change the return type of the method to the wrapper class java.lang.Long:
/ / Long queryAllNumFromCart (Integer userId); / / when the queried data is 0, null is returned instead of 0, which can be changed to a wrapper class
Or add the function of IFNULL (expr1,expr2) to the sql statement to determine:
Select IFNULL (sum (num), 0) from t_cart where user_id = 1
It is recommended that you modify the sql statement.
At this point, I believe that everyone on the "MyBatis query no record when the return value error how to do" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.