MybatisPlus全局自动填充配置 & 自定义类型处理器

自定义实现类MetaHandler

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();
    }

ZonedDateTime类型处理

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自动填充功能失效导致的原因及解决

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值