若依笔记(四):代码生成器

已知使用MyBatisPlus代码生成器可以自动生成Entity、Mapper、Service、Controller代码,前提是数据库中有数据表,生成pojo类以及对于该数据表的增删改查命令的代码,若依更进一步能选择表后生成代码、预览、下载,同时可以生产前端代码,与后端一起还能实现权限管理和数据隔离。

入口是有路由权限的前端index.vue,点击后调用api.js后发起后端请求,后端过controller-->service-->maapper查询请求;

核心就是generatorCode方法:

java 复制代码
    @Override
    public void generatorCode(String tableName)
    {
        // 查询表信息
        GenTable table = genTableMapper.selectGenTableByName(tableName);
        // 设置主子表信息
        setSubTable(table);
        // 设置主键列信息
        setPkColumn(table);

        VelocityInitializer.initVelocity();

        VelocityContext context = VelocityUtils.prepareContext(table);

        // 获取模板列表
        List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory());
        for (String template : templates)
        {
            if (!StringUtils.containsAny(template, "sql.vm", "api.js.vm", "index.vue.vm", "index-tree.vue.vm"))
            {
                // 渲染模板
                StringWriter sw = new StringWriter();
                Template tpl = Velocity.getTemplate(template, Constants.UTF8);
                tpl.merge(context, sw);
                try
                {
                    String path = getGenPath(table, template);
                    FileUtils.writeStringToFile(new File(path), sw.toString(), CharsetKit.UTF_8);
                }
                catch (IOException e)
                {
                    throw new ServiceException("渲染模板失败,表名:" + table.getTableName());
                }
            }
        }
    }

待续

相关推荐
@yanyu6669 小时前
第一个前后端分离项目
java·vue.js·springboot
惊讶的猫11 小时前
SpringMVC介绍
java·springmvc·springboot
智能工业品检测-奇妙智能1 天前
springboot对接阿里云短信
人工智能·vue·springboot·阿里云短信
懈尘2 天前
【实战分享】智慧养老系统核心模块设计 —— 健康监测与自动紧急呼叫
java·后端·websocket·mysql·springboot·livekit
JustMove0n2 天前
互联网大厂Java面试全流程问答及技术详解
java·jvm·redis·mybatis·dubbo·springboot·多线程
Javatutouhouduan2 天前
SpringBoot整合reids:JSON序列化文件夹操作实录
java·数据库·redis·html·springboot·java编程·java程序员
智能工业品检测-奇妙智能3 天前
SpringBoot整合FFmpeg的方法
人工智能·ffmpeg·springboot·deepseek·openclaw
Luke Ewin3 天前
ASR数据集采集系统 | ASR方言数据集采集系统 | ASR方言数据集采集系统 | 语音识别数据集采集系统
vue·springboot·语音识别·asr·asr数据集采集·asr方言数据集采集
孔明click336 天前
Sa-Token v1.45.0 发布 🚀,正式支持 Spring Boot 4、新增 Jackson3/Snack4 插件适配
java·sa-token·开源·springboot·登录·权限认证