由于本人一直用的tkmybatis,在学习shardingSphere的过程中。发现如果用主键作为分片策略,就算指定了用雪花算法,实际上是无效的。
原因是ShardingSphere只有当你的传递的字段没有值的时候才会去使用雪花算法生成id,所以实际使用会出错
实体类
@Data
public class TStudent implements Serializable {
@Id
// @Column(insertable = false)
private Long id;
private String name;
private Integer age;
private String address;
private static final long serialVersionUID = 1L;
}
调用insert方法
@Test
public void addStudents(){
for(int i = 0 ; i < 10 ; i ++){
TStudent c = new TStudent();
c.setAddress("安徽省合肥市");
c.setAge(new Random().nextInt(100));
c.setName("学生"+i);
tStudentDao.insert(c);
}
selectStudents();
}
执行结果
org.mybatis.spring.MyBatisSystemException: nested exception is o