亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

添加了樂觀鎖,updateTime不會自動填充。

添加了樂觀鎖,updateTime不會自動填充是怎么回事?

實體類:

//創建時間
????//設置自動填充
@TableField(fill?=?FieldFill.INSERT)
private?LocalDateTime?createTime;

//修改時間;
@TableField(fill?=?FieldFill.UPDATE)
private?LocalDateTime?updateTime;

//版本,樂觀鎖插件時會用到;默認值1,插入時不設置此字段;

@Version
private?Integer?version;


更新測試代碼:

@Test
public?void?UpdateById()?{
????int?version=2;//假設取出了小舞的版本為1;
????User?user?=?new?User();
????user.setEmail("[email protected]");
????user.setAge(100018);
????user.setVersion(version);//添加了樂觀鎖,預期version會變為2;
????user.setId(1547000994376744961L);//小舞
????int?rows?=?userMapper.updateById(user);
????System.out.println("影響行數:"?+?rows);

}


自動填充優化設置代碼:

@Override
public?void?updateFill(MetaObject?metaObject)?{
????boolean?fieldValByName?=?metaObject.hasSetter("updateTime");
????if?(fieldValByName){
????????System.out.println("updateFill~?~?~?~?~");
????????this.setFieldValByName("updateTime",LocalDateTime.now(),metaObject);
????}

}

測試結果,updateTime不會自動填充;

updateFill~ ~ ~ ~ ~

DEBUG==>? Preparing: UPDATE user SET age=?, email=?, update_time=?, version=? WHERE id=? AND version=? AND deleted=0?

DEBUG==> Parameters: 100018(Integer), [email protected](String), null, 3(Integer), 1547000994376744961(Long), 2(Integer)

DEBUG<==? ? Updates: 1

影響行數:1

我去測試過沒有setVersion()的代碼,會自動填充updateTime,難道樂觀鎖和自動填充會沖突?但是顯然不可能,老師的視頻中已經演示自動填充成功了。到底是為什么?求解!



正在回答

1 回答

查閱官網,發現要用了新的版本,加入了如下代碼:

@Bean
public?MybatisPlusInterceptor?mybatisPlusInterceptor()?{
????MybatisPlusInterceptor?mybatisPlusInterceptor?=?new?MybatisPlusInterceptor();
????mybatisPlusInterceptor.addInnerInterceptor(new?OptimisticLockerInnerInterceptor());
????return?mybatisPlusInterceptor;
}

http://img1.sycdn.imooc.com//62cee56d000119a018340955.jpg

添加了依賴:

<!--????????MybatisPlusInterceptor樂觀鎖-->
????????<dependency>
????????????<groupId>com.baomidou</groupId>
????????????<artifactId>mybatis-plus-extension</artifactId>
????????????<version>3.5.2</version>
????????</dependency>
????</dependencies>


問題完美解決!!

2 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

添加了樂觀鎖,updateTime不會自動填充。

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號