-- ======================================== -- 用户自定义模板功能数据库脚本 -- 创建时间: 2025-09-26 -- 描述: 支持用户创建私有/公开模板,模板仓库管理功能 -- ======================================== -- 1. 用户模板表 DROP TABLE IF EXISTS `user_template`; CREATE TABLE `user_template` ( `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID', `user_id` bigint NOT NULL COMMENT '用户ID', `template_name` varchar(100) NOT NULL COMMENT '模板名称', `template_group` varchar(50) NOT NULL COMMENT '模板分组', `template_content` longtext NOT NULL COMMENT '模板内容(FreeMarker模板)', `description` varchar(500) DEFAULT NULL COMMENT '模板描述', `is_public` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否公开(0-私有 1-公开)', `version` varchar(20) NOT NULL DEFAULT '1.0.0' COMMENT '模板版本', `use_count` int NOT NULL DEFAULT '0' COMMENT '使用次数统计', `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '状态(0-草稿 1-发布 2-禁用)', `create_by` bigint DEFAULT NULL COMMENT '创建人ID', `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `update_by` bigint DEFAULT NULL COMMENT '更新人ID', `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', `deleted` tinyint(1) NOT NULL DEFAULT '0' COMMENT '逻辑删除(0-未删除 1-已删除)', PRIMARY KEY (`id`), UNIQUE KEY `uk_user_template_name` (`user_id`, `template_name`), KEY `idx_user_id` (`user_id`), KEY `idx_template_group` (`template_group`), KEY `idx_is_public` (`is_public`), KEY `idx_status` (`status`), KEY `idx_create_time` (`create_time`), KEY `idx_deleted` (`deleted`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户自定义模板表'; -- 2. 用户模板仓库配置表 DROP TABLE IF EXISTS `user_template_repository`; CREATE TABLE `user_template_repository` ( `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID', `user_id` bigint NOT NULL COMMENT '用户ID', `template_source` varchar(20) NOT NULL COMMENT '模板来源(system-系统模板 user-用户模板)', `template_id` varchar(50) NOT NULL COMMENT '模板ID(系统模板用template.json中的id,用户模板用user_template.id)', `template_name` varchar(100) NOT NULL COMMENT '模板名称(冗余字段便于查询)', `template_group` varchar(50) NOT NULL COMMENT '模板分组(冗余字段便于查询)', `is_enabled` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否启用显示(0-隐藏 1-显示)', `sort_order` int NOT NULL DEFAULT '0' COMMENT '显示排序(数字越小越靠前)', `create_by` bigint DEFAULT NULL COMMENT '创建人ID', `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `update_by` bigint DEFAULT NULL COMMENT '更新人ID', `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', `deleted` tinyint(1) NOT NULL DEFAULT '0' COMMENT '逻辑删除(0-未删除 1-已删除)', PRIMARY KEY (`id`), UNIQUE KEY `uk_user_template` (`user_id`, `template_source`, `template_id`), KEY `idx_user_id` (`user_id`), KEY `idx_template_source` (`template_source`), KEY `idx_is_enabled` (`is_enabled`), KEY `idx_sort_order` (`sort_order`), KEY `idx_deleted` (`deleted`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户模板仓库配置表'; -- 3. 模板使用日志表 DROP TABLE IF EXISTS `template_usage_log`; CREATE TABLE `template_usage_log` ( `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID', `user_id` bigint NOT NULL COMMENT '使用者ID', `template_source` varchar(20) NOT NULL COMMENT '模板来源(system-系统模板 user-用户模板)', `template_id` varchar(50) NOT NULL COMMENT '模板ID', `template_name` varchar(100) NOT NULL COMMENT '模板名称', `template_group` varchar(50) NOT NULL COMMENT '模板分组', `ip_address` varchar(50) DEFAULT NULL COMMENT 'IP地址', `create_by` bigint DEFAULT NULL COMMENT '创建人ID', `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间(使用时间)', `update_by` bigint DEFAULT NULL COMMENT '更新人ID', `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', `deleted` tinyint(1) NOT NULL DEFAULT '0' COMMENT '逻辑删除(0-未删除 1-已删除)', PRIMARY KEY (`id`), KEY `idx_user_id` (`user_id`), KEY `idx_template_source` (`template_source`), KEY `idx_template_id` (`template_id`), KEY `idx_create_time` (`create_time`), KEY `idx_deleted` (`deleted`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='模板使用日志表'; -- ======================================== -- 初始化数据 -- ======================================== -- 为管理员用户(假设user_id=1)初始化系统默认模板到仓库 -- 这些数据对应template.json中的模板配置 INSERT INTO `user_template_repository` (`user_id`, `template_source`, `template_id`, `template_name`, `template_group`, `is_enabled`, `sort_order`) VALUES (1, 'system', '10', 'swagger-ui', 'ui', 1, 10), (1, 'system', '20', 'controller', 'mybatis', 1, 20), (1, 'system', '21', 'service', 'mybatis', 1, 21), (1, 'system', '22', 'service_impl', 'mybatis', 1, 22), (1, 'system', '23', 'mapper', 'mybatis', 1, 23), (1, 'system', '24', 'mybatis', 'mybatis', 1, 24), (1, 'system', '25', 'model', 'mybatis', 1, 25), (1, 'system', '26', 'mapper2', 'mybatis', 1, 26), (1, 'system', '60', 'pluscontroller', 'mybatis-plus', 1, 60), (1, 'system', '61', 'plusservice', 'mybatis-plus', 1, 61), (1, 'system', '62', 'plusmapper', 'mybatis-plus', 1, 62), (1, 'system', '63', 'plusentity', 'mybatis-plus', 1, 63); -- ======================================== -- 示例数据(可选) -- ======================================== -- 创建一个示例用户模板 INSERT INTO `user_template` ( `user_id`, `template_name`, `template_group`, `template_content`, `description`, `is_public`, `version`, `status`, `create_by` ) VALUES ( 1, 'custom-controller', 'custom', '/**\n * ${classInfo.classComment}\n * \n * @author ${classInfo.author}\n * @date ${classInfo.createDate}\n */\n@RestController\n@RequestMapping("/${classInfo.classNameLower}")\npublic class ${classInfo.className}Controller {\n // 自定义控制器模板\n}', '自定义控制器模板示例', 1, '1.0.0', 1, 1 ); -- 将示例模板添加到仓库 INSERT INTO `user_template_repository` (`user_id`, `template_source`, `template_id`, `template_name`, `template_group`, `is_enabled`, `sort_order`) SELECT 1, 'user', id, template_name, template_group, 1, 100 FROM `user_template` WHERE `template_name` = 'custom-controller';