若依分离端+人大金仓,代码生成模块报错

    一、异常

        若依分离版,框架整合人大金仓数据库后,代码生成模块无法正常使用,导入表出现异常报错如图

       

二、后端报错查看

org.springframework.jdbc.BadSqlGrammarException: 
### Error querying database.  Cause: com.kingbase8.util.KSQLException: 错误: 字段 "table_comment" 不存在
  Position: 44 At Line: 2, Line Position: 20
### The error may exist in file [D:\文章演示\RuoYi-Vue\ruoyi-generator\target\classes\mapper\generator\GenTableMapper.xml]
### The error may involve com.ruoyi.generator.mapper.GenTableMapper.selectDbTableList-Inline
### The error occurred while setting parameters
### SQL: select count(0) from (  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)                         order by create_time desc  ) tmp_count
### Cause: com.kingbase8.util.KSQLException: 错误: 字段 "table_comment" 不存在
  Position: 44 At Line: 2, Line Position: 20
; bad SQL grammar []; nested exception is com.kingbase8.util.KSQLException: 错误: 字段 "table_comment" 不存在
  Position: 44 At Line: 2, Line Position: 20

        可以看到日志主要异常时出现在SQL上,又是各种字段不存在......在登录失效的文章中已经提到过类似场景人大金仓新版本,若依登录失效问题-CSDN博客;就是已经知道我们不去配置默认模式,只要把表都放进public就可以正常读取的。

三、异常分析

