項目環境
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/>
</parent>
自動填充功能
項目引入mybatis-plus后,嘗試使用它的自動填充功能,按照官方文檔步驟實現,下面列出部分具體代碼
注解填充字段
@ApiModelProperty(value = "更新時間")
@TableField(fill = FieldFill.INSERT_UPDATE)
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime updatedTime;
@ApiModelProperty(value = "創建時間")
@TableField(fill = FieldFill.INSERT)
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createdTime;
自定義實現類TableMetaObjectHandler
@Component
public class TableMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
this.strictInsertFill(metaObject, "createdTime", LocalDateTime.class, LocalDateTime.now());
this.strictInsertFill(metaObject, "updatedTime", LocalDateTime.class, LocalDateTime.now());
}
@Override
public void updateFill(MetaObject metaObject) {
this.strictUpdateFill(metaObject, "updatedTime", LocalDateTime.class, LocalDateTime.now());
}
}
自動填充字段代碼未執行
由於MetaObjectHandler實現類中的代碼未執行,所以我debug尋找mybatis-plus源碼中執行 insertFill方法的類
com.baomidou.mybatisplus.core.MybatisParameterHandler
從圖中可以看出,MetaObjectHandler沒有注入到配置中
解決方案
-
檢查MetaObjectHandler實現類是否使用@Component
實體類字段使用注解 @TableField(fill = FieldFill.INSERT)
-
手動注入MetaObjectHandler
@Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean(); //獲取mybatis-plus全局配置 GlobalConfig globalConfig = GlobalConfigUtils.defaults(); //mybatis-plus全局配置設置元數據對象處理器為自己實現的那個 globalConfig.setMetaObjectHandler(new TableMetaObjectHandler()); mybatisSqlSessionFactoryBean.setDataSource(dataSource); //mybatisSqlSessionFactoryBean關聯設置全局配置 mybatisSqlSessionFactoryBean.setGlobalConfig(globalConfig); ... return mybatisSqlSessionFactoryBean.getObject(); }
-
刪除bean sqlSessionFactory, 這是最終解決我問題的方案
這是我原來的Mybatis配置類,主要配置了mapper路徑和配置文件路徑,后來我刪除了此類,將這兩項配置寫到了yml文件里
出現這個問題的具體原因我沒有再跟源碼了,猜測是自定義Bean sqlSessionFactory 影響到了 globalConfig ,導致配置失效。@Configuration @EnableTransactionManagement public class MybatisConfig { private static final String CONFIG_XML_PATH = "classpath:mybatis-config.xml"; private static final String MAPPER_XML_PATH = "classpath:mapping/**/*.xml"; @Bean(name = "sqlSessionFactory") public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception { MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean(); PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver(); sqlSessionFactory.setConfigLocation(pathMatchingResourcePatternResolver.getResource(CONFIG_XML_PATH)); sqlSessionFactory.setMapperLocations(pathMatchingResourcePatternResolver.getResources(MAPPER_XML_PATH)); sqlSessionFactory.setDataSource(dataSource); return sqlSessionFactory.getObject(); } @Bean public DataSourceTransactionManager transactionManager(DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } }
-
其他
以上第三種方案還解決另一個問題;使用MyBatis-Plus的 通用枚舉 特性,進行數據庫查詢時報錯:
No enum constant com.**.workbench.model.enums.SystemStatusEnum.1
這個問題的原因應該和上述自動填充失效一樣;由於 global-config.type-enums-package 配置失效,所以枚舉類未被掃描。