Mybatis-plus 系列之代码生成器(Mybatis-plus -generator)

自动生成代码

package com.mybatisplus.demo;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;

public class MybatisGenerator {
    /**
     * 读取控制台内容
     */
   /* public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + ":");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "!");
    }*/
    public static void main(String arg[]) {
        String url="jdbc:mysql://localhost:3306/db?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT";
        String userName="root";
        String pwd="root";
        String driver="com.mysql.cj.jdbc.Driver";
        String patentPackage="com.mybatisplus.demo";
        String controllerPackage="controller";
        String entityPackage="pojo";
        String mapperPackage="mapper";
        String xmlPackage="mapper.xml";
        String servicePackage="service";
        String serviceImplPackage="service.impl";
        //数据库表名
        String tableName[]={"user_table","book_table"};
        //配置文件的输出目录,默认输出在D盘根目录
        String projectPath = "f://MybatisGenerator/";

        //创建自动生成器(AutoGenerator)对象
        AutoGenerator generator = new AutoGenerator();
        /**
         * 创建全局配置(GlobalConfig )对象
         * 全局配置
         * 全局配置详情网址:https://mp.baomidou.com/config/generator-config.html#全局策略-globalconfig-配置
         */
        GlobalConfig gConfig = new GlobalConfig();
        //获取当前用户的根目录,把当前用户的根目录作为输出目录
        //String projectPath=System.getProperty("user.dir");
        //gConfig.setOutputDir(projectPath+"src/main/java");
        gConfig.setOutputDir(projectPath + "src/main/java");
        //配置开发者
        gConfig.setAuthor("");
        //是否打开输出目录,默认为true
        gConfig.setOpen(false);
        //开启.Xml文件baseResultMap映射,默认为false
        gConfig.setBaseResultMap(true);
        //开启baseColumnList,默认为false
        gConfig.setBaseColumnList(true);
        //mapper 命名方式默认值:null 例如:%sMapper生成 UserMapper
        gConfig.setMapperName("%sMapper");
        //Mapper xml 命名方式,默认值:null 例如:%sMapper 生成 UserMapper.xml
        gConfig.setXmlName("%sMapper");
        //service 命名方式,默认值:null 例如:%sService生成 UserService
        gConfig.setServiceName("%sService");
        //serviceImpl 命名方式,默认值:null 例如:%sService生成 UserServiceImpl
        gConfig.setServiceImplName("%sServiceImpl");
        //Controller命名方式,默认值:null 例如:%sController生成 UserController
        gConfig.setControllerName("%sController");
        //让全局配置生效
        generator.setGlobalConfig(gConfig);

        /**
         * 数据源配置
         */
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(url);
        dsc.setSchemaName("public");
        dsc.setDriverName(driver);
        dsc.setUsername(userName);
        dsc.setPassword(pwd);
        generator.setDataSource(dsc);

        /**
         * 包名配置
         */
        //创建包名配置(PackageConfig )对象
        PackageConfig pc = new PackageConfig();
        //创建父包模块名
        //pc.setModuleName(scanner("模块名"));
        //pc.setModeuleName("module");
        //创建父包名,若父包名为空, 则子包名必须写全部, 否则就只需写子包名
        pc.setParent(patentPackage);
        //设置Controller包名
        pc.setController(controllerPackage);
        //设置实体类包名
        pc.setEntity(entityPackage);
        //设置mapper包名
        pc.setMapper(mapperPackage);
        //设置xml包名路径
        pc.setXml(xmlPackage);
        //设置service包名
        pc.setService(servicePackage);
        //设置service实现类包名路径
        pc.setServiceImpl(serviceImplPackage);
        //使包配置生效
        generator.setPackageInfo(pc);

        /**
         * 自定义配置,可无
         */
        InjectionConfig injectionConfig = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };
        //freemaker模板引擎
        String templatePath = "/templates/mapper.xml.ftl";
        //velocity模板引擎,MyBatis-Plus 支持 Velocity(默认)
        // String templatePath="/templayes/mapper.xml.vm";
        //自定义输出配置
        List<FileOutConfig> outList = new ArrayList<>();

        // 自定义 xxList.jsp 生成,可无
//        List<FileOutConfig> focList = new ArrayList<FileOutConfig>();
//        focList.add(new FileOutConfig("/template/list.jsp.vm") {
//               @Override
//               public String outputFile(TableInfo tableInfo) {
//                  // 自定义输入文件名称
//                  return "D://my_" + tableInfo.getEntityName() + ".jsp";
//               }
//            });
//        injectionConfig.setFileOutConfigList(focList);
//        generator.setCfg(injectionConfig);

        //自定义生成xml,自已的会被优先输出,可无
//        outList.add(new FileOutConfig(templatePath) {
//            @Override
//            public String outputFile(TableInfo tableInfo) {
//                //自定义输入文件名,若设置了Entity前后缀,则xml的名称会跟着发生变化
//                return projectPath + "src/main/resources/mapper" + "/"
//                        + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
//            }
//        });
        injectionConfig.setFileOutConfigList(outList);
        generator.setCfg(injectionConfig);
        /**
         * 定义配置模板
         */
        //指定自定义模板路径, 位置:/resources/templates/entity2.java.ftl(或者是.vm)
        //注意不要带上.ftl(或者是.vm), 会根据使用的模板引擎自动识别
        // TemplateConfig templateConfig=new TemplateConfig();
        // templateConfig.setEntity("templates/entity2.java");
        // templateConfig.setService("...");
        // templateConfig.setController("...");
        // templateConfig.setXml("...");
        // generator.setTemplate(templateConfig);

        /**
         * 策略配置
         */
        StrategyConfig strategy = new StrategyConfig();
        //驼峰命名
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        //strategy.setSuperEntityClass("com.lz.ant.common.BaseEntity");
        //【实体】是否为lombok模型(默认 false)
        strategy.setEntityLombokModel(true);
        //生成 @RestController 控制器
        strategy.setRestControllerStyle(true);
        //去掉数据表的前缀
        strategy.setTablePrefix("CA_", "RES_");
        //去掉字段的前缀
        strategy.setFieldPrefix("CA_", "RES_");
        // 公共父类
        //strategy.setSuperControllerClass("com.lz.ant.common.BaseController");
        // 写于父类中的公共字段
        //strategy.setSuperEntityColumns("id");
        //strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
        //需要包含的表名,允许正则表达式,多个表名使用英文逗号分割
        strategy.setInclude(tableName);
        strategy.setControllerMappingHyphenStyle(true);
        generator.setStrategy(strategy);
        generator.setTemplateEngine(new FreemarkerTemplateEngine());
        generator.execute();
    }
}

依赖

 	<dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.29</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
            <version>8.0.15</version>
        </dependency>
         <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.2.0</version>
        </dependency>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值