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

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

待续

相关推荐
爱敲代码的TOM20 小时前
OAuth2协议与微信扫码登录实战
springboot·java后端·微信扫码登录
星光一影1 天前
智慧停车与充电一体化管理平台:打造城市出行新生态
mysql·vue·能源·springboot·uniapp
Coder_Boy_2 天前
基于SpringAI的智能平台基座开发-(三)
人工智能·springboot·aiops·langchain4j
Coder_Boy_2 天前
基于SpringAI的智能平台基座开发-(七)
人工智能·springboot·aiops·langchain4j
Coder_Boy_3 天前
SpringAI与LangChain4j的智能应用-(实践篇2)
人工智能·springboot·aiops·langchain4j
大学生资源网3 天前
基于Javaweb技术的宠物用品商城的设计与实现(源码+文档)
java·mysql·毕业设计·源码·springboot
九月生3 天前
基于 Sa-Token 实现 API 接口签名校验(Spring Boot 3 实战)
web安全·springboot
带刺的坐椅4 天前
超越 SpringBoot 4.0了吗?OpenSolon v3.8, v3.7.4, v3.6.7 发布
java·ai·springboot·web·solon·flow·mcp
hgz07104 天前
Spring Boot自动配置
java·springboot
TimberWill5 天前
MinIO整合SpringBoot实现获取文件夹目录结构及文件内容
java·linux·springboot