Mybatis returns an error resolution of type int or Integer

  • 2020-05-26 08:30:22
  • OfStack

The error will be reported as follows:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Mapper method 'com.bill.springMybatis.dao.UserDao.getUserIdByName attempted to return null from a method with a primitive return type (int).
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.Java:894)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

If the return type is set to either the encapsulation type Integer or the base type int, errors are possible

For example,


<select id="queryPaySum" resultType="java.lang.Integer" parameterType="map" > 
  select sum(p.CASH_FEE) from PAY_INFO p 
  where 1=1 
     and p.TRADE_RESULT_CODE = #{traderesultcode} 
</select>

The Oracle I used USES the nvl() function to solve the problem


<select id="queryPaySum" resultType="java.lang.Integer" parameterType="map" > 
  select nvl(sum(p.CASH_FEE), 0) from PAY_INFO p 
  where 1=1 
     and p.TRADE_RESULT_CODE = #{traderesultcode} 
</select> 

Supplement:

Here's a look: replace null values with ISNULL(), NVL(), IFNULL(), and COALESCE()

In database operations, it is often necessary to replace the null value of 1 query, such as the function SUM(). If there is no value, this function will return NULL, which is what we do not want to see.

In MySQL we can write:


select IFNULL(sum(data),0) ...

In SQLSERVER we can write:


select ISNULL(sum(data),0) ...

In Oracle we can write:


select NVL(sum(data),0) ...

The method that applies to all databases can be written like this:


select COALESCE(sum(data),0) ...

COALESCE () usage:

COALESCE(value,...)

Returns the first value that is not null, or null if the parameter list is all null


sSELECT COALESCE(NULL,1);
    -> 1
SELECT COALESCE(NULL,NULL,NULL);
    -> NULL


Related articles: