Closed DragonZru closed 2 months ago
已解决: 覆盖实现 ` @org.apache.ibatis.annotations.Mapper public interface ExampleMapper extends Mapper<Example, Long> {
@Lang(Caching.class)
//@SelectKey(statement = "SELECT SEQ.NEXTVAL FROM DUAL", keyProperty = "id", before = true, resultType = long.class)
@Options(useGeneratedKeys = false, keyProperty = "id")
@InsertProvider(type = EntityProvider.class, method = "insert")
int insert(Example entity);
} ` mapper5 默认实现 useGeneratedKeys = true ,导致genid 失效
BaseMapper是没有特殊配置的,Mapper是有特殊配置的。可以考虑继承 BaseMapper.
版本 2.2.1
model `import com.ylli.api.common.uid.SnowFlakeGenerator; import io.mybatis.provider.Entity; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.data.annotation.Version;
import java.sql.Timestamp; import java.util.Map;
@Entity.Table("t_example") @Data @NoArgsConstructor public class Example {
id
bigint UNSIGNED NOT NULL,username
varchar(255) NOT NULL,password
varchar(255) NOT NULL,version
bigint NOT NULL DEFAULT 0,status
tinyint(1) NOT NULL DEFAULT 1,create_time
datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,update_time
datetime NULL ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (id
) ); */ }`mapper
`import com.ylli.api.example.model.Example; import io.mybatis.mapper.Mapper;
@org.apache.ibatis.annotations.Mapper public interface ExampleMapper extends Mapper<Example, Long> { }`
SnowFlakeGenerator
`public class SnowFlakeGenerator implements IDGenerator, GenId {
}`
测试
exampleMapper.insert(new Example("name", "description"));
错误提示:
Error updating database. Cause: java.sql.SQLIntegrityConstraintViolationException: Column 'id' cannot be null
The error may exist in com/ylli/api/example/mapper/ExampleMapper.java (best guess)
The error may involve com.ylli.api.example.mapper.ExampleMapper.insert-Inline
The error occurred while setting parameters
SQL: INSERT INTO t_example(id,username,password,version,status,create_time,update_time) VALUES (?,?,?,?,?,?,?)
Cause: java.sql.SQLIntegrityConstraintViolationException: Column 'id' cannot be null
请问有什么问题,当我切换 mapper4中 @KeySql(genId = SnowFlakeGenerator.class) 是可以的。