mapper适配【从mysql到oracle】

斜单鹗
2023-12-01

迁移注意点:

  1. 时间函数兼容
    str_to_date -> to_date
    date_format -> to_char
    now()->sysdate
    时间格式化:
    ‘%Y-%m-%d %H:%i:%s’ -> ‘yyyy-mm-dd hh24:mi:ss’
    https://www.cnblogs.com/ladyM/p/8378548.html

  2. 分页兼容
    limit -> rownum
    https://blog.csdn.net/use_admin/article/details/83622414

# mysql
select * from tablename
where xxx = xx
order by xxx desc
limit #{startPage}, #{count}
# oracle
select * from (
select tt.*, ROWNUM as rowno from (
select * from tablename
where xxx = xx
order by xxx desc
) tt 
where ROWNUM <= #{startPage} + #{count}) table_alias
where table_alias.rowno > #{startPage}
  1. 关键字字段查询不支持` `,要大写并加" "

  2. 默认current_timestamp类型的字段也需要显式赋值

  3. 字符串拼接函数concat用法区别
    oracle 三个字段及以上用concat拼接需要嵌套两个concat:

#mysql
select concat('%', 'test’, '%’)
#oracle 不支持mysql多参数用法
select concat(concat('%', 'test’), '%’)

https://www.cnblogs.com/sunchunmei/p/12169964.html

  1. mybatis mapper条件语句中的NULL要改为小写
<if test="item.name != null and item.name != ''>
  1. 添加自增序列,修改mapper insert语句
<selectKey keyProperty="id" resultType="java.lang.Integer" order="BEFORE">
	select MCC_XXX_SEQUENCE.nextval from dual
</selectKey>

也可以在insert语句直接使用

insert into table_name (id,xxx) values (MCC_XXX_SEQUENCE.nextval, #{xxx});
  1. mybatis 批量更新
<!-- mysql中为 open="" close=";" -->
<foreach collection="batchUpdateParams" index="index" item="item" separator=";" open="begin" close=";end;">
	update table_name
	<set>
		<if test="...">...</if>
	</set>
	where ...
</foreach>
  1. 空值容错
    oracle下 #{null} 会报错:参数类型不明确,需要加上对应的jdbcType
mysql:
#{column_name}
oracle:
#{column_name, jdbcType=VARCHAR}
  1. oracle特殊关键字兼容
    mysql的关键字不需要SQL中特殊处理;
    oracle则有以下关键字需要注意:
select level,resource,date,number

以上字段作为表字段时sql中需要转大写加双引号处理:

select "LEVEL","RESOURCE","DATE","NUMBER"
 类似资料: