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

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

待续

相关推荐
云边有个稻草人9 小时前
飞算JavaAI能处理SaaS套餐变更业务吗?
java·springboot·ai编程·java开发·飞算javaai·java代码生成·ai coding模型
todoitbo11 小时前
飞算JavaAI的多租户权限隔离实测
java·springboot·ai编程·java开发·飞算javaai·java代码生成
闲猫15 小时前
Docker本地用Dockfile 构建Java后端SprintBoot镜像
docker·springboot·dockerfile
Kings992 天前
AI 请求参数智能生成与一键调试方法支持,Fast Request重磅发布
ai·springboot·idea插件
小小啊python4 天前
IDEA中SpringBoot项目配置热部署
java·ide·intellij-idea·springboot·热部署
glustful5 天前
企业内部审计项目管理 系统开源(springboot+vue3)
vue3·springboot
还是鼠鼠11 天前
Spring AI ChatClient详解:创建第一个AI客户端
java·springboot·spring ai·chatclient
西凉的悲伤11 天前
Java 基于 MySQL 行锁实现高并发业务序列号生成器
java·mysql·springboot·序列号生成
YDS82911 天前
大营销平台 —— 细节优化和前端对接
java·springboot·ddd
刘名喜13 天前
第22篇-数据库迁移-Liquibase
kotlin·springboot