目录

1、添加依赖

<dependency>
			<groupid>com.baomidou</groupid>
			<artifactid>mybatis-plus-boot-starter</artifactid>
			<version>3.3.2</version>
		</dependency>
		<dependency>
			<groupid>com.baomidou</groupid>
			<artifactid>mybatis-plus-generator</artifactid>
			<version>3.3.2</version>
		</dependency>
		<dependency>
			<groupid>org.freemarker</groupid>
			<artifactid>freemarker</artifactid>
			<version>2.3.30</version>
		</dependency>

2、编写代码生成器

public class codegenerator {
    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    public static string scanner(string tip) {
        scanner scanner = new scanner(system.in);
        system.out.println("请输入" + tip + ":");
        if (scanner.hasnext()) {
            string ipt = scanner.next();
            if (stringutils.isnotblank(ipt)) {
                return ipt;
            }
        }
        throw new mybatisplusexception("请输入正确的" + tip + "!");
    }

    public static void main(string[] args) {
        // 代码生成器
        autogenerator mpg = new autogenerator();

        // 全局配置
        globalconfig gc = new globalconfig();
        string projectpath = system.getproperty("user.dir");
        gc.setoutputdir(projectpath + "/check-service/src/main/java");
        gc.setauthor("raoyujie");
        gc.setopen(false);
        // gc.setswagger2(true); 实体属性 swagger2 注解
        mpg.setglobalconfig(gc);

        // 数据源配置
        datasourceconfig dsc = new datasourceconfig();
        dsc.seturl("jdbc:mysql://127.0.0.1:3306/test?useunicode=true&usessl=false&characterencoding=utf8");
        dsc.setdrivername("com.mysql.cj.jdbc.driver");
        dsc.setusername("root");
        dsc.setpassword("root");
        mpg.setdatasource(dsc);

        // 包配置
        packageconfig pc = new packageconfig();
        pc.setparent("com.hrbust.health.check.service");
        mpg.setpackageinfo(pc);

        // 自定义配置
        injectionconfig cfg = new injectionconfig() {
            @override
            public void initmap() {
                // to do nothing
            }
        };

        // 如果模板引擎是 freemarker
        string templatepath = "/templates/mapper.xml.ftl";

        // 自定义输出配置
        list<fileoutconfig> foclist = new arraylist<>();
        // 自定义配置会被优先输出
        foclist.add(new fileoutconfig(templatepath) {
            @override
            public string outputfile(tableinfo tableinfo) {
                // 自定义输出文件名 , 如果你 entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
                return projectpath + "/check-service/src/main/resources/mapper/" + pc.getmodulename()
                        + "/" + tableinfo.getentityname() + "mapper" + stringpool.dot_xml;
            }
        });

        cfg.setfileoutconfiglist(foclist);
        mpg.setcfg(cfg);

        // 配置模板
        templateconfig templateconfig = new templateconfig();

        templateconfig.setxml(null);
        mpg.settemplate(templateconfig);

        // 策略配置
        strategyconfig strategy = new strategyconfig();
        strategy.setnaming(namingstrategy.underline_to_camel);
        strategy.setcolumnnaming(namingstrategy.underline_to_camel);
        strategy.setentitylombokmodel(true);
        strategy.setrestcontrollerstyle(true);
        strategy.setinclude(scanner("表名,多个英文逗号分割").split(","));
        strategy.setcontrollermappinghyphenstyle(true);
        strategy.settableprefix(pc.getmodulename() + "_");
        mpg.setstrategy(strategy);
        mpg.settemplateengine(new freemarkertemplateengine());
        mpg.execute();
    }
}

3、运行主程序,输入表名

到此这篇关于mybatis plus generator 根据数据库自动生成实体类的实现示例的文章就介绍到这了,更多相关mybatis plus generator自动生成实体类内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!