迁移达梦数据库适配LocalDateTime

2 篇文章 0 订阅
1 篇文章 0 订阅

达梦数据库兼容LocalDateTime

问题

如果使用的是mybatis作为orm框架,那么当从mysql数据库切换到达梦数据库时,使用了LocalDateTime会报错。

Error attempting to get column 'create_time' from result set. Cause: dm.jdbc.driver.DMException: 不支持的接口或功能

根本原因是mybatis在转换LocalDateTime的时候发生了异常

解决办法


/**
 * 日期时间处理,适配达梦数据库
 */
public class LocalDateTimeTypeHandler extends BaseTypeHandler<LocalDateTime> {
    public LocalDateTimeTypeHandler() {
    }

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, LocalDateTime parameter, JdbcType jdbcType) throws SQLException {
        if (parameter == null) {
            ps.setTimestamp(i, null);
            return;
        }
        ps.setTimestamp(i, Timestamp.valueOf(parameter));
    }

    @Override
    public LocalDateTime getNullableResult(ResultSet rs, String columnName) throws SQLException {
        Timestamp timestamp = rs.getTimestamp(columnName);
        if (timestamp == null) {
            return null;
        }
        return timestamp.toLocalDateTime();
    }

    @Override
    public LocalDateTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        Timestamp timestamp = rs.getTimestamp(columnIndex);
        if (timestamp == null) {
            return null;
        }
        return timestamp.toLocalDateTime();
    }

    @Override
    public LocalDateTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        Timestamp timestamp = cs.getTimestamp(columnIndex);
        if (timestamp == null) {
            return null;
        }
        return timestamp.toLocalDateTime();
    }
}

全局注册TypeHandler

@Component
public class CustomTypeHandlerParser implements ApplicationContextAware {
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        //从spring容器获取sqlSessionFactory
        SqlSessionFactory sqlSessionFactory = applicationContext.getBean(SqlSessionFactory.class);
        //获取typeHandler注册器
        TypeHandlerRegistry typeHandlerRegistry = sqlSessionFactory.getConfiguration().getTypeHandlerRegistry();
        //注册typeHandler
        typeHandlerRegistry.register(LocalDateTime.class, LocalDateTimeTypeHandler.class);
    }
}

参考

  1. Mybatis类型处理器
  2. 达梦数据库迁移
达梦数据库适配MySQL时可能会遇到以下问题: 1. MySQL表字段与达梦数据库关键字冲突:如果MySQL表字段的名称与达梦数据库的关键字相同,在使用达梦数据库时会报错。解决方法是将名称为关键字的表字段换个名字,并将相关的SQL语句中的字段名也进行相应的更改\[1\]。 2. 达梦不支持MySQL默认时间函数:在将MySQL数据库迁移达梦数据库时,如果MySQL表的时间字段设置了默认值,使用的是CURRENT_TIMESTAMP函数,在达梦8中迁移时会报错,但在达梦7中可以正常迁移。解决方法是先将MySQL表的默认值去掉,将表结构导入达梦数据库后,再在达梦中执行语句alter table 表名 modify 字段名 default sysdate\[2\]。 3. 达梦数据库不支持LAST_INSERT_ID():在MySQL中,如果需要返回刚新增数据的自增主键值,可以使用LAST_INSERT_ID()函数。然而,在达梦数据库中不支持该函数。在使用mybatis时,可以考虑使用其他方式来获取自增主键值,例如使用SEQUENCE或UUID来生成主键\[3\]。 综上所述,达梦数据库适配MySQL时需要注意以上问题,并根据具体情况采取相应的解决方法。 #### 引用[.reference_title] - *1* *2* *3* [Mysql项目替换达梦数据库的经历——3.springboot链接达梦数据库以及注意事项和问题](https://blog.csdn.net/qq_38238045/article/details/117476106)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值