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

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

待续

相关推荐
大学生资源网3 小时前
java毕业设计之面向校园的助力跑腿系统设计与实现源码(源码+文档+数据库)
java·数据库·mysql·毕业设计·源码·springboot
原来是好奇心5 小时前
深入Spring Boot源码(五):外部化配置与Profile机制深度解析
java·源码·springboot
沧澜sincerely10 小时前
WebSocket 实时聊天功能
网络·websocket·vue·springboot
后端小张13 小时前
【JAVA 进阶】深入拆解SpringBoot自动配置:从原理到实战的完整指南
java·开发语言·spring boot·后端·spring·spring cloud·springboot
原来是好奇心1 天前
深入Spring Boot源码(四):Starter机制与依赖管理深度解析
java·源码·springboot·starter
原来是好奇心2 天前
深入Spring Boot源码(三):自动配置之Spring Boot的“魔法“核心
java·自动配置·源码·springboot
她说..2 天前
Spring AOP场景5——异常处理(附带源码)
java·数据库·后端·spring·springboot·spring aop
期待のcode2 天前
MyBatisX插件
java·数据库·后端·mybatis·springboot
码界奇点2 天前
基于SpringBoot和Vue的Fuint门店会员营销系统设计与实现
vue.js·spring boot·后端·毕业设计·springboot·源代码管理