mysql、oracle、postgresql兼容适配

一、sql使用区别

1. dual表

oracle独有的表,目的是限制sql语句结构完整

select (select * from table_name where age = 20) t from dual

mysql和pgsql没有这张表,可以直接去掉

select (select * from table_name where age = 20) t

2. 布尔类型

oracle和mysql没有boolean类型,可使用number(int)char代替

pgsql中有bool类型,数字和字符自动转换为boolean类型(0→f、1→t、no→f、yes→t)

3. update表别名

pgsql不适用,mysql 和 oracle支持

update table_name t set t.name = 'abc' where id = 1

4. 字符串传值

pgsql 、oracle 仅支持单引号

select * from table_name where name = 'abc'

mysql 单引号/双引号都支持

select * from table_name where name = "abc"

5. 批量插入

mysql、pgsql批量插入

insert into table_name() values()

oracle批量插入

insert all into table_name() values()

6. ......

二、 mybatis兼容不同数据库

使用if标签判断_databaseId,分别适配不同的数据库,具体代码如下:

<insert id="insertBatch" parameterType="java.util.List">
    <if test="_databaseId=='mysql' or _databaseId=='postgresql'">
        insert into table_name 
        (<include refid="insertBatchColumn"></include>)
        values
        <foreach collection="list" item="item" index="index" separator="," >
            (<include refid="insertBatchValue"></include>)
        </foreach>
    </if>
    <if test="_databaseId=='oracle'">
        insert all
        <foreach collection="list" item="item" index="index" separator="">
            into table_name 
            (<include refid="insertBatchColumn"></include>)
            values (<include refid="insertBatchValue"></include>)
        </foreach>
        select * from dual
    </if>
</insert>

<sql id="insertBatchColumn">
    id,name,age,gender
</sql>
<sql id="insertBatchValue">
    #{item.id,jdbcType=VARCHAR}, #{item.name,jdbcType=VARCHAR}, 
    #{item.age,jdbcType=INTEGER},#{item.gender,jdbcType=INTEGER}
</sql>

原创不易,转载请注明来源

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值