網站首頁 編程語言 正文
Mybatis中報錯:attempted to return null from a method with a primitive return type (int)
作者:此方星河 更新時間: 2022-05-17 編程語言發生緣由
- 學習Mybatis的使用
環境
- jdk版本:jdk-16.0.2
- Idea版本:2021.2
- Mybatis版本:mybatis-3.5.9
- 電腦系統:win10
問題及補救
問題描述
其他文件就不在這里贅述了,有User類,自己抽取的SqlSessionUtils類,以及配置文件等等。UserMapper.java
:
/**
* 插入一條用戶信息
*/
int insertUser(User user);
UserMapper.xml
:
<select id="insertUser" resultType="user">
insert into t_user values(null, #{username}, #{password}, #{age}, #{sex}, #{email});
select>
創建測試類運行:
@Test
public void testInsertUser() {
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int result = mapper.insertUser(new User(null, "張三", "243", 34, "男", "234@qq"));
System.out.println(result);
}
結果報錯:
org.apache.ibatis.binding.BindingException: Mapper method 'com.linxuan.mybatis.mapper.UserMapper.insertUser attempted to return null from a method with a primitive return type (int).
解決方案
- 網上搜索說可以將返回值給換一下,也就是方法的返回值換成
Integer
,這樣返回NULL不會報錯。int insertUser(User user);
換成:Integer insertUser(User user);
。同時mapper.insertUser(new User(null, "張三", "243", 34, "男", "234@qq"));
的返回值也變成了Integer
。
執行測試類之后確實沒有報錯,數據庫里面也確實是添加了用戶信息,可是返回值變成了NULL,這不合理啊! - 突然我想到了,咱們這個SQL語句是DML,插入語句的啊,我為什么要用
select
標簽呢?
修改為insert
標簽之后就不會報錯了。
原文鏈接:https://blog.csdn.net/m0_51426055/article/details/124502734
相關推薦
- 2022-04-08 深入理解Golang的反射reflect示例_Golang
- 2022-08-04 Python+NumPy繪制常見曲線的方法詳解_python
- 2022-06-07 Python?Numpy庫的超詳細教程_python
- 2022-08-16 Python中集合的創建及常用函數的使用詳解_python
- 2023-01-26 redis性能優化之生產中實際遇到的問題及排查總結_Redis
- 2022-09-29 React實現下拉框的key,value的值同時傳送_React
- 2023-04-27 antd?upload上傳如何獲取文件寬高_React
- 2022-01-15 跨域系列之proxy代理,解決跨域的方法之一
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支