报错信息如下:
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>