mybatis-plus mysql迁移oracle实践

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

 

集成环境

spring boot 
mybatis-plus 3.3.0 
sharding

迁移改造点

  1. oracle 表名字段名小写是需要使用双引号

  2. oracle 没有limit

  3. oracle ID自增需要借助序列

  4. oracle 空串与NULL等价,均存为NULL

oracle 表名字段名小写是需要使用双引号

类com.baomidou.mybatisplus.core.config.GlobalConfig.DbConfig中存在相关配置 
capitalMode=true即可

oracle 没有limit

limit 应借助Pagenation插件实现,使用page获取其中一页

oracle ID自增需要借助序列

使用KeySequence注解以及KeyGenerator进行,相关底层代码见com.baomidou.mybatisplus.core.injector.methods.Insert#injectMappedStatement

oracle 空串与NULL等价,均存为NULL

  1. 插入值使用代码默认填充,屏蔽mysql和oracle的区别,具体见注解中TableField的fill属性,如FieldFill.INSERT

  2. 更新时仅更新非空串,见update-strategy中FieldStrategy

配置文件如下

 

mybatis-plus:

mapper-locations: classpath:mapper/*.xml

configuration:

#log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

map-underscore-to-camel-case: true

global-config:

db-config:

capital-mode: true # 大写

id-type: 2 # INPUT

insert-strategy: IGNORED # 默认填充

update-strategy: NOT_EMPTY # 非空串才更新

需要声明keyGenerator,在config类中增加如下代码配合KeySequence使用

 

@Bean

public OracleKeyGenerator oracleKeyGenerator(){

return new OracleKeyGenerator();

}

结语

本实践尚未达到oracle和mysql无代码改动迁移,后续需继续推进

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要将 MyBatis-PlusMySQL 切换到 Oracle 数据库,需要进行以下步骤: 1. 修改数据库驱动和连接信息 在 pom.xml 文件中将 MySQL 的数据库驱动和连接信息替换为 Oracle 的,例如: ``` <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc8</artifactId> <version>19.3.0.0</version> </dependency> ``` 同时,在 application.yml 或 application.properties 文件中修改数据库连接信息,例如: ``` spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl spring.datasource.username=your_username spring.datasource.password=your_password ``` 2. 修改 MyBatis-Plus 的配置 在 MyBatis-Plus 的配置文件中,需要将 MySQL 的方言替换为 Oracle 的,例如: ``` mybatis-plus: mapper-locations: classpath*:mapper/**/*.xml global-config: db-config: id-type: ID_WORKER table-prefix: mp_ field-strategy: not_empty logic-delete-value: 1 logic-not-delete-value: 0 sql-injector: com.baomidou.mybatisplus.core.injector.LogicSqlInjector configuration: map-underscore-to-camel-case: true log-impl: org.apache.ibatis.logging.stdout.StdOutImpl dialect-type: oracle ``` 3. 修改 SQL 语句 在 XML 映射文件中,需要将 MySQL 的 SQL 语句替换为 Oracle 的,例如: ``` <select id="getUserById" resultType="User"> SELECT * FROM user WHERE id = #{id} </select> ``` 修改为: ``` <select id="getUserById" resultType="User"> SELECT * FROM user WHERE id = #{id} </select> ``` 需要注意的是,Oracle 数据库中的 SQL 语法与 MySQL 有所不同,需要进行相应的修改。 完成以上步骤后,即可将 MyBatis-PlusMySQL 切换到 Oracle 数据库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值