Mybaits整合达梦数据库

背景

项目需要使用国产达梦数据库,因此需要将mysql改造成达梦,相应的sql也需要改造。

sql改款

随便举个例子,mysql的建表语句改造成达梦,这里达梦支持的自增主键是identity,目前mybaits-plus是支持该方式的,因此我们在mybaits中使用的sequence来获取表主键的下一个值来实现主键自增。

# 达梦sql 示例
# 创建序列
create sequence seq_sys_notice
 increment by 1 -- 每次增加1
 start with 100 -- 从100开始增加
 nomaxvalue     -- 没有最大值限制
 nominvalue     -- 没有最小值限制
 cache 20;  
# 创建表结构
create table sys_notice (
  notice_id         number(20)      not null,
  notice_title      varchar2(50)    not null,
  notice_type       char(1)         not null,
  notice_content    varchar2(2000)  default null,
  status            char(1)         default '0',
  create_by         varchar2(64)    default '',
  create_time       date,
  update_by         varchar2(64)    default '',
  update_time       date,
  remark            varchar2(255)   default null
);
#指定主键
alter table sys_notice add constraint pk_sys_notice primary key (notice_id);
#增加表注释及字段注释
comment on table  sys_notice                   is '通知公告表';
comment on column sys_notice.notice_id         is '公告主键seq_sys_notice.nextval';
comment on column sys_notice.notice_title      is '公告标题';
comment on column sys_notice.notice_type       is '公告类型(1通知 2公告)';
comment on column sys_notice.notice_content    is '公告内容';
comment on column sys_notice.status            is '公告状态(0正常 1关闭)';
comment on column sys_notice.create_by         is '创建者';
comment on column sys_notice.create_time       is '创建时间';
comment on column sys_notice.update_by         is '更新者';
comment on column sys_notice.update_time       is '更新时间';
comment on column sys_notice.remark            is '备注';
maven依赖
<!--达梦驱动、方言等依赖-->
     <dependency>
            <groupId>com.dameng</groupId>
            <artifactId>Dm7JdbcDriver17</artifactId>
            <version>7.6.0.77</version>
        </dependency>
        <dependency>
            <groupId>com.dameng</groupId>
            <artifactId>DmDialect-for-hibernate4.0</artifactId>
            <version>7.6.0.165</version>
      </dependency>
配置文件
spring:
	datasource:
	        type: com.alibaba.druid.pool.DruidDataSource
	        driverClassName: dm.jdbc.driver.DmDriver
	        druid:
	            # 主库数据源
	            master:
	                url: jdbc:dm://127.0.0.1:5236/dm?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8
	                username: SYSDBA
	                password: 111111111
mybaits插入数据xml改造

使用selectKey获取序列的下一个值,属性order定义为before。

  <insert id="insertNotice" parameterType="SysNotice">
        <selectKey resultType="java.lang.Long" order="BEFORE" keyProperty="noticeId">
            SELECT SEQ_SYS_NOTICE.NEXTVAL
        </selectKey>
        insert into sys_notice (
            notice_id,
			<if test="noticeTitle != null and noticeTitle != '' ">notice_title, </if>
			<if test="noticeType != null and noticeType != '' ">notice_type, </if>
			<if test="noticeContent != null and noticeContent != '' ">notice_content, </if>
			<if test="status != null and status != '' ">status, </if>
			<if test="remark != null and remark != ''">remark,</if>
 			<if test="createBy != null and createBy != ''">create_by,</if>
 			create_time
 		)values(
 		    #{noticeId},
			<if test="noticeTitle != null and noticeTitle != ''">#{noticeTitle}, </if>
			<if test="noticeType != null and noticeType != ''">#{noticeType}, </if>
			<if test="noticeContent != null and noticeContent != ''">#{noticeContent}, </if>
			<if test="status != null and status != ''">#{status}, </if>
			<if test="remark != null and remark != ''">#{remark},</if>
 			<if test="createBy != null and createBy != ''">#{createBy},</if>
 			sysdate()
		)
    </insert>

此时,达梦数据库整合已经完毕,有用的点个赞吧!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码大师麦克劳瑞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值