问答详情
源自:2-1 通用传统模式简介及通用mapper新增方法

insert时没有生成id

    @Test
    public void insert(){
        User user = new User();
        user.setName("tom");
        user.setManagerId(1088248166370832385L);
        user.setCreateTime(new Date());
        int insert = userMapper.insert(user);
        System.out.println("insert = " + insert);
    }

org.springframework.dao.DataIntegrityViolationException: 

### Error updating database.  Cause: java.sql.SQLException: Field 'id' doesn't have a default value

### The error may exist in com/exam/demo/mapper/UserMapper.java (best guess)

### The error may involve com.exam.demo.mapper.UserMapper.insert-Inline

### The error occurred while setting parameters

### SQL: INSERT INTO user  ( name,   manager_id, create_time )  VALUES  ( ?,   ?, ? )

### Cause: java.sql.SQLException: Field 'id' doesn't have a default value

; Field 'id' doesn't have a default value; nested exception is java.sql.SQLException: Field 'id' doesn't have a default value


提问者:慕姐5116453 2021-01-05 10:36

个回答

  • 老猿
    2021-01-05 16:25:30

    同学,你主键策略设置的是什么?包括mp和数据库的,我怀疑你mp设置的为自增,但数据库没设置。