开发喵星球

若依未分离版整合达梦数据库(192)

一、导入数据库和表

将数据库和表都迁移到达梦数据库中,详情可见官网:https://eco.dameng.com/document/dm/zh-cn/faq/faq-mysql-dm8-migrate.html

二、添加依赖

修改ruoyi-admin模块的pom.xml文件,将mysql-jdbc依赖换成dm-jdbc依赖

<dependencies>    
    <!-- 其他依赖省略 -->   
    <!-- Mysql驱动包 -->
<!--        <dependency>-->
<!--            <groupId>mysql</groupId>-->
<!--            <artifactId>mysql-connector-java</artifactId>-->
<!--        </dependency>-->

    <!-- 达梦驱动包 -->
    <dependency>
        <groupId>com.dameng</groupId>
        <artifactId>Dm8JdbcDriver18</artifactId>
        <version>8.1.1.49</version>
    </dependency>
</dependencies>

注意jdbc包的版本:Dm7JdbcDriver18中7是DM数据库的版本,18是JDK版本。

二、数据库连接池配置

配置修改如下:

# 数据源配置
spring:
    datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        driverClassName: dm.jdbc.driver.DmDriver
        druid:
            # 主库数据源
            master:
                url: jdbc:dm://[ip]:[port]
                username: SYSDBA
                password: ***

三、Pagehelper设置

修改如下:

# PageHelper分页插件
pagehelper: 
  helperDialect: oracle
  supportMethodsArguments: true
  params: count=countSql 

四、适配DM不支持的SQL函数

1、replace into函数:

记录在线用户功能,SysUserOnlineMapper里的saveOnline方法,用到了不支持的replace into函数,需替换成merge into函数;

