日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達者為師

網(wǎng)站首頁 編程語言 正文

Mybatis中報錯:attempted to return null from a method with a primitive return type (int)

作者:此方星河 更新時間: 2022-05-17 編程語言

發(fā)生緣由

  • 學(xué)習(xí)Mybatis的使用

環(huán)境

  • jdk版本:jdk-16.0.2
  • Idea版本:2021.2
  • Mybatis版本:mybatis-3.5.9
  • 電腦系統(tǒng):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>

創(chuàng)建測試類運行:

    @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);
    }

結(jié)果報錯:

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).

解決方案

  • 網(wǎng)上搜索說可以將返回值給換一下,也就是方法的返回值換成Integer,這樣返回NULL不會報錯。
    int insertUser(User user);換成:Integer insertUser(User user);。同時mapper.insertUser(new User(null, "張三", "243", 34, "男", "234@qq"));的返回值也變成了Integer。
    執(zhí)行測試類之后確實沒有報錯,數(shù)據(jù)庫里面也確實是添加了用戶信息,可是返回值變成了NULL,這不合理??!
  • 突然我想到了,咱們這個SQL語句是DML,插入語句的啊,我為什么要用select標(biāo)簽?zāi)兀?br> 修改為insert標(biāo)簽之后就不會報錯了。

原文鏈接:https://blog.csdn.net/m0_51426055/article/details/124502734

欄目分類
最近更新