mybatisPlus的代码生成

MyBatis-Plus代码生成器配置与使用实战
本文介绍了如何在项目中引入MyBatis-Plus代码生成器,详细配置了依赖、YAML文件,并展示了如何生成包括实体类、Mapper、Service等在内的代码。通过设置驼峰命名、自动填充字段等功能,简化了开发流程,提高了开发效率。

坐标

<!--mybatis-plus代码生成器-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.3</version>
        </dependency>
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.31</version>
        </dependency>
        <dependency>
            <groupId>com.ibeetl</groupId>
            <artifactId>beetl</artifactId>
            <version>3.10.0.RELEASE</version>
        </dependency>

        <!--mybatis-plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.0</version>
        </dependency>

配置yml

server:
  port: 8080
spring:
  #项目名称
  application:
    name: demo
  #???
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
    username: root
    password: 12345678

#mybatis-plus配置
mybatis-plus:
  configuration:
    map-underscore-to-camel-case: true #驼峰命名
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #日志
  global-config:
    db-config:
      logic-delete-field: deleted #逻辑删除字段
      logic-delete-value: 1
      logic-not-delete-value: 0

代码生成


public class CodeGenerator {

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

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");//获取当前项目所在目录
        gc.setOutputDir(projectPath + "/src/main/java");//代码生成的目录
        gc.setAuthor("朱云肖");//作者
        gc.setOpen(false);//生成代码后不打开文件夹
        gc.setServiceName("%sService");//取消IService前面的I
        // gc.setSwagger2(true); 实体属性 Swagger2 注解
        gc.setIdType(IdType.ASSIGN_ID);//主键生成策略
        gc.setDateType(DateType.ONLY_DATE);//日期类型
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai");
        // dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("12345678");
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.zyx");//父包
        pc.setModuleName("demo");//模块名
        pc.setEntity("entity");
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setServiceImpl("service.impl");
        pc.setController("controller");
        mpg.setPackageInfo(pc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        //数据库相关
        //strategy.setTablePrefix("tb_");//取消数据库表名的前缀
        strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表名映射实体类使用驼峰
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库列名映射实体类字段名驼峰
        //实体类相关
        //strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");//设置父类实体
        strategy.setEntityLombokModel(true);//实体类的lombok注解
        //mapper相关

        //service相关

        //controller相关
        strategy.setRestControllerStyle(true);//开启@RestController注解
        strategy.setControllerMappingHyphenStyle(true);//url里面使用下划线
        //strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");//设置控制器的父类


        strategy.setLogicDeleteFieldName("deleted");//逻辑删除字段
        strategy.setVersionFieldName("version");//乐观锁字段
        //设置自动填充字段
        TableFill createTime = new TableFill("create_time", FieldFill.INSERT);
        TableFill modifiedTime = new TableFill("modified_time", FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> tableFillList=new ArrayList<>();
        tableFillList.add(createTime);
        tableFillList.add(modifiedTime);
        strategy.setTableFillList(tableFillList);

        mpg.setStrategy(strategy);

        //执行代码生成
        mpg.execute();
    }

}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值