MyBatis-Plus中MetaObjectHandler沒生效完美解決
Mybatisplus自動填充功能失效
通過SpringBoot框架集成 mybatis-plus首先導入需要的依賴
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.2</version>
</dependency>
在appication.yml添加相關配置
mybatis-plus configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl//打印sql語句
mapper-locations: com/example/mapper/xml/*.xml // 配置mapper的掃描,找到所有的mapper.xml映射文件
創(chuàng)建實體類對象
@Data
@AllArgsConstructor
@NoArgsConstructor
public class OrderMaster implements Serializable {
@TableId(type = IdType.ASSIGN_UUID)//自動生成
private String orderId;
private String Name;
private String Phone;
private String Address;
/**
* 創(chuàng)建時間
*/
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
/**
* 修改時間
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
}
按照官方文檔進行配置
要記得添加@Component注解
@Component
//自動填充配置
public class FillHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
log.info("開始填充時間");
this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
this.setFieldValByName("updateTime", LocalDateTime.now(),metaObject);
}
@Override
public void updateFill(MetaObject metaObject) {
this.setFieldValByName("updateTime", LocalDateTime.now(),metaObject);
}
}
正常來說到了這一步,一般情況下就好了常見的錯誤有這幾種
- 日期類不一致導致 創(chuàng)建日期、更新日期 為 null
- @Component 沒有被掃到,可以看下啟動類的位置,啟動類掃描的包是在其所在包以下的包
- 還有就是填充的字段屬性不一致,比如Date和LocalDateTime
- 檢查MetaObjectHandler實現類是否使用@Component
- 實體類字段使用注解 @TableField(fill = FieldFill.INSERT)
可惜我的問題不是以上幾種,于是我打了斷點,發(fā)現根本沒有執(zhí)行到 MetaObjectHandler的實現類=>FillHandler于是我輸出了所有的bean,發(fā)現MetaObjectHandler并沒有注入進去。
這里的原因在于mybatis有自己默認的配置文件,所以我們自定義的沒有生效,自定義Bean sqlSessionFactory 影響到了 globalConfig ,導致配置失效。
添加這樣一個配置類即可
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.example.handler.FillHandler;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
public class sqlSessionFactory {
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
//獲取mybatis-plus全局配置
GlobalConfig globalConfig = GlobalConfigUtils.defaults();
//mybatis-plus全局配置設置元數據對象處理器為自己實現的那個
globalConfig.setMetaObjectHandler(new FillHandler());
mybatisSqlSessionFactoryBean.setDataSource(dataSource);
//mybatisSqlSessionFactoryBean關聯設置全局配置
mybatisSqlSessionFactoryBean.setGlobalConfig(globalConfig);
return mybatisSqlSessionFactoryBean.getObject();
}
}
到這里就終于好了,這個問題困擾了我一整天,終于解決了!
到此這篇關于MyBatis-Plus中MetaObjectHandler沒生效完美解決的文章就介紹到這了,更多相關MyBatis-Plus MetaObjectHandler沒生效內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
在IDEA中配置Selenium和WebDriver的具體操作
在自動化測試領域Selenium是一款非常流行的開源工具,它支持多種瀏覽器,并提供了豐富的API供開發(fā)者使用,而WebDriver則是Selenium的一個重要組件,它負責驅動瀏覽器執(zhí)行測試腳本,這篇文章主要給大家介紹了在IDEA中配置Selenium和WebDriver的具體操作,需要的朋友可以參考下2024-10-10
SpringBoot框架aop切面的execution表達式解讀
這篇文章主要介紹了SpringBoot框架aop切面的execution表達式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
詳解Spring中singleton?bean如何同時服務多個請求
這篇文章主要介紹了詳解Spring中singleton?bean如何同時服務多個請求2023-02-02

