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

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

待续

相关推荐
她说..2 天前
Spring AOP场景3——接口防抖(附带源码)
java·后端·spring·java-ee·springboot
原来是好奇心2 天前
深入Spring Boot源码(二):启动过程深度剖析
java·源码·springboot
原来是好奇心2 天前
深入Spring Boot源码(一):环境搭建与初探项目架构
java·gradle·源码·springboot
宠友信息2 天前
打造可持续增长的垂直社区:仿小红书平台功能体系与架构深度解析
java·微服务·微信小程序·springboot·uniapp
后端小张2 天前
【JAVA进阶】Docker 2025完全指南:从容器入门到企业级实践
java·运维·开发语言·spring·docker·容器·springboot
zwjapple3 天前
Spring Boot 处理带文件表单的几种方式
springboot·form·表单
深紫色的三北六号3 天前
大疆不同任务类型执行逻辑,上云API源码分析
java·无人机·springboot·大疆·上云api
期待のcode3 天前
Thymeleaf模板引擎
java·html·springboot
期待のcode3 天前
MyBatis-Plus通用枚举
java·数据库·后端·mybatis·springboot
武哥聊编程3 天前
【从0带做】基于Springboot3+Vue3的生态养殖管理系统
java·学习·vue·毕业设计·springboot