MyBatis query return type int, an empty result set NULL, reported abnormal solve

E.g:

  <select id="getPersonRecordId" parameterType="java.lang.String" resultType="int">
    select role_id
    from p_person_role
    where stffe_id = #{stffeId,jdbcType=VARCHAR}
  </select>

When there is no record will be reported the following error

Servlet.service() for servlet [springDispatcherServlet] in context with path [/xxxx] threw exception [Request processing failed; 
nested exception is org.apache.ibatis.binding.BindingException: Mapper method 'com.xxx.PersonRoleRelationMapper.getPersonRecordno attempted to return null from a method with a primitive return type (int).] 
with root cause org.apache.ibatis.binding.BindingException: Mapper method 'com.xxx.PPersonRoleRelationMapper.getPersonRecordno attempted to return null from a method with a primitive return type (int).

If you encounter this problem, you can use the MySQL IFNULL function and the MAX function will return NULL values ​​are converted to 0. For example, the above-mentioned SQL statements can be changed to:

select IFFULL(MAX(role_id),0) AS role_id  from  p_person_role where  stffe_id = #{stffeId,jdbcType=VARCHAR}

 


 

In SQLSERVER we can write:
the SELECT ISNULL (max (the Data), 0) ...


 

In Oracle, we can write:
the SELECT NVL (max (the Data), 0) ...


 

The method can be written to the database applicable to all:
the SELECT COALESCE (max (the Data), 0) ...


 

Of course, these may be replaced with a max sum function.

Published 244 original articles · won praise 16 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_18671415/article/details/105303819