当配置mybatis返回int类型时

 

select >
    SELECT
    	  id
    FROM user
    WHERE userName = #{userName}
  </select>

 

会报错如下:

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)

 

解决方案,返回类型设置为封装类型Integer而不是基本类型int

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.bill.springMybatis.dao.UserDao">

  <cache />
  <select >
    SELECT
    	  id
    FROM user
    WHERE userName = #{userName}
  </select>
  
</mapper>

service层如果需要int数据类型,可以自动从Integer进行转换, 当然有可能加入一些判断,比如Integer为Null,赋给int可以先转成0

 

工程源码:

http://download.csdn.net/detail/sundongsdu/5851343

相关文章:

  • 2022-12-23
  • 1970-01-01
  • 2022-12-23
  • 2022-01-30
  • 2021-09-27
  • 2022-12-23
  • 2021-12-12
猜你喜欢
  • 2021-07-12
  • 2022-12-23
  • 2022-12-23
  • 2021-09-10
  • 2021-07-30
相关资源
相似解决方案