import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.time.ZonedDateTime;
@Component
public class MetaHandler implements MetaObjectHandler {
/**
* 新增数据执行
* @param metaObject
*/
@Override
public void insertFill(MetaObject metaObject) {
setValue("createTime", ZonedDateTime.now(), metaObject);
setValue("updateTime", ZonedDateTime.now(), metaObject);
}
/**
* 更新数据执行
* @param metaObject
*/
@Override
public void updateFill(MetaObject metaObject) {
setValue("updateTime", ZonedDateTime.now(), metaObject);
}
private void setValue(String fieldName, Object value, MetaObject metaObject) {
Object field = getFieldValByName(fieldName, metaObject);
if (field == null && value != null) {
setFieldValByName(fieldName, value, metaObject);
}
}
}
@Configuration
@EnableConfigurationProperties({MybatisPlusProperties.class})
public class DataSourcePrimaryConfig {
@Autowired
private MybatisPlusProperties properties;
/**
* 创建 SqlSessionFactory
*/
@Bean(name = "primarySqlSessionFactory")
@Primary
public SqlSessionFactory testSqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/xxx/**/*.xml"));
GlobalConfig globalConfig = this.properties.getGlobalConfig();
// 全局自动填充配置
globalConfig.setMetaObjectHandler(new MetaHandler());
bean.setGlobalConfig(globalConfig);
bean.setVfs(SpringBootVFS.class);
// 自定义类型处理器的路径
bean.setTypeHandlersPackage("com.xxx.xxx.common.persistence.typehandler");
return bean.getObject();
}
package com.xxx.xxx.common.persistence.typehandler;
import org.apache.ibatis.lang.UsesJava8;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import java.sql.*;
import java.time.ZoneId;
import java.time.ZonedDateTime;
/**
* mybatis 3.5以上自带的ZonedDateTimeTypeHandler 需要驱动的支持
* @author Shinka
* @date 2021/08/26
*/
@UsesJava8
public class ZonedDateTimeTypeHandler extends BaseTypeHandler<ZonedDateTime> {
private static ZonedDateTime getZonedDateTime(Timestamp timestamp) {
if (timestamp != null) {
return ZonedDateTime.ofInstant(timestamp.toInstant(), ZoneId.systemDefault());
}
return null;
}
@Override
public void setNonNullParameter(PreparedStatement ps, int i, ZonedDateTime parameter, JdbcType jdbcType)
throws SQLException {
ps.setTimestamp(i, Timestamp.from(parameter.toInstant()));
}
@Override
public ZonedDateTime getNullableResult(ResultSet rs, String columnName) throws SQLException {
Timestamp timestamp = rs.getTimestamp(columnName);
return getZonedDateTime(timestamp);
}
@Override
public ZonedDateTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
Timestamp timestamp = rs.getTimestamp(columnIndex);
return getZonedDateTime(timestamp);
}
@Override
public ZonedDateTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
Timestamp timestamp = cs.getTimestamp(columnIndex);
return getZonedDateTime(timestamp);
}
}
@ApiModelProperty(value = "创建时间")
@TableField(value = "create_time", fill = FieldFill.INSERT)
protected ZonedDateTime createTime;
@ApiModelProperty(value = "更新时间")
@TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
protected ZonedDateTime updateTime;
官方文档 - 自动填充功能
mybatis-plus实现对创建时间和更新时间的自动填充
MyBatis-Plus如何自动填充数据表的创建时间和更新时间
mybatis-plus如何自动填充字段创建时间和修改时间
Mybatis-plus实现主键自增和自动注入时间
MybatisPlus自动维护数据库时间戳
mybatis属性映射中的ZonedDateTime,用于插入到MS-SQL
mybatis中使用Java8的日期LocalDate、LocalDateTime
mybatis typeHandlers 类型处理器(xml配置四)
mybatis学习(一):XML配置-TypeHandler
MyBatis 3.5.5 参考文档 类型处理器(typeHandlers)
基于SpringBoot自定义实现Mybatis-TypeHandler
SpringBoot Mybatis EnumTypeHandler自定义统一处理器
spring boot 与mybatis整合,type-aliases-package、type-handlers-package等配置不起作用,导致类加载失败
更新时自定义的TypeHandler不生效
FastjsonTypeHandler不生效
springboot+mybatis 自定义类型处理器配置无效问题
Springboot整合Mybatis使用TypeHandler来转换数据库中的数据
MyBatis-Plus自动填充功能失效导致的原因及解决