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添加相關(guān)配置
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實現(xiàn)類是否使用@Component
- 實體類字段使用注解 @TableField(fill = FieldFill.INSERT)
可惜我的問題不是以上幾種,于是我打了斷點,發(fā)現(xiàn)根本沒有執(zhí)行到 MetaObjectHandler的實現(xiàn)類=>FillHandler于是我輸出了所有的bean,發(fā)現(xiàn)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全局配置設置元數(shù)據(jù)對象處理器為自己實現(xiàn)的那個 globalConfig.setMetaObjectHandler(new FillHandler()); mybatisSqlSessionFactoryBean.setDataSource(dataSource); //mybatisSqlSessionFactoryBean關(guān)聯(lián)設置全局配置 mybatisSqlSessionFactoryBean.setGlobalConfig(globalConfig); return mybatisSqlSessionFactoryBean.getObject(); } }
到這里就終于好了,這個問題困擾了我一整天,終于解決了!
到此這篇關(guān)于MyBatis-Plus中MetaObjectHandler沒生效完美解決的文章就介紹到這了,更多相關(guān)MyBatis-Plus MetaObjectHandler沒生效內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在IDEA中配置Selenium和WebDriver的具體操作
在自動化測試領(lǐng)域Selenium是一款非常流行的開源工具,它支持多種瀏覽器,并提供了豐富的API供開發(fā)者使用,而WebDriver則是Selenium的一個重要組件,它負責驅(qū)動瀏覽器執(zhí)行測試腳本,這篇文章主要給大家介紹了在IDEA中配置Selenium和WebDriver的具體操作,需要的朋友可以參考下2024-10-10MyBatis中調(diào)用存儲過程和函數(shù)的實現(xiàn)示例
在MyBatis中調(diào)用存儲過程和函數(shù)是一個相對高級的特性,本文主要介紹了MyBatis中調(diào)用存儲過程和函數(shù)的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-07-07SpringBoot框架aop切面的execution表達式解讀
這篇文章主要介紹了SpringBoot框架aop切面的execution表達式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05詳解Spring中singleton?bean如何同時服務多個請求
這篇文章主要介紹了詳解Spring中singleton?bean如何同時服務多個請求2023-02-02