3.1 代码生成器原理(引用若依管理系统RuoYi-Vue(三):代码生成器原理和实战 - 狂盗一枝梅 - 博客园 (cnblogs.com)

1.Velocity

Velocity是一个基于Java的模板引擎,其提供了一个Context容器,在java代码里面我们可以往容器中存值,然后在vm文件中使用特定的语法获取,这是velocity基本的用法,其与jsp、freemarker并称为三大视图展现技术。作为一个模块引擎,除了作为前后端分离的MVC展现层,Velocity还有一些其他用途,比如源代码生成。

在若依Vue系统中,正是使用了Velocity技术实现的源代码生成。大体上,源代码生成只需三步走:

  1. 创建模板文件
  2. 准备上下文(变量值)
  3. 替换模板文件中的变量

三步走完之后源代码就生成了,说起来是很简单的,但是实际上做起来会比较麻烦,特别是第一步创建模板文件是最复杂的,以下为index.vue模板文件部分源代码:

#foreach($column in $columns)
#if($column.query)
#set($dictType=$column.dictType)
#set($AttrName=$column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
#set($parentheseIndex=$column.columnComment.indexOf("("))
#if($parentheseIndex != -1)
#set($comment=$column.columnComment.substring(0, $parentheseIndex))
#else
#set($comment=$column.columnComment)
#end
#if($column.htmlType == "input")
...

可以看到,该vue模板文件中充斥着大量Velocity的if-else语法,嵌套在一起更是显得无比复杂。

总之,整体上来看,java代码的模板比较简单,vue和mybatis mapper的模板文件比较复杂。

2.information_schema 数据库

mysql数据库中有一个information_schema数据库,它是mysql的系统数据库之一,它里面存储着两个表TABLES以及COLUMNS,这两个表分别存储着所有的表信息以及所有表中的列信息,代码生成器正是以两张表的信息为核心实现的。

3.ruoyi-vue代码生成器源码分析

ruoyi-vue代码生成器相关代码均位于ruoyi-generator模块中,根据之前的实际操作体验上来看,最简单的情况,前端页面只需要两步即可完成代码生成

  • 导入表结构
  • 生成代码

实际上这两步对应着后端的两个接口:com.ruoyi.generator.controller.GenController#importTableSave 和 com.ruoyi.generator.controller.GenController#batchGenCode ,生成源码的步骤就要从这两步下手。

首先看com.ruoyi.generator.controller.GenController#importTableSave 接口,它做了以下这些事情

  1. 从information_schema数据库的tables表中查询目标表的表明、标注释、创建时间和更新时间,但是忽略掉定时任务的表和已经生成过的表。
  2. 初始化表数据并将数据插入ruoyi数据库的gen_table表
  3. 从information_schema数据库的columns表中查询目标表的列信息,包含字段名、字段注释、字段类型、是否允许为null等详细信息
  4. 初始化列信息并将数据插入ruoyi数据库的gen_table_column表

接下来看下 com.ruoyi.generator.controller.GenController#batchGenCode 接口,它做了以下这些事情

  1. 从ruoyi数据库的gen_table、gen_table_column表查询出生成代码需要的表和列信息。
  2. 初始化Velocity
  3. 准备Velocity上下文信息(变量值信息)
  4. 读取模板、渲染模板,然后将渲染后的模板内容添加进如压缩流,之后前端就可以下载zip压缩文件了。

3.2 故障分析

        由上可以知道若依在MySQL下代码生成需要在 information_schema获取所需要代码生成的表的信息以及行信息,那么原因就很明显了就是人大金仓数据库下并没有information_schema或者说根本不是这个表。

        information_schema

        information_schema 数据库跟 performance_schema 一样,都是 MySQL 自带的信息数据库。其中 performance_schema 用于性能分析,而 information_schema 用于存储数据库元数据(关于数据的数据),例如数据库名、表名、列的数据类型、访问权限等。

四、解决方案

        知道具体原因了,就好解决了,这个部分我去查阅了相关文献,但是并没有找到很好的解决方法,最后因为postgreSQL与人大金仓很相近,所以我决定用postgreSQL的方式改写代码生成的相关SQL

GenTableMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.generator.mapper.GenTableMapper">

	<resultMap type="GenTable" id="GenTableResult">
	    <id     property="tableId"        column="table_id"          />
		<result property="tableName"      column="table_name"        />
		<result property="tableComment"   column="table_comment"     />
		<result property="subTableName"   column="sub_table_name"    />
		<result property="subTableFkName" column="sub_table_fk_name" />
		<result property="className"      column="class_name"        />
		<result property="tplCategory"    column="tpl_category"      />
		<result property="packageName"    column="package_name"      />
		<result property="moduleName"     column="module_name"       />
		<result property="businessName"   column="business_name"     />
		<result property="functionName"   column="function_name"     />
		<result property="functionAuthor" column="function_author"   />
		<result property="genType"        column="gen_type"          />
		<result property="genPath"        column="gen_path"          />
		<result property="options"        column="options"           />
		<result property="createBy"       column="create_by"         />
		<result property="createTime"     column="create_time"       />
		<result property="updateBy"       column="update_by"         />
		<result property="updateTime"     column="update_time"       />
		<result property="remark"         column="remark"            />
		<collection property="columns" javaType="java.util.List" resultMap="GenTableColumnResult" />
	</resultMap>

	<resultMap type="GenTableColumn" id="GenTableColumnResult">
        <id     property="columnId"       column="column_id"      />
        <result property="tableId"        column="table_id"       />
        <result property="columnName"     column="column_name"    />
        <result property="columnComment"  column="column_comment" />
        <result property="columnType"     column="column_type"    />
        <result property="javaType"       column="java_type"      />
        <result property="javaField"      column="java_field"     />
        <result property="isPk"           column="is_pk"          />
        <result property="isIncrement"    column="is_increment"   />
        <result property="isRequired"     column="is_required"    />
        <result property="isInsert"       column="is_insert"      />
        <result property="isEdit"         column="is_edit"        />
        <result property="isList"         column="is_list"        />
        <result property="isQuery"        column="is_query"       />
        <result property="queryType"      column="query_type"     />
        <result property="htmlType"       column="html_type"      />
        <result property="dictType"       column="dict_type"      />
        <result property="sort"           column="sort"           />
        <result property="createBy"       column="create_by"      />
        <result property="createTime"     column="create_time"    />
        <result property="updateBy"       column="update_by"      />
        <result property="updateTime"     column="update_time"    />
    </resultMap>

	<sql id="selectGenTableVo">
        select table_id, table_name, table_comment, sub_table_name, sub_table_fk_name, class_name, tpl_category, package_name, module_name, business_name, function_name, function_author, gen_type, gen_path, options, create_by, create_time, update_by, update_time, remark from gen_table
    </sql>

    <select id="selectGenTableList" parameterType="GenTable" resultMap="GenTableResult">
		<include refid="selectGenTableVo"/>
		<where>
			<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>
		</where>
	</select>

	<select id="selectDbTableList" parameterType="GenTable" resultMap="GenTableResult">
		SELECT * FROM information_schema.tables
		<where>
		    table_schema='public'
		  and table_catalog='ry-vue'
		  and  table_type='BASE TABLE'
		<if test="tableName != null and tableName != ''">
			AND lower(table_name) like lower(concat('%', #{tableName}, '%'))
		</if>
		</where>
	</select>


	<select id="selectDbTableListByNames" resultMap="GenTableResult">
		SELECT tb.table_name, d.description as table_comment
		FROM information_schema.tables tb
		JOIN pg_class c ON c.relname = tb.table_name
		LEFT JOIN pg_description d ON d.objoid = c.oid AND d.objsubid = '0'
		where table_name NOT LIKE 'qrtz_%' and table_name NOT LIKE 'gen_%' and tb.table_schema = 'public'
		and table_name in
		<foreach collection="array" item="name" open="(" separator="," close=")">
			#{name}
		</foreach>
	</select>

	<select id="selectTableByName" parameterType="String" resultMap="GenTableResult">
		SELECT tb.table_name, d.description as table_comment
		FROM information_schema.tables tb
				 JOIN pg_class c ON c.relname = tb.table_name
				 LEFT JOIN pg_description d ON d.objoid = c.oid AND d.objsubid = '0'
		WHERE tb.table_schema = 'public'

		  AND tb.table_name=#{tableName}
			AND d.description <![CDATA[ <> ]]> ''
	</select>

	<select id="selectGenTableById" parameterType="Long" resultMap="GenTableResult">
	    SELECT t.table_id, t.table_name, t.table_comment, t.sub_table_name, t.sub_table_fk_name, t.class_name, t.tpl_category, t.package_name, t.module_name, t.business_name, t.function_name, t.function_author, t.gen_type, t.gen_path, t.options, t.remark,
			   c.column_id, c.column_name, c.column_comment, c.column_type, c.java_type, c.java_field, c.is_pk, c.is_increment, c.is_required, c.is_insert, c.is_edit, c.is_list, c.is_query, c.query_type, c.html_type, c.dict_type, c.sort
		FROM gen_table t
			 LEFT JOIN gen_table_column c ON t.table_id = c.table_id
		where t.table_id = #{tableId} order by c.sort
	</select>

	<select id="selectGenTableByName" parameterType="String" resultMap="GenTableResult">
	    SELECT t.table_id, t.table_name, t.table_comment, t.sub_table_name, t.sub_table_fk_name, t.class_name, t.tpl_category, t.package_name, t.module_name, t.business_name, t.function_name, t.function_author, t.gen_type, t.gen_path, t.options, t.remark,
			   c.column_id, c.column_name, c.column_comment, c.column_type, c.java_type, c.java_field, c.is_pk, c.is_increment, c.is_required, c.is_insert, c.is_edit, c.is_list, c.is_query, c.query_type, c.html_type, c.dict_type, c.sort
		FROM gen_table t
			 LEFT JOIN gen_table_column c ON t.table_id = c.table_id
		where t.table_name = #{tableName} order by c.sort
	</select>

	<select id="selectGenTableAll" parameterType="String" resultMap="GenTableResult">
	    SELECT t.table_id, t.table_name, t.table_comment, t.sub_table_name, t.sub_table_fk_name, t.class_name, t.tpl_category, t.package_name, t.module_name, t.business_name, t.function_name, t.function_author, t.gen_type, t.gen_path, t.options, t.remark,
			   c.column_id, c.column_name, c.column_comment, c.column_type, c.java_type, c.java_field, c.is_pk, c.is_increment, c.is_required, c.is_insert, c.is_edit, c.is_list, c.is_query, c.query_type, c.html_type, c.dict_type, c.sort
		FROM gen_table t
			 LEFT JOIN gen_table_column c ON t.table_id = c.table_id
		order by c.sort
	</select>

	<insert id="insertGenTable" parameterType="GenTable" useGeneratedKeys="true" keyProperty="tableId">
        insert into gen_table (
			<if test="tableName != null">table_name,</if>
			<if test="tableComment != null and tableComment != ''">table_comment,</if>
			<if test="className != null and className != ''">class_name,</if>
			<if test="tplCategory != null and tplCategory != ''">tpl_category,</if>
			<if test="packageName != null and packageName != ''">package_name,</if>
			<if test="moduleName != null and moduleName != ''">module_name,</if>
			<if test="businessName != null and businessName != ''">business_name,</if>
			<if test="functionName != null and functionName != ''">function_name,</if>
			<if test="functionAuthor != null and functionAuthor != ''">function_author,</if>
			<if test="genType != null and genType != ''">gen_type,</if>
			<if test="genPath != null and genPath != ''">gen_path,</if>
			<if test="remark != null and remark != ''">remark,</if>
 			<if test="createBy != null and createBy != ''">create_by,</if>
			create_time
         )values(
			<if test="tableName != null">#{tableName},</if>
			<if test="tableComment != null and tableComment != ''">#{tableComment},</if>
			<if test="className != null and className != ''">#{className},</if>
			<if test="tplCategory != null and tplCategory != ''">#{tplCategory},</if>
			<if test="packageName != null and packageName != ''">#{packageName},</if>
			<if test="moduleName != null and moduleName != ''">#{moduleName},</if>
			<if test="businessName != null and businessName != ''">#{businessName},</if>
			<if test="functionName != null and functionName != ''">#{functionName},</if>
			<if test="functionAuthor != null and functionAuthor != ''">#{functionAuthor},</if>
			<if test="genType != null and genType != ''">#{genType},</if>
			<if test="genPath != null and genPath != ''">#{genPath},</if>
			<if test="remark != null and remark != ''">#{remark},</if>
 			<if test="createBy != null and createBy != ''">#{createBy},</if>
			sysdate()
         )
    </insert>

    <update id="updateGenTable" parameterType="GenTable">
        update gen_table
        <set>
            <if test="tableName != null">table_name = #{tableName},</if>
            <if test="tableComment != null and tableComment != ''">table_comment = #{tableComment},</if>
            <if test="subTableName != null">sub_table_name = #{subTableName},</if>
            <if test="subTableFkName != null">sub_table_fk_name = #{subTableFkName},</if>
            <if test="className != null and className != ''">class_name = #{className},</if>
            <if test="functionAuthor != null and functionAuthor != ''">function_author = #{functionAuthor},</if>
            <if test="genType != null and genType != ''">gen_type = #{genType},</if>
            <if test="genPath != null and genPath != ''">gen_path = #{genPath},</if>
            <if test="tplCategory != null and tplCategory != ''">tpl_category = #{tplCategory},</if>
            <if test="packageName != null and packageName != ''">package_name = #{packageName},</if>
            <if test="moduleName != null and moduleName != ''">module_name = #{moduleName},</if>
            <if test="businessName != null and businessName != ''">business_name = #{businessName},</if>
            <if test="functionName != null and functionName != ''">function_name = #{functionName},</if>
            <if test="options != null and options != ''">options = #{options},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="remark != null">remark = #{remark},</if>
            update_time = sysdate()
        </set>
        where table_id = #{tableId}
    </update>

    <delete id="deleteGenTableByIds" parameterType="Long">
        delete from gen_table where table_id in
        <foreach collection="array" item="tableId" open="(" separator="," close=")">
            #{tableId}
        </foreach>
    </delete>

</mapper>

GenTableColumnMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.generator.mapper.GenTableColumnMapper">

    <resultMap type="GenTableColumn" id="GenTableColumnResult">
        <id     property="columnId"       column="column_id"      />
        <result property="tableId"        column="table_id"       />
        <result property="columnName"     column="column_name"    />
        <result property="columnComment"  column="column_comment" />
        <result property="columnType"     column="column_type"    />
        <result property="javaType"       column="java_type"      />
        <result property="javaField"      column="java_field"     />
        <result property="isPk"           column="is_pk"          />
        <result property="isIncrement"    column="is_increment"   />
        <result property="isRequired"     column="is_required"    />
        <result property="isInsert"       column="is_insert"      />
        <result property="isEdit"         column="is_edit"        />
        <result property="isList"         column="is_list"        />
        <result property="isQuery"        column="is_query"       />
        <result property="queryType"      column="query_type"     />
        <result property="htmlType"       column="html_type"      />
        <result property="dictType"       column="dict_type"      />
        <result property="sort"           column="sort"           />
        <result property="createBy"       column="create_by"      />
        <result property="createTime"     column="create_time"    />
        <result property="updateBy"       column="update_by"      />
        <result property="updateTime"     column="update_time"    />
    </resultMap>

	<sql id="selectGenTableColumnVo">
        select column_id, table_id, column_name, column_comment, column_type, java_type, java_field, is_pk, is_increment, is_required, is_insert, is_edit, is_list, is_query, query_type, html_type, dict_type, sort, create_by, create_time, update_by, update_time from gen_table_column
    </sql>

    <select id="selectGenTableColumnListByTableId" parameterType="GenTableColumn" resultMap="GenTableColumnResult">
        <include refid="selectGenTableColumnVo"/>
        where table_id = #{tableId}
        order by sort
    </select>

    <select id="selectDbTableColumnsByName" parameterType="String" resultMap="GenTableColumnResult">
        select ordinal_position as sort,column_name ,data_type as column_type,
               case is_nullable when 'NO' then 1 else 0 end as is_required,
               case when position('nextval' in column_default)>0 then 1 else 0 end as is_increment,
               case when b.pk_name is null then 0 else 1 end as is_pk,c.DeText as column_comment
        from information_schema.columns
                 left join (
            select pg_attr.attname as colname,pg_constraint.conname as pk_name from pg_constraint
              inner join pg_class on pg_constraint.conrelid = pg_class.oid
              inner join pg_attribute pg_attr on pg_attr.attrelid = pg_class.oid and pg_attr.attnum = pg_constraint.conkey[1]
              inner join pg_type on pg_type.oid = pg_attr.atttypid
            where pg_class.relname = (#{tableName}) and pg_constraint.contype='p'
        ) b on b.colname = information_schema.columns.column_name
                 left join (
            select attname,description as DeText from pg_class
                 left join pg_attribute pg_attr on pg_attr.attrelid= pg_class.oid
                   left join pg_description pg_desc on pg_desc.objoid = pg_attr.attrelid and pg_desc.objsubid=pg_attr.attnum
            where pg_attr.attnum>0 and pg_attr.attrelid=pg_class.oid and pg_class.relname=#{tableName}
        )c on c.attname = information_schema.columns.column_name
        where table_schema='public' and table_name=(#{tableName}) order by ordinal_position asc
	</select>

    <insert id="insertGenTableColumn" parameterType="GenTableColumn" useGeneratedKeys="true" keyProperty="columnId">
        insert into gen_table_column (
			<if test="tableId != null and tableId != ''">table_id,</if>
			<if test="columnName != null and columnName != ''">column_name,</if>
			<if test="columnComment != null and columnComment != ''">column_comment,</if>
			<if test="columnType != null and columnType != ''">column_type,</if>
			<if test="javaType != null and javaType != ''">java_type,</if>
			<if test="javaField != null  and javaField != ''">java_field,</if>
			<if test="isPk != null and isPk != ''">is_pk,</if>
			<if test="isIncrement != null and isIncrement != ''">is_increment,</if>
			<if test="isRequired != null and isRequired != ''">is_required,</if>
			<if test="isInsert != null and isInsert != ''">is_insert,</if>
			<if test="isEdit != null and isEdit != ''">is_edit,</if>
			<if test="isList != null and isList != ''">is_list,</if>
			<if test="isQuery != null and isQuery != ''">is_query,</if>
			<if test="queryType != null and queryType != ''">query_type,</if>
			<if test="htmlType != null and htmlType != ''">html_type,</if>
			<if test="dictType != null and dictType != ''">dict_type,</if>
			<if test="sort != null">sort,</if>
			<if test="createBy != null and createBy != ''">create_by,</if>
			create_time
         )values(
			<if test="tableId != null and tableId != ''">#{tableId},</if>
			<if test="columnName != null and columnName != ''">#{columnName},</if>
			<if test="columnComment != null and columnComment != ''">#{columnComment},</if>
			<if test="columnType != null and columnType != ''">#{columnType},</if>
			<if test="javaType != null and javaType != ''">#{javaType},</if>
			<if test="javaField != null and javaField != ''">#{javaField},</if>
			<if test="isPk != null and isPk != ''">#{isPk},</if>
			<if test="isIncrement != null and isIncrement != ''">#{isIncrement},</if>
			<if test="isRequired != null and isRequired != ''">#{isRequired},</if>
			<if test="isInsert != null and isInsert != ''">#{isInsert},</if>
			<if test="isEdit != null and isEdit != ''">#{isEdit},</if>
			<if test="isList != null and isList != ''">#{isList},</if>
			<if test="isQuery != null and isQuery != ''">#{isQuery},</if>
			<if test="queryType != null and queryType != ''">#{queryType},</if>
			<if test="htmlType != null and htmlType != ''">#{htmlType},</if>
			<if test="dictType != null and dictType != ''">#{dictType},</if>
			<if test="sort != null">#{sort},</if>
			<if test="createBy != null and createBy != ''">#{createBy},</if>
			sysdate()
         )
    </insert>

    <update id="updateGenTableColumn" parameterType="GenTableColumn">
        update gen_table_column
        <set>
            column_comment = #{columnComment},
            java_type = #{javaType},
            java_field = #{javaField},
            is_insert = #{isInsert},
            is_edit = #{isEdit},
            is_list = #{isList},
            is_query = #{isQuery},
            is_required = #{isRequired},
            query_type = #{queryType},
            html_type = #{htmlType},
            dict_type = #{dictType},
            sort = #{sort},
            update_by = #{updateBy},
            update_time = sysdate()
        </set>
        where column_id = #{columnId}
    </update>

    <delete id="deleteGenTableColumnByIds" parameterType="Long">
        delete from gen_table_column where table_id in
        <foreach collection="array" item="tableId" open="(" separator="," close=")">
            #{tableId}
        </foreach>
    </delete>

    <delete id="deleteGenTableColumns">
        delete from gen_table_column where column_id in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item.columnId}
        </foreach>
    </delete>

</mapper>

将这两个xml替换,基本就能使用了,但是还是会有部分表注释,列注释丢失,但并不影响正常使用。

具体原理

pgsql 常用查询汇总(查询数据表字段)_pgsql 查询表字段-CSDN博客

对照文章,去同逻辑替换相应的SQL就可以了

(请多多支持,点赞收藏o(* ̄▽ ̄*)ブ)

(有疑问也可以留言咨询或私信噢,也会多多接受大家建议,改进文章质量)

  • 26
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值