关于使用Mybatis进行数据库删除时delete返回结果一直为0但数据库删除成功问题

UserMapper.xml内容为

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace: 当前Mapper动态sql映射文件的唯一标识名称: -->
<mapper namespace="mappers.UserMapper">

    <select id="getUserById" resultType="com.hfxt.pojo.User" >
        select
        uid, username, user_pwd as userPwd
        from sys_user_tb
        where uid = 2;
    </select>
    <!--    扩展: 查询所有用户. -->
    <select id="getUserAll" resultType="com.hfxt.pojo.User">
        select uid, username, user_pwd as userPwd
        from sys_user_tb ;
    </select>

    <insert id="addUser">
        insert into sys_user_tb (username,user_pwd)
        values ('HelloKitty','111111');
    </insert>

    <update id="updateUserById">
        update sys_user_tb set user_pwd = '888888'
        where uid = 21 ;
    </update>

    <delete id="deleteUserById">
        delete from sys_user_tb where uid = 22;
    </delete>
</mapper>

数据库内容为

执行语句为

@Test
    public void test2(){
        String resource = "SqlMapConfig.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        int addUser = sqlSession.insert("mappers.UserMapper.addUser");
        int updateUserById = sqlSession.update("mappers.UserMapper.updateUserById");
        int deleteUserById = sqlSession.delete("mappers.UserMapper.deleteUserById");
        System.out.println(addUser+"-"+updateUserById+"-"+deleteUserById);
        sqlSession.commit();
        sqlSession.close();
    }

执行结果为

由数据库变化可知数据删除成功,但delete返回值为0

解决方案

 勾选属性中的跳过测试即可

原因是Maven项目测试类的@Test注解会执行两次,所以delete的值总为零,在文件->项目结构->Maven->Runner中勾选skip test就可以解决

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值