当配置mybatis返回int类型时

select id="getUserIdByName" parameterType="string" resultType="int">
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 id="getUserIdByName" parameterType="string" resultType="Integer">
SELECT
id
FROM user
WHERE userName = #{userName}
</select> </mapper>

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

工程源码:

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

使用MyBatis查询int类型字段,返回NULL值时报异常的解决方法的更多相关文章

  1. 使用MyBatis查询 返回类型为int,但是当查询结果为空NULL,报异常的解决方法

    使用MyBatis查询 返回类型为int,但是当查询结果为空NULL,会报异常. 例如: <select id="getPersonRecordId" parameterTy ...

  2. python 将mysql数据库中的int类型修改为NULL 报1366错误,解决办法

    gt.run_sql()是用pymysql 封装的类 distribution_sort_id type: int目的:将此字段值全部修改为NULL g=2gt.run_sql("updat ...

  3. MyBatis 传一个类型为String的参数时常见问题及解决方法

    MyBatis要求如果参数为String的话,不管接口方法的形参是什么,在Mapper.xml中引用时需要改变为_parameter才能识别 : <select id="selectB ...

  4. 使用like查询text类型字段

    使用like查询text类型字段 public bool Exists(GetReadType GRT, ClientMessageGetRead TypeID, string MessageID, ...

  5. mysql int类型字段插入空字符串时自动转为0

    mysql int类型字段插入空字符串时自动转为0 如果不想转的话可以修改配置文件 修改 my.ini 文件. # Set the SQL mode to strictsql-mode=”STRICT ...

  6. MySqlClient访问tinyint字段返回布尔值

    原文 MySqlClient访问tinyint字段返回布尔值 症状: 使用MySqlClient访问tinyint       unsign 字段返回布尔值 true 和        false,但 ...

  7. 面试题:对NotNull字段插入Null值 有啥现象?

    Hi,大家好!我是白日梦. 今天我要跟你分享的话题是:"对NotNull字段插入Null值有啥现象?" 一. 推荐阅读 首发地址:https://mp.weixin.qq.com/ ...

  8. Django 分页查询并返回jsons数据,中文乱码解决方法

    Django 分页查询并返回jsons数据,中文乱码解决方法 一.引子 Django 分页查询并返回 json ,需要将返回的 queryset 序列化, demo 如下: # coding=UTF- ...

  9. 报错NameError: name ‘null’ is not defined的解决方法

    报错NameError: name 'null' is not defined的解决方法 eval()介绍 eval()函数十分强大,官方demo解释为:将字符串str当成有效的表达式来求值并返回计算 ...

随机推荐

  1. phpMyAdmin在Mac OS X上的配置和使用

    本文主要记录phpMyAdmin在Mac OS X上的配置和使用,避免朋友们走弯路,浪费不必要的时间.   1. 下载:    2. 在"设置"中打开" web shar ...

  2. SQL Server如何启用xp_cmdshell组件

    [错误描述]: SQL Server阻止了对组件‘xp_cmdshell’的过程‘sys.xp_cmdshell’的访问.因为此组件已作为此服务嚣安全配置的一部分而被关闭.系统管理员可以通过使用sp_ ...

  3. Difference between Satisfiable, Valid, Unsatisfiable & Invalid

    A formula is satisfiable if it is possible to find an interpretation (model) that makes the formula  ...

  4. Eclipse shortcuts

    Editor Shortcut Description Alt + / Content assist. A great help for our coding. Ctrl + Shift + F Fo ...

  5. BpBinder 转换为 BpCameraService 流程

    interface_cast<ICameraService>(binder) : 其中binder 为IBinder类型,实际为BpBinder interface_cast 定义在IIn ...

  6. 【AI】蒙特卡洛搜索树

    http://jeffbradberry.com/posts/2015/09/intro-to-monte-carlo-tree-search/ 蒙特卡洛方法与随机优化: http://iacs-co ...

  7. [转]silverlight Datagrid 行上增加ToolTip

    有两种办法: 1. 直接在后台处理在数据绑定后 ,注册LoadingRow 事件this.DataGrid.LoadingRow += new EventHandler<DataGridRowE ...

  8. 让VS2010/VS2012添加新类时自动添加public关键字

    在VS添加类别的时候,每次都需要添加public关键字,表示好麻烦. 但是可以避免这个麻烦的. 通过修改VS2010的ItemTemplate,可以避免这个麻烦. 修改方法如下: 1. 打开文件夹Mi ...

  9. 牢骚与javascript中的this

    最近在看关于拖延症的一本书<拖拉一点也无妨>,后面得出结论是自己写博客大部分处于两种状态,心情很好和心情很不好的时候.因为正常状态下感觉写博客吧,是件很麻烦的事情,不如去看看电影看看漫画啥 ...

  10. java之Object类介绍

    1.Object类是所有java类的基类 如果在类的声明中未使用extends关键字指明其基类,则默认基类为Object类,ex: public class Person{ ~~~~~ } 等价于 p ...