项目环境
<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 配置失效,所以枚举类未被扫描。