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

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

待续

相关推荐
折哥的程序人生 · 物流技术专研6 小时前
Java面试通关⑨:SpringBoot核心全集
自动配置·springboot·starter·校招·java面试·面试真题·内嵌容器
ShiXZ21320 小时前
PDF-OCR文件识别篇(八):配置、运维与排错
java·运维·ocr·dubbo·springboot
ShiXZ21321 小时前
PDF-OCR文件识别篇(七):数据入库
java·pdf·json·ocr·springboot
带刺的坐椅8 天前
Spring Boot → Solon 注解迁移实战指南:一张对照表说清楚
java·springboot·web·solon
十五喵源码网18 天前
基于springboot2+vue2的租房管理系统
java·毕业设计·springboot·论文笔记
whaledown18 天前
Kafka 与 Java 消息队列入门:用订单场景理解核心机制
java·kafka·消息队列·springboot
二哈赛车手19 天前
新人笔记---最终版智能体图片分析完整方案,包括一些总结于经验,以及各种优化点讲解
java·笔记·spring·ai·springboot
十五喵源码网19 天前
基于SpringBoot2+vue2的酒店客房管理系统
java·毕业设计·springboot·论文笔记
这里是杨杨吖20 天前
SpringBoot+Vue高校在线考试系统 附带详细运行指导视频
vue·在线考试·springboot
就改了20 天前
ElasticsearchRestTemplate使用方法详解!!!
java·elasticsearch·springboot