Cause: java.sql.SQLIntegrityConstraintViolationException: Column ‘xxx‘ cannot be null

  • Post author:
  • Post category:java


1、报错信息

2、定位错误的范围

### SQL: insert into business             (businessId,password,salt,businessName,businessAddress,businessExplain,starPrice,deliveryPrice)             values (?,?,?,?,?,?,?,?)

3、从上述报错内容已知错误存在于SQL的insert into操作里,然后跳转到BuseineeMapper.xml里

4、定位到BusinessMapper.xml里的id为add的部分

    <insert id="add" parameterType="Business" useGeneratedKeys="true">
        <!-- 使用数据库的自增列管理businessId值,并保存到Business类的businessId属性中 -->
        <selectKey keyColumn="businessId" keyProperty="businessId" resultType="int">
            select last_insert_id() as businessId
        </selectKey>
        insert into business
            (businessId,password,salt,businessName,businessAddress,businessExplain,starPrice,deliveryPrice)
            values (#{businessId},#{password},#{salt},#{businessName},#{businessAddress},#{businessExplain},#{starPrice},#{deliveryPrice})
    </insert>

5、发现sql语句和xml标签没写错,排除问题存在于BusinessMapper.xml

6、最后发现businessId是必填字段而且是主键,但没有给businessId勾选自动递增,所以插入数据时因为businessId没值且不自增而导致异常

7、勾选上自动递增并保存,运行成功



版权声明:本文为m0_64284147原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。