第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

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

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

實體類:

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

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

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

@Version
private?Integer?version;


更新測試代碼:

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

}


自動填充優(yōu)化設(shè)置代碼:

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

}

測試結(jié)果,updateTime不會自動填充;

updateFill~ ~ ~ ~ ~

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

DEBUG==> Parameters: 100018(Integer), xw@baomidou.com(String), null, 3(Integer), 1547000994376744961(Long), 2(Integer)

DEBUG<==? ? Updates: 1

影響行數(shù):1

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



正在回答

1 回答

查閱官網(wǎng),發(fā)現(xiàn)要用了新的版本,加入了如下代碼:

@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 回復(fù) 有任何疑惑可以回復(fù)我~

舉報

0/150
提交
取消

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

我要回答 關(guān)注問題
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號