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.damenggroupId>
            <artifactId>Dm7JdbcDriver17artifactId>
            <version>7.6.0.77version>
        dependency>
        <dependency>
            <groupId>com.damenggroupId>
            <artifactId>DmDialect-for-hibernate4.0artifactId>
            <version>7.6.0.165version>
      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>

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

你可能感兴趣的:(java,数据库,mybatis,spring,boot,java,sql)