springboot-mybatis数据库从mysql迁移到达梦数据库

作者 时间
雨中星辰 2019-05-05

背景

领导要求支持国产数据库项目要适配达梦6、达梦7、gbase、金仓等国产数据库。

1. 表数据迁移

幸好达梦6有数据迁移工具,可以方便的将mysql数据迁移到达梦中。

DM管理工具->工具->DM数据迁移工具


image.png

2.mybatis迁移的遇到的问题:Caused by: java.lang.AbstractMethodError: Method dm/jdbc/driver/DmdbPreparedStatement.isClosed()Z is abstract

错误详情:

Caused by: java.lang.AbstractMethodError: Method dm/jdbc/driver/DmdbPreparedStatement.isClosed()Z is abstract
    at dm.jdbc.driver.DmdbPreparedStatement.isClosed(Unknown Source) ~[dmdriver-1.1.0.jar:na]
    at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.isClosed(HikariProxyPreparedStatement.java) ~[HikariCP-2.7.9.jar:na]
    at sun.reflect.GeneratedMethodAccessor145.invoke(Unknown Source) ~[na:na]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:45005) ~[na:1.8.0_111]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_111]
    at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:78) ~[mybatis-3.4.6.jar:3.4.6]
    at com.sun.proxy.$Proxy129.isClosed(Unknown Source) ~[na:na]
    at org.apache.ibatis.executor.BaseExecutor.closeStatement(BaseExecutor.java:285) ~[mybatis-3.4.6.jar:3.4.6]
    at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:65) ~[mybatis-3.4.6.jar:3.4.6]
    at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:326) ~[mybatis-3.4.6.jar:3.4.6]
    at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156) ~[mybatis-3.4.6.jar:3.4.6]

错误原因:

mybatis的版本与达梦6的jdbc驱动不匹配,达梦6的jdbc的驱动版本比较低。

解决办法:

将mybatis的版本降低,在项目中mybatis是通过mybatis-spring-boot-starter引入的,所以修改mybatis-spring-boot-starter的版本即可。

修改前为:

        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        

修改后为:

        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.2.0
        

3. 数据库连接池遇到的问题:Caused by: java.lang.AbstractMethodError: dm.jdbc.driver.DmdbConnection.isValid(I)Z

错误详情:

Caused by: java.lang.AbstractMethodError: dm.jdbc.driver.DmdbConnection.isValid(I)Z
    at com.zaxxer.hikari.pool.PoolBase.checkDriverSupport(PoolBase.java:445) ~[HikariCP-2.7.9.jar:na]
    at com.zaxxer.hikari.pool.PoolBase.setupConnection(PoolBase.java:412) ~[HikariCP-2.7.9.jar:na]
    at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:370) ~[HikariCP-2.7.9.jar:na]
    at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:194) ~[HikariCP-2.7.9.jar:na]
    at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:460) ~[HikariCP-2.7.9.jar:na]
    at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:534) ~[HikariCP-2.7.9.jar:na]
    at com.zaxxer.hikari.pool.HikariPool.(HikariPool.java:115) ~[HikariCP-2.7.9.jar:na]
    at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-2.7.9.jar:na]
    at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:151) ~[spring-jdbc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:115) ~[spring-jdbc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:78) ~[spring-jdbc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.mybatis.spring.transaction.SpringManagedTransaction.openConnection(SpringManagedTransaction.java:82) ~[mybatis-spring-1.3.1.jar:1.3.1]
    at org.mybatis.spring.transaction.SpringManagedTransaction.getConnection(SpringManagedTransaction.java:68) ~[mybatis-spring-1.3.1.jar:1.3.1]
    at org.apache.ibatis.executor.BaseExecutor.getConnection(BaseExecutor.java:336) ~[mybatis-3.4.2.jar:3.4.2]
    at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:84) ~[mybatis-3.4.2.jar:3.4.2]
    at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:62) ~[mybatis-3.4.2.jar:3.4.2]
    at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:324) ~[mybatis-3.4.2.jar:3.4.2]
    at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156) ~[mybatis-3.4.2.jar:3.4.2]
    at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109) ~[mybatis-3.4.2.jar:3.4.2]
    at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:83) ~[mybatis-3.4.2.jar:3.4.2]
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148) ~[mybatis-3.4.2.jar:3.4.2]
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141) ~[mybatis-3.4.2.jar:3.4.2]
    at sun.reflect.GeneratedMethodAccessor121.invoke(Unknown Source) ~[na:na]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:45005) ~[na:1.8.0_111]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_111]
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433) ~[mybatis-spring-1.3.1.jar:1.3.1]
    at com.sun.proxy.$Proxy89.selectList(Unknown Source) ~[na:na]
    at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:230) ~[mybatis-spring-1.3.1.jar:1.3.1]
    at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:137) ~[mybatis-3.4.2.jar:3.4.2]
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:75) ~[mybatis-3.4.2.jar:3.4.2]
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59) ~[mybatis-3.4.2.jar:3.4.2]
    at com.sun.proxy.$Proxy112.findAll(Unknown Source) ~[na:na]
    at sun.reflect.GeneratedMethodAccessor120.invoke(Unknown Source) ~[na:na]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:45005) ~[na:1.8.0_111]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_111]
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343) ~[spring-aop-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:197) ~[spring-aop-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) ~[spring-tx-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at com.sun.proxy.$Proxy113.findAll(Unknown Source) ~[na:na]
    at com.epri.zeus.admin.sync.RegistryServerSync.afterPropertiesSet(RegistryServerSync.java:99) ~[classes/:na]

错误原因:
springboot中默认是使用的hikariCP数据库连接池,默认使用Connection.isValid() API来判断连接不可用,但是达梦6的jdbc驱动不支持该方法。


image.png

解决办法:

修改配置文件在springboot的配置文件application.properties加入:

spring.datasource.hikari.connection-test-query=SELECT 1

4. 修改mybatis的sql文件

  1. 表名,需要用模式名."表名"来表示
    例:

修改前:

        select *
        from soft t

修改后:

        select *
        from SG_DATACENTER."soft" t
  1. 字段名,都需要用双引号引起来
    例:

修改前:

        select id,name
        from soft t

修改后

        select t."id",t."name"
        from SG_DATACENTER."soft" t
        where id=#{id}
  1. group by语句需要修改,在达梦6中,出现在select列表中的字段,如果没有在组函数中,那么必须出现在group by 子句中。(select中的字段不可以单独出现,必须出现在group语句中或者在组函数中
    例:

修改前:

        select t."id",t."name",t."type"
        from SG_DATACENTER."soft" t
        where id=#{id}
        group by t."type"

修改后:

        select t."id",t."name",t."type"
        from SG_DATACENTER."soft" t
        where id=#{id}
        group by t."id",t."name",t."type"

说明,上面只是一个演示, 在实际操作中不能这样粗暴的修改,应对应实际场景进行修改。

  1. 分页需要修改,分页插件PageHelper对国产数据库的适配比较差,需要还想用这个插件的话,就需要自行扩展,否则,就在mapper文件中添加分页语句。

具体,略。

ps:想到接下来还要适配好几种数据库就觉得头疼,幸好项目中没有太多多表联合查询,准备在接下来有空的时候将数据库改为elasticsearch。

各位大佬,有没有更好的方法?求解。

你可能感兴趣的:(springboot-mybatis数据库从mysql迁移到达梦数据库)