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

已知使用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 天前
10342_基于Springboot的云存管家平台的设计与实现
mysql·毕业设计·springboot·文件管理·云存储·云存管家
淡淡的说非2 天前
LangChain4j 深度解析与Java工程化落地实践
ai·llm·springboot·langchain4j
千寻技术帮2 天前
10343_基于Springboot的考研信息查询系统
mysql·vue·springboot·考研论坛·考研录取
shejizuopin3 天前
基于JavaSSM+MySQL的实验室考勤管理系统设计与实现
java·mysql·vue·毕业设计·论文·springboot·实验室考勤管理系统设计与实现
千寻技术帮4 天前
10349_基于Springboot的万仙山旅游管理系统
mysql·springboot·旅游管理·在线旅游
jasnet_u4 天前
SpringCloudAlibaba的web微服务快速搭建
java·springboot·springlcoud
susu10830189114 天前
springboot3.5.8整合minio8.5.9
java·springboot
悟空码字4 天前
SpringBoot整合Zookeeper,实现分布式集群部署
java·zookeeper·springboot·编程技术·后端开发