MyBatis consulta tipo de retorno int, un conjunto de resultados vacío NULL, informó resolver anormal

Por ejemplo:

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

Cuando no hay ningún registro será el siguiente 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).

Si se produce este problema, puede utilizar la función de MySQL IFNULL y la función MAX, volverá valores NULL se convierten a 0. Por ejemplo, las sentencias SQL antes mencionados se pueden cambiar a:

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

 


 

En SQLSERVER podemos escribir:
SELECT ISNULL (max (los datos), 0) ...


 

En Oracle, podemos escribir:
SELECT NVL (max (los datos), 0) ...


 

El método se puede escribir en la base de datos aplicable a todos:
la COALESCE SELECT (max (los datos), 0) ...


 

Por supuesto, estos pueden ser reemplazados con una función de suma máx.

Publicados 244 artículos originales · elogios ganado 16 · Vistas a 10000 +

Supongo que te gusta

Origin blog.csdn.net/qq_18671415/article/details/105303819
Recomendado
Clasificación