Mybatis更新数据库数据操作报错org.apache.ibatis.exceptions.PersistenceException:

报错信息如下:

org.apache.ibatis.exceptions.PersistenceException: 
### Error updating database.  Cause: java.sql.SQLIntegrityConstraintViolationException: Column 'is_free_delivery' cannot be null
### The error may exist in mappers/goods.xml
### The error may involve goods.update-Inline
### The error occurred while setting parameters
### SQL: UPDATE t_goods         SET title=?,             sub_title=?,             original_cost=?,             current_price=?,             discount=?,             is_free_delivery=?,             category_id=?         WHERE goods_id = ?

 程序如下:

    @Test
    public void testUpdate() {
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtils.openSession();
            Goods goods = sqlSession.selectOne("goods.selectById", 739);
            goods.setTitle("更新商品名称");
            int num=sqlSession.update("goods.update",goods);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            if (sqlSession!=null){
                sqlSession.rollback();
            }
        } finally {
            MybatisUtils.closeSession(sqlSession);
        }
    }

解决方法:

删除goods.xml中不更新的项目:

改正前:

    </insert>
    <update id="update" parameterType="com.imooc.mybatis.utils.entity.Goods">
        UPDATE t_goods
        SET title=#{title},
            sub_title=#{subTitle},
            original_cost=#{originalCost},
            current_price=#{currentPRice},
            discount=#{discount},
            is_free_delivery=#{isFreeDeliver},
            category_id=#{categoryId}
        WHERE goods_id = #{goodSId}
    </update>

 改正后:

    </insert>
    <update id="update" parameterType="com.imooc.mybatis.utils.entity.Goods">
        UPDATE t_goods
        SET title=#{title}
        WHERE goods_id = #{goodSId}
    </update>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值