refactor(os-generator): 重构代码生成器的数据库查询和操作,改SQL server为MySQL

- 使用 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()
boardTest
zch 2 months ago
parent a0eba36531
commit 31cb429756

@ -40,35 +40,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</select> </select>
<select id="selectDbTableColumnsByName" parameterType="String" resultMap="GenTableColumnResult"> <select id="selectDbTableColumnsByName" parameterType="String" resultMap="GenTableColumnResult">
SELECT a.name AS column_name, select column_name, (case when (is_nullable = 'no' <![CDATA[ && ]]> 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
(CASE WHEN a.ifnullable = 1 THEN 0 ELSE 1 END) AS is_required, from information_schema.columns where table_schema = (select database()) and table_name = (#{tableName})
(CASE WHEN ( order by ordinal_position
SELECT COUNT(*) FROM sysobjects
WHERE (name IN (
SELECT name FROM sysindexes
WHERE (id = a.id)
AND (indid IN (
SELECT indid FROM sysindexkeys
WHERE (id = a.id)
AND (colid IN (SELECT colid FROM syscolumns WHERE (id = a.id) AND (name = a.name)))
))))
AND (xtype = 'PK')
) > 0 THEN 1
ELSE 0 END) AS is_pk,
a.colorder AS sort,
ifnull(g.[value], ' ') AS column_comment,
(CASE WHEN COLUMNPROPERTY(a.id, a.name, 'IsIdentity') = 1 THEN 1 ELSE 0 END) AS is_increment,
b.name AS column_type
FROM syscolumns as a
LEFT JOIN systypes b ON a.xtype = b.xusertype
INNER JOIN sysobjects d ON a.id = d.id AND d.xtype = 'U' AND d.name <![CDATA[<>]]> 'dtproperties'
LEFT JOIN syscomments e ON a.cdefault = e.id
LEFT JOIN sys.extended_properties g ON a.id = g.major_id AND a.colid = g.minor_id
LEFT JOIN sys.extended_properties f ON d.id = f.class AND f.minor_id = 0
LEFT JOIN sys.objects h ON a.id = h.object_id
LEFT JOIN sys.schemas i ON h.schema_id = i.schema_id
WHERE d.name = #{tableName}
ORDER BY a.colorder
</select> </select>
<insert id="insertGenTableColumn" parameterType="GenTableColumn" useGeneratedKeys="true" keyProperty="columnId"> <insert id="insertGenTableColumn" parameterType="GenTableColumn" useGeneratedKeys="true" keyProperty="columnId">
@ -111,27 +85,27 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="dictType != null and dictType != ''">#{dictType},</if> <if test="dictType != null and dictType != ''">#{dictType},</if>
<if test="sort != null">#{sort},</if> <if test="sort != null">#{sort},</if>
<if test="createBy != null and createBy != ''">#{createBy},</if> <if test="createBy != null and createBy != ''">#{createBy},</if>
now() sysdate()
) )
</insert> </insert>
<update id="updateGenTableColumn" parameterType="GenTableColumn"> <update id="updateGenTableColumn" parameterType="GenTableColumn">
update gen_table_column update gen_table_column
<set> <set>
<if test="columnComment != null">column_comment = #{columnComment},</if>
<if test="javaType != null">java_type = #{javaType},</if>
<if test="javaField != null">java_field = #{javaField},</if>
<if test="isInsert != null">is_insert = #{isInsert},</if> <if test="isInsert != null">is_insert = #{isInsert},</if>
<if test="isEdit != null">is_edit = #{isEdit},</if> <if test="isEdit != null">is_edit = #{isEdit},</if>
<if test="isList != null">is_list = #{isList},</if> <if test="isList != null">is_list = #{isList},</if>
<if test="isQuery != null">is_query = #{isQuery},</if> <if test="isQuery != null">is_query = #{isQuery},</if>
<if test="isRequired != null">is_required = #{isRequired},</if> <if test="isRequired != null">is_required = #{isRequired},</if>
<if test="columnComment != null">column_comment = #{columnComment},</if>
<if test="javaType != null">java_type = #{javaType},</if>
<if test="javaField != null">java_field = #{javaField},</if>
<if test="queryType != null">query_type = #{queryType},</if> <if test="queryType != null">query_type = #{queryType},</if>
<if test="htmlType != null">html_type = #{htmlType},</if> <if test="htmlType != null">html_type = #{htmlType},</if>
<if test="dictType != null">dict_type = #{dictType},</if> <if test="dictType != null">dict_type = #{dictType},</if>
<if test="sort != null">sort = #{sort},</if> <if test="sort != null">sort = #{sort},</if>
<if test="updateBy != null">update_by = #{updateBy},</if> <if test="updateBy != null">update_by = #{updateBy},</if>
update_time = now() update_time = sysdate()
</set> </set>
where column_id = #{columnId} where column_id = #{columnId}
</update> </update>

@ -68,64 +68,38 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
AND lower(table_comment) like lower(concat('%', #{tableComment}, '%')) AND lower(table_comment) like lower(concat('%', #{tableComment}, '%'))
</if> </if>
<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 --> <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
<!-- and <![CDATA[ create_time >= convert(datetime, #{params.beginTime}, 20)]]>--> AND date_format(create_time,'%Y%m%d') &gt;= date_format(#{params.beginTime},'%Y%m%d')
and datediff(d, create_time, #{params.beginTime}) <![CDATA[<=]]> 0
</if> </if>
<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 --> <if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
<!-- and <![CDATA[ create_time <= convert(datetime, #{params.endTime}, 20)]]>--> AND date_format(create_time,'%Y%m%d') &lt;= date_format(#{params.endTime},'%Y%m%d')
and datediff(d, create_time, #{params.endTime}) <![CDATA[>=]]> 0
</if> </if>
</where> </where>
order by create_time desc
</select> </select>
<select id="selectDbTableList" parameterType="GenTable" resultMap="GenTableResult"> <select id="selectDbTableList" parameterType="GenTable" resultMap="GenTableResult">
SELECT select table_name, table_comment, create_time, update_time from information_schema.tables
so.name as table_name, where table_schema = (select database())
sep.value as table_comment, AND table_name NOT LIKE 'qrtz\_%' AND table_name NOT LIKE 'gen\_%'
so.create_date as create_time, AND table_name NOT IN (select table_name from gen_table)
so.modify_date as update_time
FROM
sys.objects as so
LEFT JOIN sys.extended_properties as sep on so.object_id = sep.major_id
WHERE
so.type = 'U'
AND sep.minor_id = 0
AND so.name NOT LIKE 'qrtz_%' AND so.name NOT LIKE 'gen_%'
AND so.name NOT LIKE 'act_%' AND so.name NOT LIKE 'flw_%'
AND so.name NOT IN (select table_name from gen_table)
<if test="tableName != null and tableName != ''"> <if test="tableName != null and tableName != ''">
AND lower(so.name) like lower(concat('%', #{tableName}, '%')) AND lower(table_name) like lower(concat('%', #{tableName}, '%'))
</if> </if>
<if test="tableComment != null and tableComment != ''"> <if test="tableComment != null and tableComment != ''">
AND lower(cast(sep.value as nvarchar)) like lower(concat('%', #{tableComment}, '%')) AND lower(table_comment) like lower(concat('%', #{tableComment}, '%'))
</if> </if>
<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 --> <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
<!-- and <![CDATA[ create_time >= convert(datetime, #{params.beginTime}, 20)]]>--> AND date_format(create_time,'%Y%m%d') &gt;= date_format(#{params.beginTime},'%Y%m%d')
and datediff(d, so.create_date, #{params.beginTime}) <![CDATA[<=]]> 0
</if> </if>
<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 --> <if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
<!-- and <![CDATA[ create_time <= convert(datetime, #{params.endTime}, 20)]]>--> AND date_format(create_time,'%Y%m%d') &lt;= date_format(#{params.endTime},'%Y%m%d')
and datediff(d, so.create_date, #{params.endTime}) <![CDATA[>=]]> 0
</if> </if>
order by so.create_date desc order by create_time desc
</select> </select>
<select id="selectDbTableListByNames" resultMap="GenTableResult"> <select id="selectDbTableListByNames" resultMap="GenTableResult">
SELECT select table_name, table_comment, create_time, update_time from information_schema.tables
SO.name table_name, where table_name NOT LIKE 'qrtz\_%' and table_name NOT LIKE 'gen\_%' and table_schema = (select database())
SEP.VALUE table_comment, and table_name in
SO.create_date create_time,
SO.modify_date update_time
FROM
sys.objects AS SO
LEFT JOIN sys.extended_properties AS SEP ON SO.object_id = SEP.major_id
WHERE
SO.type = 'U'
AND SEP.minor_id = 0
and SO.name NOT LIKE 'qrtz_%' and SO.name NOT LIKE 'gen_%'
AND so.name NOT LIKE 'act_%' AND so.name NOT LIKE 'flw_%'
and SO.name in
<foreach collection="array" item="name" open="(" separator="," close=")"> <foreach collection="array" item="name" open="(" separator="," close=")">
#{name} #{name}
</foreach> </foreach>
@ -193,7 +167,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="genPath != null and genPath != ''">#{genPath},</if> <if test="genPath != null and genPath != ''">#{genPath},</if>
<if test="remark != null and remark != ''">#{remark},</if> <if test="remark != null and remark != ''">#{remark},</if>
<if test="createBy != null and createBy != ''">#{createBy},</if> <if test="createBy != null and createBy != ''">#{createBy},</if>
now() sysdate()
) )
</insert> </insert>
@ -221,7 +195,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="options != null and options != ''">options = #{options},</if> <if test="options != null and options != ''">options = #{options},</if>
<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if> <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
<if test="remark != null">remark = #{remark},</if> <if test="remark != null">remark = #{remark},</if>
update_time = now() update_time = sysdate()
</set> </set>
where table_id = #{tableId} where table_id = #{tableId}
</update> </update>

@ -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}菜单'); values('${functionName}', '${parentMenuId}', '1', '${businessName}', '${moduleName}/${businessName}/index', 1, 0, 'C', '0', '0', '${permissionPrefix}:list', '#', 'admin', now(), '', null, '${functionName}菜单');
-- 按钮父菜单ID -- 按钮父菜单ID
DECLARE @parentId INT; SET @parentId = LAST_INSERT_ID();
SET @parentId = SCOPE_IDENTITY();
-- 按钮 SQL -- 按钮 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) 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)

Loading…
Cancel
Save