mybatis-plus-generator

代码生成器

  • 说明:是Baomidou开源团队开发的,不依赖于mybatis-plus包的mybatis代码生成器(生成pojo,mapper,service,controller和xml关系映射空文件)

(1)依赖

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-generator</artifactId>
    <version>3.5.0</version>
</dependency>

(2)模板

个人常用的模板

  • 说明:放在测试文件夹中执行便可
// 代码自动生成器
@SpringBootTest
public class MyBatisPlusCodeGenerator {
    @Test
    public static void main(String[] args){
        //需要构建一个代码自动生成器对象    
        AutoGenerator mpg = new AutoGenerator();

        // 1、全局配置 
        GlobalConfig gc=new GlobalConfig();
        String projectPath=System.getProperty("user.dir");
        gc.setOutputDir(projectPath+"/src/main/java");
        gc.setAuthor("Beholder1234");
        gc.setOpen(false); // 是否打开输出目录
        gc.setFileOverride(false);//是否覆盖 
        gc.setServiceName("%sService");//去Service的I前缀  
        gc.setIdType(IdType.AUTO); // 主键策略
        gc.setDateType(DateType.ONLY_DATE); // 设置 数据库时间类型 到 实体类时间类型 对应策略
//        gc.setSwagger2(true);  // 是否生成实体属性 Swagger2注解
        mpg.setGlobalConfig(gc);
        
        // 2、数据源配置  
        DataSourceConfig dsc=new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/demosql?useUnicode=true&characterEncoding=UTF-8");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("13106777006");
        dsc.setDbType(DbType.MYSQL); // 使用什么数据库
        mpg.setDataSource(dsc);
        
        // 3、包的配置 
        PackageConfig pc=new PackageConfig();
        pc.setModuleName("generatorPackage");
        pc.setParent("com.beholder1234.springbootproject1");
        pc.setEntity("pojo"); //配置实体类文件目录名
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setController("controller");
        mpg.setPackageInfo(pc);
        
        //  4、策略配置 
        StrategyConfig strategy=new StrategyConfig();
        strategy.setInclude("user","goods");//设置要映射的表名
        strategy.setNaming(NamingStrategy.underline_to_camel); // 数据库表名下划线转驼峰模式
        strategy.setColumnNaming(NamingStrategy.underline_to_camel); // 数据库元素名下划线转驼峰模式
//        strategy.setEntityLombokModel(true);//自动lombok;
        strategy.setLogicDeleteFieldName("deleted");  // 使用逻辑删除的字段的名字
        //自动填充配置  
/*        TableFill gmtCreate=new TableFill("gmt_create",FieldFill.INSERT);
        TableFill gmtModified=new TableFill("gmt_modified",FieldFill.INSERT_UPDATE);
        ArrayList<TableFill>tableFills=new ArrayList<>();
        tableFills.add(gmtCreate);
        tableFills.add(gmtModified);
        strategy.setTableFillList(tableFills);
        //乐观锁    
/*        strategy.setVersionFieldName("version");
        strategy.setRestControllerStyle(true);
        strategy.setControllerMappingHyphenStyle(true); // localhost:8080/hello_id_2 */
        mpg.setStrategy(strategy);
        mpg.execute(); //执行
        }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值