From 31cb429756539464c7804ab45b9cd1c2ef311b36 Mon Sep 17 00:00:00 2001 From: zch Date: Mon, 28 Apr 2025 13:30:30 +0800 Subject: [PATCH] =?UTF-8?q?refactor(os-generator):=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E7=94=9F=E6=88=90=E5=99=A8=E7=9A=84=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E6=9F=A5=E8=AF=A2=E5=92=8C=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=EF=BC=8C=E6=94=B9SQL=20server=E4=B8=BAMySQL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 使用 information_schema 替代 sysobjects 等系统表,提高兼容性 - 更新了 sql.vm 文件中的 SQL 语句.1. 删除了 DECLARE @parentId INT; 语句,因为 MySQL 中不需要预先声明变量2. 将 SET @parentId = SCOPE_IDENTITY(); 修改为 SET @parentId = LAST_INSERT_ID(); ,使用 MySQL 兼容的函数获取最后插入的自增 ID。注意:1. 在 MySQL/TiDB 中,用户变量以 @ 开头,不需要像 SQL Server 那样预先声明2. LAST_INSERT_ID() 函数用于获取最后插入的自增 ID,替代 SQL Server 中的 SCOPE_IDENTITY() --- .../mapper/generator/GenTableColumnMapper.xml | 124 +++----- .../mapper/generator/GenTableMapper.xml | 280 ++++++++---------- os-generator/src/main/resources/vm/sql/sql.vm | 3 +- 3 files changed, 177 insertions(+), 230 deletions(-) diff --git a/os-generator/src/main/resources/mapper/generator/GenTableColumnMapper.xml b/os-generator/src/main/resources/mapper/generator/GenTableColumnMapper.xml index 3706536..1e97f36 100644 --- a/os-generator/src/main/resources/mapper/generator/GenTableColumnMapper.xml +++ b/os-generator/src/main/resources/mapper/generator/GenTableColumnMapper.xml @@ -29,7 +29,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" - + 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 @@ -40,98 +40,72 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" + select column_name, (case when (is_nullable = 'no' column_key != 'PRI') then '1' else '0' 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 + insert into gen_table_column ( - 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 - )values( - #{tableId}, - #{columnName}, - #{columnComment}, - #{columnType}, - #{javaType}, - #{javaField}, - #{isPk}, - #{isIncrement}, - #{isRequired}, - #{isInsert}, - #{isEdit}, - #{isList}, - #{isQuery}, - #{queryType}, - #{htmlType}, - #{dictType}, - #{sort}, - #{createBy}, - now() - ) + 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 + )values( + #{tableId}, + #{columnName}, + #{columnComment}, + #{columnType}, + #{javaType}, + #{javaField}, + #{isPk}, + #{isIncrement}, + #{isRequired}, + #{isInsert}, + #{isEdit}, + #{isList}, + #{isQuery}, + #{queryType}, + #{htmlType}, + #{dictType}, + #{sort}, + #{createBy}, + sysdate() + ) update gen_table_column + column_comment = #{columnComment}, + java_type = #{javaType}, + java_field = #{javaField}, is_insert = #{isInsert}, is_edit = #{isEdit}, is_list = #{isList}, is_query = #{isQuery}, is_required = #{isRequired}, - column_comment = #{columnComment}, - java_type = #{javaType}, - java_field = #{javaField}, query_type = #{queryType}, html_type = #{htmlType}, dict_type = #{dictType}, sort = #{sort}, update_by = #{updateBy}, - update_time = now() + update_time = sysdate() where column_id = #{columnId} diff --git a/os-generator/src/main/resources/mapper/generator/GenTableMapper.xml b/os-generator/src/main/resources/mapper/generator/GenTableMapper.xml index 6b81f9d..803f3b6 100644 --- a/os-generator/src/main/resources/mapper/generator/GenTableMapper.xml +++ b/os-generator/src/main/resources/mapper/generator/GenTableMapper.xml @@ -5,7 +5,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" - + @@ -26,39 +26,39 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" - + - + - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + - select table_id, table_name, table_comment, sub_table_name, sub_table_fk_name, class_name, tpl_category, tpl_web_type, 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 - - - @@ -68,169 +68,143 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" AND lower(table_comment) like lower(concat('%', #{tableComment}, '%')) - - and datediff(d, create_time, #{params.beginTime}) 0 + AND date_format(create_time,'%Y%m%d') >= date_format(#{params.beginTime},'%Y%m%d') - - and datediff(d, create_time, #{params.endTime}) =]]> 0 + AND date_format(create_time,'%Y%m%d') <= date_format(#{params.endTime},'%Y%m%d') - order by create_time desc - + - + - + - + - - - insert into gen_table ( - table_name, - table_comment, - class_name, - tpl_category, - tpl_web_type, - package_name, - module_name, - business_name, - function_name, - function_author, - gen_type, - gen_path, - remark, - create_by, - create_time - )values( - #{tableName}, - #{tableComment}, - #{className}, - #{tplCategory}, - #{tplWebType}, - #{packageName}, - #{moduleName}, - #{businessName}, - #{functionName}, - #{functionAuthor}, - #{genType}, - #{genPath}, - #{remark}, - #{createBy}, - now() - ) - - - - ${sql} - - - update gen_table - - table_name = #{tableName}, - table_comment = #{tableComment}, - sub_table_name = #{subTableName}, - sub_table_fk_name = #{subTableFkName}, - class_name = #{className}, - function_author = #{functionAuthor}, - gen_type = #{genType}, - gen_path = #{genPath}, - tpl_category = #{tplCategory}, - tpl_web_type = #{tplWebType}, - package_name = #{packageName}, - module_name = #{moduleName}, - business_name = #{businessName}, - function_name = #{functionName}, - options = #{options}, - update_by = #{updateBy}, - remark = #{remark}, - update_time = now() - - where table_id = #{tableId} - - - - delete from gen_table where table_id in - - #{tableId} - - + + insert into gen_table ( + table_name, + table_comment, + class_name, + tpl_category, + tpl_web_type, + package_name, + module_name, + business_name, + function_name, + function_author, + gen_type, + gen_path, + remark, + create_by, + create_time + )values( + #{tableName}, + #{tableComment}, + #{className}, + #{tplCategory}, + #{tplWebType}, + #{packageName}, + #{moduleName}, + #{businessName}, + #{functionName}, + #{functionAuthor}, + #{genType}, + #{genPath}, + #{remark}, + #{createBy}, + sysdate() + ) + + + + ${sql} + + + + update gen_table + + table_name = #{tableName}, + table_comment = #{tableComment}, + sub_table_name = #{subTableName}, + sub_table_fk_name = #{subTableFkName}, + class_name = #{className}, + function_author = #{functionAuthor}, + gen_type = #{genType}, + gen_path = #{genPath}, + tpl_category = #{tplCategory}, + tpl_web_type = #{tplWebType}, + package_name = #{packageName}, + module_name = #{moduleName}, + business_name = #{businessName}, + function_name = #{functionName}, + options = #{options}, + update_by = #{updateBy}, + remark = #{remark}, + update_time = sysdate() + + where table_id = #{tableId} + + + + delete from gen_table where table_id in + + #{tableId} + + \ No newline at end of file diff --git a/os-generator/src/main/resources/vm/sql/sql.vm b/os-generator/src/main/resources/vm/sql/sql.vm index 8921ca5..ee4f1f7 100644 --- a/os-generator/src/main/resources/vm/sql/sql.vm +++ b/os-generator/src/main/resources/vm/sql/sql.vm @@ -3,8 +3,7 @@ insert into sys_menu (menu_name, parent_id, order_num, path, component, is_frame values('${functionName}', '${parentMenuId}', '1', '${businessName}', '${moduleName}/${businessName}/index', 1, 0, 'C', '0', '0', '${permissionPrefix}:list', '#', 'admin', now(), '', null, '${functionName}菜单'); -- 按钮父菜单ID -DECLARE @parentId INT; -SET @parentId = SCOPE_IDENTITY(); +SET @parentId = LAST_INSERT_ID(); -- 按钮 SQL insert into sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)