mybatisplus 自动生成实体类、service、mapper

1.导入Maven依赖

         
            com.baomidou
            mybatis-plus-generator
            3.1.2
            test
        
        
            org.apache.velocity
            velocity-engine-core
            2.1
        
        
            com.baomidou
            mybatis-plus-core
            3.0.4
        

2.编写测试类

public class Test {
    private static final String TABLE_NAMES = "数据库表名";

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

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setBaseColumnList(true);
        String projectPath = "路径";
        gc.setOutputDir(projectPath );
        gc.setAuthor("创建人");
        gc.setFileOverride(false);//是否覆盖之前文件
        gc.setOpen(false);
        gc.setSwagger2(true); //实体属性 Swagger2 注解
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("数据库连接url");
        dsc.setSchemaName("数据库名称");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername(登录名);
        dsc.setPassword(登录密码);
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(包全路径);
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };
        mpg.setCfg(cfg);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setEntityBuilderModel(true);
        strategy.setInclude("表");
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix("tb_");//需要截取不用的,比如我的表开头都有tb_,需要去掉后生成
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new VelocityTemplateEngine());
        mpg.execute();
    }
}

3.测试,生成成功

mybatisplus 自动生成实体类、service、mapper_第1张图片

*****这里建议不要直接生成到自己的实际项目中,可以创建临时目录,生成以后copy过去,防止将之前误操作覆盖

你可能感兴趣的:(后台,工具类)