Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException

时间:2022-07-28
本文章向大家介绍Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1.错误截图

自己在idea中写一个测试类,也不能删除pid为1的那条数据,报错如下

package test;

import entity.Product;
import mapper.ProductMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * zt
 * 2020/10/12
 * 21:38
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:application.xml")
public class TestMapper {
    @Autowired
    private ProductMapper mapper;
    @Test
    public void test1(){
        Product p = new Product();
        p.setTid(1);
        p.setPname("aaa");
        mapper.add(p);
    }

    @Test
    public void test2(){

        mapper.delete(2);
    }

}

2.错误原因

所以删除最后那条自己刚刚添加的数据,因为这条数据没有和购物车表中数据有外键约束,所以删除并不会报错