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

已知使用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());
                }
            }
        }
    }

待续

相关推荐
慧都小项12 小时前
MyEclipse 2026:当 Agent、MCP 与 Java 26 进入 Eclipse 工作流
java·eclipse·springboot·copilot·myeclipse
冬栈程序设计1 天前
Springboot宿舍管理系统【670099】 -附源码(开箱即用)
java·毕业设计·springboot·课程设计·程序设计·大作业
苏生Susheng2 天前
【软件实施】Linux企业运维常用命令手册
java·linux·运维·服务器·springboot·springcloud·软件实施
武哥聊编程3 天前
【AI实战项目】基于LangChain4j+Springboot+Vue+RAG+PGVector的AI智能客服与工单处理系统
springboot·rag·pgvector·langchain4j
小宇宙清歡渡3 天前
Java Stream 流全面实战指南(Spring Boot 3 + JDK 17)
java·springboot·stream流
小宇宙清歡渡5 天前
SpringBoot JDK17 线程池的使用
springboot·线程池·jdk17
十五喵源码网7 天前
基于springboot2+vue2的疾病防控综合系统
java·毕业设计·springboot·论文笔记
玉&心7 天前
GraalVM 21 Native Image 编译踩坑全记录:从 class 初始化冲突到成功构建
ai·springboot·graalvm·rag
十五喵源码网7 天前
基于SpringBoot2+vue2的健身房管理系统
java·毕业设计·springboot·论文笔记
記億揺晃着的那天8 天前
从 0 到 2,000+ 次提交:ERP 四年的架构演进
架构·springboot·erp