栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

mybatis-plus代码生成

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力
mybatis-plus代码生成

文章目录
  • mybatis-plus代码生成
  • 前言
  • 一、mybatis-plus是什么?
  • 二、使用步骤
    • 1.引入依赖
    • 2.代码
  • 总结


前言

AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。


一、mybatis-plus是什么?

MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

二、使用步骤 1.引入依赖

代码如下(示例):

       
            com.baomidou
            mybatis-plus-boot-starter
            3.0.5
        

        
           org.freemarker
           freemarker
           2.3.31
       
2.代码

代码如下(示例):

 // 代码生成器
        AutoGenerator mpg = new AutoGenerator();
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        // 策略配置
        StrategyConfig strategy = new StrategyConfig();

        
        // 作者
        gc.setAuthor("lzy");
        // 数据库密码
        dsc.setPassword("xxxx");
        // 数据库url
        dsc.setUrl("jdbc:mysql://xxxxx:3306/xxxxx?useUnicode=true&useSSL=false&characterEncoding=utf8");
        // 需要创建的表名,什么都不填生成所有
        strategy.setInclude("admin");
        



        // 全局配置
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setBaseResultMap(true);
        gc.setOpen(false);
        gc.setDateType(DateType.ONLY_DATE);
        mpg.setGlobalConfig(gc);

        // 数据源配置
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("");
        pc.setParent("com.xx.xx");
        pc.setMapper("mapper");
        mpg.setPackageInfo(pc);

        // 策略配置
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix(pc.getModuleName() + "_");
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };
        List focList = new ArrayList<>();
        focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return projectPath + "/src/main/java/com/xxx/xxx/mapper/" + pc.getModuleName()
                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

        // 自定义模板配置
        TemplateConfig tc = new TemplateConfig();
        tc.setServiceImpl("/templates/serviceImpl.java");
        tc.setXml(null);
        mpg.setTemplate(tc);

        mpg.execute();

模板:




    
    
    MyBatis-Plus代码生成器
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    




MyBatis-Plus代码生成器

*项目
*数据库配置
可选配置

表配置

包名配置

文件名格式

字段名配置

实体类配置

XML配置

其他配置


总结

根据自己需求,更改配置即可。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/919279.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号