<insert id="saveOnline" parameterType="SysUserOnline">
    replace into sys_user_online(sessionId, login_name, dept_name, ipaddr, login_location, browser, os, status, start_timestamp, last_access_time, expire_time)
    values (#{sessionId}, #{loginName}, #{deptName}, #{ipaddr}, #{loginLocation}, #{browser}, #{os}, #{status}, #{startTimestamp}, #{lastAccessTime}, #{expireTime})
</insert>

修改后:

<insert id="saveOnline" parameterType="SysUserOnline">
    merge into sys_user_online
        using (select #{sessionId} sessionId, #{loginName} login_name, #{deptName} dept_name, #{ipaddr} ipaddr, #{loginLocation} login_location, #{browser} browser, #{os} os,
                          #{tatus} status, #{startTimestamp} start_timestamp, #{lastAccessTime} last_access_time, #{expireTime} expire_time from dual) d
            on sys_user_online.sessionId = d.sessionId
            when matched then
                update set sys_user_online.login_name = d.login_name, sys_user_online.dept_name = d.dept_name, sys_user_online.ipaddr = d.ipaddr,
                    sys_user_online.login_location = d.login_location, sys_user_online.browser = d.browser, sys_user_online.os = d.os, sys_user_online.status = d.status,
                    sys_user_online.start_timestamp = d.start_timestamp, sys_user_online.last_access_time = d.last_access_time, sys_user_online.expire_time = d.expire_time
            when not matched then
                insert (sessionId, login_name, dept_name, ipaddr, login_location, browser, os, status, start_timestamp, last_access_time, expire_time)
                    values(d.sessionId, d.login_name, d.dept_name, d.ipaddr, d.login_location, d.browser, d.os, d.status, d.start_timestamp, d.last_access_time, d.expire_time)
</insert>

2. find_in_set

find_in_set函数替换成instr函数
在部门管理,SysDeptMapper里用到了不支持的find_in_set函数,替换成instr函数;

-- 原函数
find_in_set(#{deptId}, ancestors)
-- 新函数
instr(','||ancestors||',' ,  ','|| #{deptId} ||',') 

char类型,长度由1变成4,读出的数据会补空,如‘1 ’,会引起系统显示/反填异常;

五、Mapper代码修改

达梦数据库中查询表和列定义信息的简要说明:
1. SYS.SYSOBJECTS,记录系统中所有对象的信息,表定义信息从这里获取;
2. SYS.SYSTABLECOMMENTS,记录表或视图的注释信息;
3. SYS.SYSCOLUMNS,记录列定义信息;
4. SYS.SYSCOLUMNCOMMENTS,记录列的注释信息;
5. 获取当前模式名称:select CURR_SCH from V$SESSIONS LIMIT 1;

1、获取表定义信息,修改GenTableMapper.xml

1. selectDbTableList修改:

原代码:

<select id="selectDbTableList" parameterType="GenTable" resultMap="GenTableResult">
    select table_name, table_comment, create_time, update_time from information_schema.tables
    where table_schema = (select database())
    AND table_name NOT LIKE 'QRTZ_%' AND table_name NOT LIKE 'gen_%'
    AND table_name NOT IN (select table_name from gen_table)
    <if test="tableName != null and tableName != ''">
        AND lower(table_name) like lower(concat('%', #{tableName}, '%'))
    </if>
    <if test="tableComment != null and tableComment != ''">
        AND lower(table_comment) like lower(concat('%', #{tableComment}, '%'))
    </if>
        order by create_time desc
</select>

修改后代码:

<select id="selectDbTableList" parameterType="GenTable" resultMap="GenTableResult">
    select so.NAME table_name, st.COMMENTtable_comment, so.CRTDATE create_time from SYS.SYSOBJECTS so left join SYS.SYSTABLECOMMENTS st on so.NAME = st.TVNAME and st.TABLE_TYPE = 'TABLE' and st.SCHNAME = (select CURR_SCH from VSESSIONS LIMIT 1)
    where so.TYPE='SCHOBJ' and so.SUBTYPE='UTAB' and so.PID = -1 and INSTR(so.NAME,'##') = 0 and so.SCHID = (select ID from SYS.SYSOBJECTS where TYPE='SCH' and NAME= (select CURR_SCH from VSESSIONS LIMIT 1)) and so.NAME NOT LIKE 'QRTZ_%' AND so.NAME NOT LIKE 'gen_%'
    and so.NAME NOT IN (select table_name from gen_table)
    <if test="tableName != null and tableName != ''">
    and lower(so.NAME) like lower(concat('%', #{tableName}, '%'))
    </if>
    <if test="tableComment != null and tableComment != ''">
    and lower(st.COMMENT$) like lower(concat('%', #{tableComment}, '%'))
    </if>
    order by so.CRTDATE desc
</select>

2. 方法selectDbTableListByNames修改:

原代码:

<select id="selectDbTableListByNames" resultMap="GenTableResult">
    select table_name, table_comment, create_time, update_time from information_schema.tables
    where table_name NOT LIKE 'qrtz_%' and table_name NOT LIKE 'gen_%' and table_schema = (select database())
    and table_name in
    <foreach collection="array" item="name" open="(" separator="," close=")">
    #{name}
    </foreach> 
</select>

修改后代码:

<select id="selectDbTableListByNames" resultMap="GenTableResult">
    select so.NAME table_name, st.COMMENTtable_comment, so.CRTDATE create_time from SYS.SYSOBJECTS so left join SYS.SYSTABLECOMMENTS st on so.NAME = st.TVNAME and st.TABLE_TYPE = 'TABLE' and st.SCHNAME = (select CURR_SCH from VSESSIONS LIMIT 1)
    where so.TYPE='SCHOBJ' and so.SUBTYPE='UTAB' and so.PID = -1 and INSTR(so.NAME,'##') = 0 and so.SCHID = (select ID from SYS.SYSOBJECTS where TYPE='SCH' and NAME= (select CURR_SCH from VSESSIONS LIMIT 1)) and so.NAME NOT LIKE 'qrtz_%' and so.NAME NOT LIKE 'gen_%'
    and so.NAME in
    <foreach collection="array" item="name" open="(" separator="," close=")">
    #{name}
    </foreach> 
</select>

2、获取列定义信息,修改GenTableColumnMapper

selectDbTableColumnsByName修改
原代码:

<select id="selectDbTableColumnsByName" parameterType="String" resultMap="GenTableColumnResult">
    select column_name, (case when (is_nullable = 'no' <![CDATA[ && ]]> column_key != 'PRI') then '1' else null end) as is_required, (case when column_key = 'PRI' then '1' else '0' end) as is_pk,ordinal_position as sort, column_comment, (case when extra = 'auto_increment' then '1' else '0' end) as is_increment, column_type 
    from information_schema.columns where table_schema = (select database()) and table_name = (#{tableName}) 
    order by ordinal_position
</select>

修改后代码:

<select id="selectDbTableColumnsByName" parameterType="String" resultMap="GenTableColumnResult">
    select sc.NAME column_name, (case when sc.NULLABLE= 'N' then '1' else null end) as is_required, (case when sc.INFO2 = 1 then '1' else '0'  end) is_pk, sc.COLID sort, scc.COMMENT column_comment, (case when sc.INFO2 = 1 then '1' else '0' end) as is_increment, LOWER(sc.TYPE) column_type
    from SYS.SYSCOLUMNS sc left join SYS.SYSOBJECTS so on sc.ID = so.ID left join SYS.SYSCOLUMNCOMMENTS SCC on sc.NAME = scc.COLNAME and scc.TABLE_TYPE = 'TABLE' and scc.SCHNAME = (select CURR_SCH from VSESSIONS LIMIT 1) and scc.TVNAME= so.NAME
    where so.TYPE='SCHOBJ' and so.SUBTYPE='UTAB' and so.PID = -1 and so.SCHID = (select ID from SYS.SYSOBJECTS where TYPE='SCH' and NAME= (select CURR_SCH from VSESSIONS LIMIT 1)) and so.NAME = #{tableName}
    order by sc.COLID
</select>

六、工具类修改:

1、GenConstants

位置: com/ruoyi/common/constant/GenConstants.java

// 增加两个常量,用于判断浮点数和整型
public class GenConstants{
......
    /** 浮点数 */
    public static final String[] COLUMNTYPE_NUMBER_DOUBLE = {  "number", "float", "double", "decimal" };

    /** 整型 */
    public static final String[] COLUMNTYPE_NUMBER_INTEGER = { "tinyint", "smallint", "mediumint", "int", "integer",
            "bit" };
......
}

2、GenUtils

位置:com/ruoyi/generator/util/GenUtils.java

public class GenUtils{
    ...
    /**
     * 初始化列属性字段
     */
    public static void initColumnField(GenTableColumn column, GenTable table){
        ...
        else if (arraysContains(GenConstants.COLUMNTYPE_NUMBER, dataType))
        {
            column.setHtmlType(GenConstants.HTML_INPUT);

            /* 原代码
            // 如果是浮点型 统一用BigDecimal
            String[] str = StringUtils.split(StringUtils.substringBetween(column.getColumnType(), "(", ")"), ",");
            if (str != null && str.length == 2 && Integer.parseInt(str[1]) > 0)
            {
                column.setJavaType(GenConstants.TYPE_BIGDECIMAL);
            }
            // 如果是整形
            else if (str != null && str.length == 1 && Integer.parseInt(str[0]) <= 10)
            {
                column.setJavaType(GenConstants.TYPE_INTEGER);
            }
            // 长整形
            else
            {
                column.setJavaType(GenConstants.TYPE_LONG);
            }*/

            /** 修改后的代码 */
            // 如果是浮点型 统一用Double
            if (arraysContains(GenConstants.COLUMNTYPE_NUMBER_DOUBLE, dataType))
            {
                column.setJavaType(GenConstants.TYPE_DOUBLE);
            }
            // 如果是整形
            else if (arraysContains(GenConstants.COLUMNTYPE_NUMBER_INTEGER, dataType))
            {
                column.setJavaType(GenConstants.TYPE_INTEGER);
            }
            // 长整形
            else
            {
                column.setJavaType(GenConstants.TYPE_LONG);
            }
        }
        ...
    }
    ...
}
   
分类:Java/OOP 作者:无限繁荣, 吴蓉 发表于:2024-04-28 23:32:06 阅读量:169
<<   >>


powered by kaifamiao